[RFC 8.3 3/3] Avoid a crash in source_cache::extract_lines

Tom Tromey
2019-03-08T21:04:40Z

If the first requested line is larger than the number of lines in the
source buffer, source_cache::extract_lines could crash, because it
would try to pass string::npos" to string::substr.

This patch avoids the crash by checking for this case.

gdb/ChangeLog
2019-03-08 Tom Tromey <tromey@adacore.com>

* source-cache.c (source_cache::extract_lines): Handle case where
first_pos==npos.
---
gdb/ChangeLog | 5 +++++
gdb/source-cache.c | 2 ++
2 files changed, 7 insertions(+)

diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index 27a0ade959c..b5d0d6cb7fc 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -98,6 +98,8 @@ source_cache::extract_lines (const struct source_text &text, int first_line,
{
if (pos == std::string::npos)
pos = text.contents.size ();
+ if (first_pos == std::string::npos)
+ first_pos = text.contents.size ();
*lines = text.contents.substr (first_pos, pos - first_pos);
return true;
}
--
2.20.1