From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21157 invoked by alias); 14 Dec 2011 22:06:42 -0000 Received: (qmail 21147 invoked by uid 22791); 14 Dec 2011 22:06:40 -0000 X-SWARE-Spam-Status: No, hits=-3.5 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,RP_MATCHES_RCVD,TW_RV,TW_TR X-Spam-Check-By: sourceware.org Received: from mail-ee0-f73.google.com (HELO mail-ee0-f73.google.com) (74.125.83.73) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 14 Dec 2011 22:06:27 +0000 Received: by eeke49 with SMTP id e49so40975eek.0 for ; Wed, 14 Dec 2011 14:06:26 -0800 (PST) Received: by 10.14.51.74 with SMTP id a50mr146609eec.2.1323900386354; Wed, 14 Dec 2011 14:06:26 -0800 (PST) Received: by 10.14.51.74 with SMTP id a50mr146601eec.2.1323900386254; Wed, 14 Dec 2011 14:06:26 -0800 (PST) Received: from hpza10.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id i11si2766659eea.0.2011.12.14.14.06.26 (version=TLSv1/SSLv3 cipher=AES128-SHA); Wed, 14 Dec 2011 14:06:26 -0800 (PST) Received: from ruffy.mtv.corp.google.com (ruffy.mtv.corp.google.com [172.18.110.50]) by hpza10.eem.corp.google.com (Postfix) with ESMTP id 107A520008C; Wed, 14 Dec 2011 14:06:26 -0800 (PST) Received: by ruffy.mtv.corp.google.com (Postfix, from userid 67641) id 31BFB2461AD; Wed, 14 Dec 2011 14:06:24 -0800 (PST) To: gdb-patches@sourceware.org cc: cmtice@google.com Subject: [patch] handle nested exceptions Message-Id: <20111214220625.31BFB2461AD@ruffy.mtv.corp.google.com> Date: Wed, 14 Dec 2011 23:21:00 -0000 From: dje@google.com (Doug Evans) X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-12/txt/msg00491.txt.bz2 Hi. We ran into a case where gdb was using a previously freed copy of e.message. It's not enough to keep just last_message. This patch creates a stack of messages. I will check this in in a few days if there are no objections. 2011-12-14 Doug Evans * exceptions.c (catcher_list_size): New function. (last_message): Delete. (exception_messages, exception_messages_size): New static globals. (throw_it): Use exception_messages array to handle nested calls. Index: exceptions.c =================================================================== RCS file: /cvs/src/src/gdb/exceptions.c,v retrieving revision 1.48 diff -u -p -r1.48 exceptions.c --- exceptions.c 26 Aug 2011 21:45:22 -0000 1.48 +++ exceptions.c 9 Dec 2011 19:54:36 -0000 @@ -68,6 +68,22 @@ struct catcher /* Where to go for throw_exception(). */ static struct catcher *current_catcher; +/* Return length of current_catcher list. */ + +static int +catcher_list_size (void) +{ + int size; + struct catcher *catcher; + + for (size = 0, catcher = current_catcher; + catcher != NULL; + catcher = catcher->prev) + ++size; + + return size; +} + EXCEPTIONS_SIGJMP_BUF * exceptions_state_mc_init (volatile struct gdb_exception *exception, return_mask mask) @@ -220,8 +236,6 @@ throw_exception (struct gdb_exception ex EXCEPTIONS_SIGLONGJMP (current_catcher->buf, exception.reason); } -static char *last_message; - void deprecated_throw_reason (enum return_reason reason) { @@ -359,23 +373,48 @@ print_any_exception (struct ui_file *fil } } +/* A stack of exception messages. + This is needed to handle nested calls to throw_it: we don't want to + xfree space for a message before it's used. + It is indexed by the size of the current_catcher list. + This is a dynamically allocated array so that we don't care how deeply + GDB nests its TRY_CATCHs. */ +static char **exception_messages; +/* The number of currently allocated entries in exception_messages. */ +static int exception_messages_size; + static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0) throw_it (enum return_reason reason, enum errors error, const char *fmt, va_list ap) { struct gdb_exception e; char *new_message; + int depth = catcher_list_size (); + + gdb_assert (depth > 0); - /* Save the message. Create the new message before deleting the - old, the new message may include the old message text. */ + /* Note: The new message may use an old message's text. */ new_message = xstrvprintf (fmt, ap); - xfree (last_message); - last_message = new_message; + + if (depth > exception_messages_size) + { + int old_size = exception_messages_size; + + exception_messages_size = depth + 10; + exception_messages = (char **) xrealloc (exception_messages, + exception_messages_size + * sizeof (char *)); + memset (exception_messages + old_size, 0, + (exception_messages_size - old_size) * sizeof (char *)); + } + + xfree (exception_messages[depth - 1]); + exception_messages[depth - 1] = new_message; /* Create the exception. */ e.reason = reason; e.error = error; - e.message = last_message; + e.message = new_message; /* Throw the exception. */ throw_exception (e);