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 7601D3858C54 for ; Tue, 12 Apr 2022 01:42:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7601D3858C54 X-ASG-Debug-ID: 1649727744-0c856e06abb5b040001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id jHthciCUei1JOlPY (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 11 Apr 2022 21:42:24 -0400 (EDT) X-Barracuda-Envelope-From: simon.marchi@polymtl.ca X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from simark.localdomain (192-222-157-6.qc.cable.ebox.net [192.222.157.6]) by smtp.ebox.ca (Postfix) with ESMTP id 64DB2441B21; Mon, 11 Apr 2022 21:42:24 -0400 (EDT) From: Simon Marchi X-Barracuda-RBL-IP: 192.222.157.6 X-Barracuda-Effective-Source-IP: 192-222-157-6.qc.cable.ebox.net[192.222.157.6] X-Barracuda-Apparent-Source-IP: 192.222.157.6 To: gdb-patches@sourceware.org Subject: [PATCH 1/4] gdb: allocate subfile with new Date: Mon, 11 Apr 2022 21:42:20 -0400 X-ASG-Orig-Subj: [PATCH 1/4] gdb: allocate subfile with new Message-Id: <20220412014223.2684060-1-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1649727744 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-Scan-Msg-Size: 6055 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.97292 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-Spam-Status: No, score=-3613.0 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Tue, 12 Apr 2022 01:42:29 -0000 Allocate struct subfile with new, initialize its fields instead of memset-ing it to 0. Use a unique_ptr for the window after a subfile has been allocated but before it is linked in the buildsym_compunit's list of subfile (and therefore owned by the buildsym_compunit. I can't test the change in xcoffread.c, it's best-effort. I couldn't find where subfiles are freed in that file, I assume they were intentionally (or not) leaked. Change-Id: Ib3b6877de31b7e65bc466682f08dbf5840225f24 --- gdb/buildsym.c | 37 ++++++++++++++++--------------------- gdb/buildsym.h | 22 ++++++++++++++++------ gdb/xcoffread.c | 10 ++-------- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/gdb/buildsym.c b/gdb/buildsym.c index 0df355151f45..aedc3c67b9ac 100644 --- a/gdb/buildsym.c +++ b/gdb/buildsym.c @@ -97,7 +97,7 @@ buildsym_compunit::~buildsym_compunit () nextsub = subfile->next; xfree (subfile->name); xfree (subfile->line_vector); - xfree (subfile); + delete subfile; } struct pending *next, *next1; @@ -504,13 +504,12 @@ void buildsym_compunit::start_subfile (const char *name) { const char *subfile_dirname; - struct subfile *subfile; subfile_dirname = m_comp_dir.get (); /* See if this subfile is already registered. */ - for (subfile = m_subfiles; subfile; subfile = subfile->next) + for (subfile *subfile = m_subfiles; subfile; subfile = subfile->next) { char *subfile_name; @@ -537,13 +536,9 @@ buildsym_compunit::start_subfile (const char *name) /* This subfile is not known. Add an entry for it. */ - subfile = XNEW (struct subfile); - memset (subfile, 0, sizeof (struct subfile)); - - subfile->next = m_subfiles; - m_subfiles = subfile; + subfile_up subfile (new struct subfile); - m_current_subfile = subfile; + m_current_subfile = subfile.get (); subfile->name = xstrdup (name); @@ -562,11 +557,8 @@ buildsym_compunit::start_subfile (const char *name) source file. */ subfile->language = deduce_language_from_filename (subfile->name); - if (subfile->language == language_unknown - && subfile->next != NULL) - { - subfile->language = subfile->next->language; - } + if (subfile->language == language_unknown && m_subfiles != nullptr) + subfile->language = m_subfiles->language; /* If the filename of this subfile ends in .C, then change the language of any pending subfiles from C to C++. We also accept @@ -586,12 +578,14 @@ buildsym_compunit::start_subfile (const char *name) /* And patch up this file if necessary. */ if (subfile->language == language_c - && subfile->next != NULL - && (subfile->next->language == language_cplus - || subfile->next->language == language_fortran)) - { - subfile->language = subfile->next->language; - } + && m_subfiles != nullptr + && (m_subfiles->language == language_cplus + || m_subfiles->language == language_fortran)) + subfile->language = m_subfiles->language; + + /* Link this subfile at the front of the subfile list. */ + subfile->next = m_subfiles; + m_subfiles = subfile.release (); } /* For stabs readers, the first N_SO symbol is assumed to be the @@ -791,7 +785,8 @@ buildsym_compunit::watch_main_source_file_lossage () else prev_mainsub_alias->next = mainsub_alias->next; xfree (mainsub_alias->name); - xfree (mainsub_alias); + + delete mainsub_alias; } } } diff --git a/gdb/buildsym.h b/gdb/buildsym.h index 5f0e0230fd9f..50ecd33d5441 100644 --- a/gdb/buildsym.h +++ b/gdb/buildsym.h @@ -45,16 +45,26 @@ struct dynamic_prop; struct subfile { - struct subfile *next; + subfile () = default; + + /* There's nothing wrong with copying a subfile, but we don't need to, so use + this to avoid copying one by mistake. */ + DISABLE_COPY_AND_ASSIGN (subfile); + + struct subfile *next = nullptr; + /* Space for this is malloc'd. */ - char *name; + char *name = nullptr; + /* Space for this is malloc'd. */ - struct linetable *line_vector; - int line_vector_length; - enum language language; - struct symtab *symtab; + struct linetable *line_vector = nullptr; + int line_vector_length = 0; + enum language language = language_unknown; + struct symtab *symtab = nullptr; }; +using subfile_up = std::unique_ptr; + /* Record the symbols defined for each context in a list. We don't create a struct block for the context until we know how long to make it. */ diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c index c5d2d0a492f8..296e71356dde 100644 --- a/gdb/xcoffread.c +++ b/gdb/xcoffread.c @@ -632,8 +632,6 @@ process_linenos (CORE_ADDR start, CORE_ADDR end) if (offset == 0) goto return_after_cleanup; - memset (&main_subfile, '\0', sizeof (main_subfile)); - if (inclIndx == 0) /* All source lines were in the main source file. None in include files. */ @@ -651,8 +649,6 @@ process_linenos (CORE_ADDR start, CORE_ADDR end) for (ii = 0; ii < inclIndx; ++ii) { - struct subfile *tmpSubfile; - /* If there is main file source before include file, enter it. */ if (offset < inclTable[ii].begin) { @@ -675,14 +671,12 @@ process_linenos (CORE_ADDR start, CORE_ADDR end) else { /* Have a new subfile for the include file. */ + inclTable[ii].subfile = new subfile; - tmpSubfile = inclTable[ii].subfile = XNEW (struct subfile); - - memset (tmpSubfile, '\0', sizeof (struct subfile)); firstLine = &(inclTable[ii].funStartLine); /* Enter include file's lines now. */ - enter_line_range (tmpSubfile, inclTable[ii].begin, + enter_line_range (inclTable[ii].subfile, inclTable[ii].begin, inclTable[ii].end, start, 0, firstLine); } base-commit: 50192212a72b48de7ae4d87c79d394f4e3461a5b -- 2.35.1