From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1924 invoked by alias); 27 Feb 2009 18:22:35 -0000 Received: (qmail 1914 invoked by uid 22791); 27 Feb 2009 18:22:32 -0000 X-SWARE-Spam-Status: No, hits=-2.2 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mx2.redhat.com (HELO mx2.redhat.com) (66.187.237.31) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 27 Feb 2009 18:22:25 +0000 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n1RIMMsS022267 for ; Fri, 27 Feb 2009 13:22:22 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n1RIMMZo011852 for ; Fri, 27 Feb 2009 13:22:23 -0500 Received: from localhost.localdomain (vpn-6-77.fab.redhat.com [10.33.6.77]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n1RIMLb1010244 for ; Fri, 27 Feb 2009 13:22:22 -0500 Message-ID: <49A82F5C.9000801@redhat.com> Date: Fri, 27 Feb 2009 19:41:00 -0000 From: Phil Muldoon User-Agent: Thunderbird 2.0.0.19 (X11/20090105) MIME-Version: 1.0 To: gdb-patches@sourceware.org Subject: [patch] valprint.c (read_string): Rework clean-up logic. Content-Type: multipart/mixed; boundary="------------020405090206010209050900" 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: 2009-02/txt/msg00494.txt.bz2 This is a multi-part message in MIME format. --------------020405090206010209050900 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-length: 782 Hi, While performing some Valgrind profiling on GDB we found that the "read_string" function in valprint.c can leak clean-ups. This happens as it does not account for a previous clean-up in the local chain and overwrites the local "old_chain" place holder with a new one. This patch reworks the function's clean-up logic, only registering one clean-up at the beginning and including a new clean-up function. This new clean-up function frees buffers referenced by double indirection. This patch neither increases or decreases regressions in the test-suite. It was built and tested on X86_64. 2009-02-27 Phil Muldoon * valprint.c (xfree_gdb_byte): New function. (read_string): Rework clean-up logic. Use xfree_gdb_byte for clean-up. --------------020405090206010209050900 Content-Type: text/x-patch; name="read_string_cleanup-cvs.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="read_string_cleanup-cvs.patch" Content-length: 1844 Index: gdb/valprint.c =================================================================== RCS file: /cvs/src/src/gdb/valprint.c,v retrieving revision 1.79 diff -u -r1.79 valprint.c --- gdb/valprint.c 5 Feb 2009 12:16:25 -0000 1.79 +++ gdb/valprint.c 27 Feb 2009 17:55:46 -0000 @@ -1177,6 +1177,15 @@ return (nread); } +/* Local clean-up for a gdb_byte buffer pointed to via double + indirection. This function is used in read_string. */ +static void +xfree_gdb_byte (void *buffer) +{ + gdb_byte **pointer = buffer; + xfree (*pointer); +} + /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly allocated buffer containing the string, which the caller is responsible to @@ -1226,13 +1235,14 @@ some error, such as bumping into the end of the address space. */ found_nul = 0; - old_chain = make_cleanup (null_cleanup, 0); + *buffer = NULL; + + old_chain = make_cleanup (xfree_gdb_byte, buffer); if (len > 0) { *buffer = (gdb_byte *) xmalloc (len * width); bufptr = *buffer; - old_chain = make_cleanup (xfree, *buffer); nfetch = partial_memory_read (addr, bufptr, len * width, &errcode) / width; @@ -1243,8 +1253,6 @@ { unsigned long bufsize = 0; - *buffer = NULL; - do { QUIT; @@ -1253,13 +1261,9 @@ if (*buffer == NULL) *buffer = (gdb_byte *) xmalloc (nfetch * width); else - { - discard_cleanups (old_chain); - *buffer = (gdb_byte *) xrealloc (*buffer, - (nfetch + bufsize) * width); - } + *buffer = (gdb_byte *) xrealloc (*buffer, + (nfetch + bufsize) * width); - old_chain = make_cleanup (xfree, *buffer); bufptr = *buffer + bufsize * width; bufsize += nfetch; --------------020405090206010209050900--