From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id C0FDD387085C for ; Sun, 28 Jun 2020 16:56:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C0FDD387085C X-ASG-Debug-ID: 1593363391-0c856e14ef1a7bf0001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id hEjsOWq3WZpW0nkr (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 28 Jun 2020 12:56:31 -0400 (EDT) X-Barracuda-Envelope-From: simon.marchi@polymtl.ca X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from localhost.localdomain (173-246-6-90.qc.cable.ebox.net [173.246.6.90]) by smtp.ebox.ca (Postfix) with ESMTP id 07D84441B21; Sun, 28 Jun 2020 12:56:31 -0400 (EDT) From: Simon Marchi X-Barracuda-Effective-Source-IP: 173-246-6-90.qc.cable.ebox.net[173.246.6.90] X-Barracuda-Apparent-Source-IP: 173.246.6.90 X-Barracuda-RBL-IP: 173.246.6.90 To: gdb-patches@sourceware.org Subject: [PATCH 2/3] gdb: make macro_expand_next return a gdb::unique_xmalloc_ptr Date: Sun, 28 Jun 2020 12:56:27 -0400 X-ASG-Orig-Subj: [PATCH 2/3] gdb: make macro_expand_next return a gdb::unique_xmalloc_ptr Message-Id: <20200628165628.96730-3-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200628165628.96730-1-simon.marchi@polymtl.ca> References: <20200628165628.96730-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1593363391 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Barracuda-Scan-Msg-Size: 5477 X-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.50 X-Barracuda-Spam-Status: No, SCORE=0.50 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests=BSF_RULE7568M X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.82869 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_RULE7568M Custom Rule 7568M X-Spam-Status: No, score=-15.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_QUARANTINE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 28 Jun 2020 16:56:33 -0000 For some reason, macro_expand_next does not return a gdb::unique_xmalloc_ptr, like its counterparts macro_expand and macro_expand_once. This patch fixes that. macro_buffer::release now returns a gdb::unique_xmalloc_ptr too, which required updating the other callers. The `.release (). release ()` in macro_stringify looks a bit funny, but it's because one release is for the macro_buffer, and the other is for the unique ptr. I removed the ATTRIBUTE_UNUSED_RESULT on macro_buffer::release, I don't really understand why it's there. I don't see how this method could be called without using the result, that would be an obvious memory leak. The commit that introduced it (4e4a8b932b7 "Add ATTRIBUTE_UNUSED_RESULT to macro_buffer") doesn't give any details. gdb/ChangeLog: * c-exp.y (scan_macro_expansion): Don't free `expansion`. (lex_one_token): Update. * macroexp.c (struct macro_buffer) : Return gdb::unique_xmalloc_ptr. (macro_stringify): Update. (macro_expand): Update. (macro_expand_next): Return gdb::unique_xmalloc_ptr. * macroexp.h (macro_expand_next): Likewise. Change-Id: I67a74d0d479d2c20cdc82161ead7c54cea034f56 --- gdb/c-exp.y | 18 +++++++----------- gdb/macroexp.c | 18 ++++++++---------- gdb/macroexp.h | 3 ++- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/gdb/c-exp.y b/gdb/c-exp.y index 61fa2fe684db..7fc23c4c8d28 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -2551,17 +2551,13 @@ static const struct token ident_tokens[] = static void -scan_macro_expansion (char *expansion) +scan_macro_expansion (const char *expansion) { - const char *copy; - /* We'd better not be trying to push the stack twice. */ gdb_assert (! cpstate->macro_original_text); - /* Copy to the obstack, and then free the intermediate - expansion. */ - copy = obstack_strdup (&cpstate->expansion_obstack, expansion); - xfree (expansion); + /* Copy to the obstack. */ + const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion); /* Save the old lexptr value, so we can return to it when we're done parsing the expanded text. */ @@ -2631,11 +2627,11 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name) /* Check if this is a macro invocation that we need to expand. */ if (! scanning_macro_expansion ()) { - char *expanded = macro_expand_next (&pstate->lexptr, - *expression_macro_scope); + gdb::unique_xmalloc_ptr expanded + = macro_expand_next (&pstate->lexptr, *expression_macro_scope); - if (expanded) - scan_macro_expansion (expanded); + if (expanded != nullptr) + scan_macro_expansion (expanded.get ()); } pstate->prev_lexptr = pstate->lexptr; diff --git a/gdb/macroexp.c b/gdb/macroexp.c index 92823807f153..e1d185d30c8e 100644 --- a/gdb/macroexp.c +++ b/gdb/macroexp.c @@ -128,15 +128,14 @@ struct macro_buffer xfree (text); } - /* Release the text of the buffer to the caller, which is now - responsible for freeing it. */ - ATTRIBUTE_UNUSED_RESULT char *release () + /* Release the text of the buffer to the caller. */ + gdb::unique_xmalloc_ptr release () { gdb_assert (! shared); gdb_assert (size); char *result = text; text = NULL; - return result; + return gdb::unique_xmalloc_ptr (result); } /* Resize the buffer to be at least N bytes long. Raise an error if @@ -708,7 +707,7 @@ macro_stringify (const char *str) stringify (&buffer, str, len); buffer.appendc ('\0'); - return buffer.release (); + return buffer.release ().release (); } @@ -1429,7 +1428,7 @@ macro_expand (const char *source, const macro_scope &scope) dest.appendc ('\0'); - return gdb::unique_xmalloc_ptr (dest.release ()); + return dest.release (); } @@ -1439,8 +1438,7 @@ macro_expand_once (const char *source, const macro_scope &scope) error (_("Expand-once not implemented yet.")); } - -char * +gdb::unique_xmalloc_ptr macro_expand_next (const char **lexptr, const macro_scope &scope) { struct macro_buffer tok; @@ -1454,7 +1452,7 @@ macro_expand_next (const char **lexptr, const macro_scope &scope) /* Get the text's first preprocessing token. */ if (! get_token (&tok, &src)) - return 0; + return nullptr; /* If it's a macro invocation, expand it. */ if (maybe_expand (&dest, &tok, &src, 0, scope)) @@ -1469,6 +1467,6 @@ macro_expand_next (const char **lexptr, const macro_scope &scope) else { /* It wasn't a macro invocation. */ - return 0; + return nullptr; } } diff --git a/gdb/macroexp.h b/gdb/macroexp.h index ec992f22796d..511991cacd20 100644 --- a/gdb/macroexp.h +++ b/gdb/macroexp.h @@ -68,7 +68,8 @@ gdb::unique_xmalloc_ptr macro_expand_once (const char *source, much have to do tokenization to find the end of the string that needs to be macro-expanded. Our C/C++ tokenizer isn't really designed to be called by anything but the yacc parser engine. */ -char *macro_expand_next (const char **lexptr, const macro_scope &scope); +gdb::unique_xmalloc_ptr macro_expand_next (const char **lexptr, + const macro_scope &scope); /* Functions to classify characters according to cpp rules. */ -- 2.27.0