diff mbox

[RFA] Fix leak in forward-search

Message ID 20181127233328.5164-1-philippe.waroquiers@skynet.be
State New
Headers show

Commit Message

Philippe Waroquiers Nov. 27, 2018, 11:33 p.m. UTC
Valgrind reports the below leak.
Fix the leak by using xrealloc, even for the first allocation,
as buf is static.

==29158== 5,888 bytes in 23 blocks are definitely lost in loss record 3,028 of 3,149
==29158==    at 0x4C2BE2D: malloc (vg_replace_malloc.c:299)
==29158==    by 0x41B557: xmalloc (common-utils.c:44)
==29158==    by 0x60B7D9: forward_search_command(char const*, int) (source.c:1563)
==29158==    by 0x40BA68: cmd_func(cmd_list_element*, char const*, int) (cli-decode.c:1888)
==29158==    by 0x665300: execute_command(char const*, int) (top.c:630)
...

gdb/ChangeLog
2018-11-28  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

	* source.c (forward_search_command): Fix leak by using
	xrealloc even for the first allocation in the loop, as buf
	is static.
---
 gdb/source.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Pedro Alves Nov. 29, 2018, 3:42 p.m. UTC | #1
On 11/27/2018 11:33 PM, Philippe Waroquiers wrote:
> Valgrind reports the below leak.
> Fix the leak by using xrealloc, even for the first allocation,
> as buf is static.
> 
> ==29158== 5,888 bytes in 23 blocks are definitely lost in loss record 3,028 of 3,149
> ==29158==    at 0x4C2BE2D: malloc (vg_replace_malloc.c:299)
> ==29158==    by 0x41B557: xmalloc (common-utils.c:44)
> ==29158==    by 0x60B7D9: forward_search_command(char const*, int) (source.c:1563)
> ==29158==    by 0x40BA68: cmd_func(cmd_list_element*, char const*, int) (cli-decode.c:1888)
> ==29158==    by 0x665300: execute_command(char const*, int) (top.c:630)
> ...
> 
> gdb/ChangeLog
> 2018-11-28  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
> 
> 	* source.c (forward_search_command): Fix leak by using
> 	xrealloc even for the first allocation in the loop, as buf
> 	is static.

At first sight it would seem like 'buf' was made static to avoid 
allocating a growing buffer for each command invocation.

But then, if that were the case, then you'd want 'cursize' to be
static as well.

The patch is OK, but I think that replacing 'buf' and all that
manual buffer growing with a non-static gdb::def_vector<char> defined
outside the outer loop would be even better.

Thanks,
Pedro Alves
Philippe Waroquiers Nov. 29, 2018, 11:05 p.m. UTC | #2
On Thu, 2018-11-29 at 15:42 +0000, Pedro Alves wrote:
> On 11/27/2018 11:33 PM, Philippe Waroquiers wrote:
> > Valgrind reports the below leak.
> > Fix the leak by using xrealloc, even for the first allocation,
> > as buf is static.
> > 
> > ==29158== 5,888 bytes in 23 blocks are definitely lost in loss record 3,028 of 3,149
> > ==29158==    at 0x4C2BE2D: malloc (vg_replace_malloc.c:299)
> > ==29158==    by 0x41B557: xmalloc (common-utils.c:44)
> > ==29158==    by 0x60B7D9: forward_search_command(char const*, int) (source.c:1563)
> > ==29158==    by 0x40BA68: cmd_func(cmd_list_element*, char const*, int) (cli-decode.c:1888)
> > ==29158==    by 0x665300: execute_command(char const*, int) (top.c:630)
> > ...
> > 
> > gdb/ChangeLog
> > 2018-11-28  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
> > 
> > 	* source.c (forward_search_command): Fix leak by using
> > 	xrealloc even for the first allocation in the loop, as buf
> > 	is static.
> 
> At first sight it would seem like 'buf' was made static to avoid 
> allocating a growing buffer for each command invocation.
> 
> But then, if that were the case, then you'd want 'cursize' to be
> static as well.
> 
> The patch is OK, but I think that replacing 'buf' and all that
> manual buffer growing with a non-static gdb::def_vector<char> defined
> outside the outer loop would be even better.
Thanks for the review, I have pushed this version, but I have added in
my todo list the better fix + add a test : I found no explicit
functional test for this command + my limited time on GDB development is also
shared with analysing the remaining several hundreds tests having a definite
leak :).

Philippe
diff mbox

Patch

diff --git a/gdb/source.c b/gdb/source.c
index e295fbf49e..c75351e65f 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -1560,7 +1560,7 @@  forward_search_command (const char *regex, int from_tty)
       int cursize, newsize;
 
       cursize = 256;
-      buf = (char *) xmalloc (cursize);
+      buf = (char *) xrealloc (buf, cursize);
       p = buf;
 
       c = fgetc (stream.get ());