gdb: Avoid signed integer overflow when printing source lines

When printing source lines with calls to print_source_lines we need to
pass a start line number and an end line number.  The end line number
is calculated by calling get_lines_to_list and adding this value to
the start line number.  For example this code from list_command:

    print_source_lines (cursal.symtab, first,
                        first + get_lines_to_list (), 0);

The problem is that get_lines_to_list returns a value based on the
GDB setting `set listsize LISTSIZE`.  By default LISTSIZE is 10,
however, its also possible to set LISTSIZE to unlimited, in which
case get_lines_to_list will return INT_MAX.

As the parameter signature for print_source_lines is:

  void print_source_lines (struct symtab *, int, int,
                           print_source_lines_flags);

and `first` in the above code is an `int`, then when LISTSIZE is
`unlimited` the above code will result in signed integer overflow,
which is undefined.

The solution in this patch is a new class source_lines_range that can
be constructed from a single line number and a direction (forward or
backward).  The range is then constructed from the line number and the
value of get_lines_to_list.

gdb/ChangeLog:

	* cli/cli-cmds.c (list_command): Pass a source_lines_range to
	print_source_lines.
	* source.c (print_source_lines_base): Update line number check.
	(print_source_lines): New function.
	(source_lines_range::source_lines_range): New function.
	* source.h (class source_lines_range): New class.
	(print_source_lines): New declaration.
This commit is contained in:
Andrew Burgess 2019-01-07 07:26:35 +00:00
parent 8379fac67e
commit 0e2a21335b
4 changed files with 115 additions and 26 deletions

View file

@ -895,14 +895,13 @@ list_command (const char *arg, int from_tty)
&& get_lines_to_list () == 1 && first > 1)
first -= 1;
print_source_lines (cursal.symtab, first,
first + get_lines_to_list (), 0);
print_source_lines (cursal.symtab, source_lines_range (first), 0);
}
/* "l" or "l +" lists next ten lines. */
else if (arg == NULL || arg[0] == '+')
print_source_lines (cursal.symtab, cursal.line,
cursal.line + get_lines_to_list (), 0);
print_source_lines (cursal.symtab,
source_lines_range (cursal.line), 0);
/* "l -" lists previous ten lines, the ones before the ten just
listed. */
@ -911,10 +910,9 @@ list_command (const char *arg, int from_tty)
if (get_first_line_listed () == 1)
error (_("Already at the start of %s."),
symtab_to_filename_for_display (cursal.symtab));
print_source_lines (cursal.symtab,
std::max (get_first_line_listed ()
- get_lines_to_list (), 1),
get_first_line_listed (), 0);
source_lines_range range (get_first_line_listed (),
source_lines_range::BACKWARD);
print_source_lines (cursal.symtab, range, 0);
}
return;
@ -1059,9 +1057,11 @@ list_command (const char *arg, int from_tty)
if (dummy_beg && sal_end.symtab == 0)
error (_("No default source file yet. Do \"help list\"."));
if (dummy_beg)
print_source_lines (sal_end.symtab,
std::max (sal_end.line - (get_lines_to_list () - 1), 1),
sal_end.line + 1, 0);
{
source_lines_range range (sal_end.line + 1,
source_lines_range::BACKWARD);
print_source_lines (sal_end.symtab, range, 0);
}
else if (sal.symtab == 0)
error (_("No default source file yet. Do \"help list\"."));
else if (no_end)
@ -1074,17 +1074,14 @@ list_command (const char *arg, int from_tty)
first_line = 1;
if (sals.size () > 1)
print_sal_location (sal);
print_source_lines (sal.symtab,
first_line,
first_line + get_lines_to_list (),
0);
print_source_lines (sal.symtab, source_lines_range (first_line), 0);
}
}
else if (dummy_end)
print_source_lines (sal.symtab, source_lines_range (sal.line), 0);
else
print_source_lines (sal.symtab, sal.line,
(dummy_end
? sal.line + get_lines_to_list ()
: sal_end.line + 1),
print_source_lines (sal.symtab,
source_lines_range (sal.line, (sal_end.line + 1)),
0);
}