Fix leak in linespec parser
authorSimon Marchi <simon.marchi@ericsson.com>
Fri, 30 Nov 2018 21:49:35 +0000 (16:49 -0500)
committerSimon Marchi <simon.marchi@ericsson.com>
Fri, 30 Nov 2018 21:51:28 +0000 (16:51 -0500)
commit4717cec4fe4cb3a086fb13161603112e8ded787e
tree40120eb16f1a6de11a85fd0ef6c1012db6b19472
parentf8eb6a9e8986c6428de629b5538e431e1c04caf1
Fix leak in linespec parser

Valgrind reports this leak:

  ==798== VALGRIND_GDB_ERROR_BEGIN
  ==798== 32 (24 direct, 8 indirect) bytes in 1 blocks are definitely lost in loss record 447 of 3,143
  ==798==    at 0x4C2C48C: operator new(unsigned long) (vg_replace_malloc.c:334)
  ==798==    by 0x51D401: linespec_parser_new(ls_parser*, int, language_defn const*, program_space*, symtab*, int, linespec_result*) (linespec.c:2756)
  ==798==    by 0x524BF7: decode_line_full(event_location const*, int, program_space*, symtab*, int, linespec_result*, char const*, char const*) (linespec.c:3271)
  ==798==    by 0x3E8893: parse_breakpoint_sals(event_location const*, linespec_result*) (breakpoint.c:9067)
  ==798==    by 0x3E4E7F: create_breakpoint(gdbarch*, event_location const*, char const*, int, char const*, int, int, bptype, int, auto_boolean, breakpoint_ops const*, int, int, int, unsigned int) (breakpoint.c:9248)
  ==798==    by 0x3E55F5: break_command_1(char const*, int, int) (breakpoint.c:9434)
  ==798==    by 0x40BA68: cmd_func(cmd_list_element*, char const*, int) (cli-decode.c:1888)
  ==798==    by 0x665300: execute_command(char const*, int) (top.c:630)
  ...

linespec_parser_new allocates a std::vector<symtab *> at line 2756, and stores
the pointer to this vector in PARSER_RESULT (parser)->file_symtabs.  At 3
different places in linespec.c, another std::vector is assigned to a
linespec->file_symtabs, without first deleting the current value.

The leak is fixed by assigning the vector itself instead of the pointer.
Everything should be moved, so there is no significant data copy
involved.

Tested on debian/amd64, + a bunch of tests re-run under valgrind
(including the test that throws an error).

gdb/ChangeLog:

* linespec.c (symtab_vector_up): Remove.
(symtabs_from_filename): Change return type to std::vector.
(collect_symtabs_from_filename): Likewise.
(create_sals_line_offset): Assign return value of
collect_symtabs_from_filename to *ls->file_symtabs.
(convert_explicit_location_to_linespec): Remove call to release.
(parse_linespec): Likewise.
(symtab_collector) <symtab_collector>: Remove initialization of
m_symtabs.
<release_symtabs>: Change return type to std::vector<symtab *>.
<operator ()>: Adjust.
gdb/ChangeLog
gdb/linespec.c