This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[RFC 8.3 3/3] Avoid a crash in source_cache::extract_lines
- From: Tom Tromey <tromey at adacore dot com>
- To: gdb-patches at sourceware dot org
- Cc: Tom Tromey <tromey at adacore dot com>
- Date: Fri, 8 Mar 2019 14:04:33 -0700
- Subject: [RFC 8.3 3/3] Avoid a crash in source_cache::extract_lines
- References: <20190308210433.32683-1-tromey@adacore.com>
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