Skip to content

Commit bb0da2b

Browse files
author
Philippe Waroquiers
committed
Fix leak of identifier in macro definition.
Valgrind detects leaks like the following (gdb.base/macscp.exp). This patch fixes 1 of the 3 leaks (the last one in the list below). The remaining leaks are better fixed in splay_tree_remove and splay_tree_insert in libiberty. Tested on debian/amd64, natively and under valgrind. ==22285== 64 (48 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record 737 of 3,377 ==22285== at 0x4C2BE6D: malloc (vg_replace_malloc.c:309) ==22285== by 0x4049E7: xmalloc (common-utils.c:44) ==22285== by 0x533A20: new_macro_key(macro_table*, char const*, macro_source_file*, int) (macrotab.c:355) ==22285== by 0x53438B: macro_define_function(macro_source_file*, int, char const*, int, char const**, char const*) (macrotab.c:822) ==22285== by 0x52F945: macro_define_command(char const*, int) (macrocmd.c:409) ... ==22285== 128 (96 direct, 32 indirect) bytes in 2 blocks are definitely lost in loss record 1,083 of 3,377 ==22285== at 0x4C2BE6D: malloc (vg_replace_malloc.c:309) ==22285== by 0x4049E7: xmalloc (common-utils.c:44) ==22285== by 0x533A20: new_macro_key(macro_table*, char const*, macro_source_file*, int) (macrotab.c:355) ==22285== by 0x534277: macro_define_object_internal(macro_source_file*, int, char const*, char const*, macro_special_kind) (macrotab.c:776) ==22285== by 0x52F7E0: macro_define_command(char const*, int) (macrocmd.c:414) ... ==22285== 177 bytes in 19 blocks are definitely lost in loss record 1,193 of 3,377 ==22285== at 0x4C2BE6D: malloc (vg_replace_malloc.c:309) ==22285== by 0x4049E7: xmalloc (common-utils.c:44) ==22285== by 0x52F5BD: extract_identifier(char const**, int) (macrocmd.c:316) ==22285== by 0x52F77D: macro_define_command(char const*, int) (macrocmd.c:355) gdb/ChangeLog 2019-02-06 Philippe Waroquiers <[email protected]> * macrocmd.c (extract_identifier): Return a gdb::unique_xmalloc_ptr<char> instead of a char *, and update callers.
1 parent 424eb55 commit bb0da2b

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

gdb/ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2019-02-06 Philippe Waroquiers <[email protected]>
2+
3+
* macrocmd.c (extract_identifier): Return
4+
a gdb::unique_xmalloc_ptr<char> instead of a char *, and update
5+
callers.
6+
17
2019-02-06 John Baldwin <[email protected]>
28

39
* fbsd-nat.c (fbsd_fetch_cmdline): Join arguments with spaces.

gdb/macrocmd.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ skip_ws (const char **expp)
288288
function will also allow "..." forms as used in varargs macro
289289
parameters. */
290290

291-
static char *
291+
static gdb::unique_xmalloc_ptr<char>
292292
extract_identifier (const char **expp, int is_parameter)
293293
{
294294
char *result;
@@ -317,7 +317,7 @@ extract_identifier (const char **expp, int is_parameter)
317317
memcpy (result, *expp, len);
318318
result[len] = '\0';
319319
*expp += len;
320-
return result;
320+
return gdb::unique_xmalloc_ptr<char> (result);
321321
}
322322

323323
struct temporary_macro_definition : public macro_definition
@@ -346,14 +346,13 @@ static void
346346
macro_define_command (const char *exp, int from_tty)
347347
{
348348
temporary_macro_definition new_macro;
349-
char *name = NULL;
350349

351350
if (!exp)
352351
error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
353352

354353
skip_ws (&exp);
355-
name = extract_identifier (&exp, 0);
356-
if (! name)
354+
gdb::unique_xmalloc_ptr<char> name = extract_identifier (&exp, 0);
355+
if (name == NULL)
357356
error (_("Invalid macro name."));
358357
if (*exp == '(')
359358
{
@@ -380,7 +379,7 @@ macro_define_command (const char *exp, int from_tty)
380379
/* Must update new_macro as well... */
381380
new_macro.argv = (const char * const *) argv;
382381
}
383-
argv[new_macro.argc] = extract_identifier (&exp, 1);
382+
argv[new_macro.argc] = extract_identifier (&exp, 1).release ();
384383
if (! argv[new_macro.argc])
385384
error (_("Macro is missing an argument."));
386385
++new_macro.argc;
@@ -404,32 +403,30 @@ macro_define_command (const char *exp, int from_tty)
404403
++exp;
405404
skip_ws (&exp);
406405

407-
macro_define_function (macro_main (macro_user_macros), -1, name,
406+
macro_define_function (macro_main (macro_user_macros), -1, name.get (),
408407
new_macro.argc, (const char **) new_macro.argv,
409408
exp);
410409
}
411410
else
412411
{
413412
skip_ws (&exp);
414-
macro_define_object (macro_main (macro_user_macros), -1, name, exp);
413+
macro_define_object (macro_main (macro_user_macros), -1, name.get (),
414+
exp);
415415
}
416416
}
417417

418418

419419
static void
420420
macro_undef_command (const char *exp, int from_tty)
421421
{
422-
char *name;
423-
424422
if (!exp)
425423
error (_("usage: macro undef NAME"));
426424

427425
skip_ws (&exp);
428-
name = extract_identifier (&exp, 0);
429-
if (! name)
426+
gdb::unique_xmalloc_ptr<char> name = extract_identifier (&exp, 0);
427+
if (name == nullptr)
430428
error (_("Invalid macro name."));
431-
macro_undef (macro_main (macro_user_macros), -1, name);
432-
xfree (name);
429+
macro_undef (macro_main (macro_user_macros), -1, name.get ());
433430
}
434431

435432

0 commit comments

Comments
 (0)