cli-script: use unique_ptr to not leak next struct

In cli/cli-script.c, process_next_line() allocates memory
which will eventually end up being assigned to the 'next'
field in struct command_line.  However, in a case
recurse_read_control_structure returns 'invalid_control'
this memory is leaked. This commit uses std::unique_ptr
as appropriate to prevent this leakage.

This issue was found by coverity scanning.

gdb/ChangeLog:

        * cli/cli-script.h (command_line_up): New unique_ptr typedef.
	* cli/cli-script.c (multi_line_command_p): Use unique_ptr
        command_line_up instead of struct command_line.
	(build_command_line): Likewise.
	(get_command_line): Update the cmd function call parameter.
	(process_next_line):  Use unique_ptr command_line_up instead
        of struct command_line.
	(recurse_read_control_structure): Change the the type of
        next to command_line_up.
	(read_command_lines_1): Change type of `next' to be
        command_line_up and update all references of `next'
        accordingly.
This commit is contained in:
Alexandra Hájková 2021-05-20 20:55:35 +02:00
parent 9a01ec4c03
commit bb6203bf1d
3 changed files with 55 additions and 36 deletions

View file

@ -66,6 +66,9 @@ struct command_lines_deleter
/* A reference-counted struct command_line. */
typedef std::shared_ptr<command_line> counted_command_line;
/* A unique_ptr specialization for command_line. */
typedef std::unique_ptr<command_line, command_lines_deleter> command_line_up;
/* * Structure for saved commands lines (for breakpoints, defined
commands, etc). */