diff options
author | Andrew Burgess <andrew.burgess@embecosm.com> | 2019-01-07 07:26:35 +0000 |
---|---|---|
committer | Andrew Burgess <andrew.burgess@embecosm.com> | 2019-01-09 14:11:24 +0000 |
commit | 0e2a21335b6fc4a5b6bed19d9623916c52918b72 (patch) | |
tree | d476440be29fbef1a438b80788c5cb30d94011a5 /gdb/source.c | |
parent | 8379fac67e963e0d12649c58f79d52824a7eafdf (diff) | |
download | gdb-0e2a21335b6fc4a5b6bed19d9623916c52918b72.zip gdb-0e2a21335b6fc4a5b6bed19d9623916c52918b72.tar.gz gdb-0e2a21335b6fc4a5b6bed19d9623916c52918b72.tar.bz2 |
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.
Diffstat (limited to 'gdb/source.c')
-rw-r--r-- | gdb/source.c | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/gdb/source.c b/gdb/source.c index f865c8a..1f10379 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -1331,13 +1331,8 @@ print_source_lines_base (struct symtab *s, int line, int stopline, last_source_error = 0; /* If the user requested a sequence of lines that seems to go backward - (from high to low line numbers) then we don't print anything. - The use of '- 1' here instead of '<=' is currently critical, we rely - on the undefined wrap around behaviour of 'int' for stopline. When - the use has done: 'set listsize unlimited' then stopline can overflow - and appear as MIN_INT. This is a long-standing bug that needs - fixing. */ - if (stopline - 1 < line) + (from high to low line numbers) then we don't print anything. */ + if (stopline <= line) return; std::string lines; @@ -1399,6 +1394,18 @@ print_source_lines (struct symtab *s, int line, int stopline, { print_source_lines_base (s, line, stopline, flags); } + +/* See source.h. */ + +void +print_source_lines (struct symtab *s, source_lines_range line_range, + print_source_lines_flags flags) +{ + print_source_lines_base (s, line_range.startline (), + line_range.stopline (), flags); +} + + /* Print info on range of pc's in a specified line. */ @@ -1822,6 +1829,33 @@ set_substitute_path_command (const char *args, int from_tty) forget_cached_source_info (); } +/* See source.h. */ + +source_lines_range::source_lines_range (int startline, + source_lines_range::direction dir) +{ + if (dir == source_lines_range::FORWARD) + { + LONGEST end = static_cast <LONGEST> (startline) + get_lines_to_list (); + + if (end > INT_MAX) + end = INT_MAX; + + m_startline = startline; + m_stopline = static_cast <int> (end); + } + else + { + LONGEST start = static_cast <LONGEST> (startline) - get_lines_to_list (); + + if (start < 1) + start = 1; + + m_startline = static_cast <int> (start); + m_stopline = startline; + } +} + void _initialize_source (void) |