public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Aaron Merey <amerey@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] gdb/debuginfod: Prevent out_of_range exception
Date: Wed, 20 Apr 2022 22:08:39 +0000 (GMT)	[thread overview]
Message-ID: <20220420220839.0A61E3858D1E@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=52449404c4e38000351e8f2c7b768f12fd808eda

commit 52449404c4e38000351e8f2c7b768f12fd808eda
Author: Aaron Merey <amerey@redhat.com>
Date:   Fri Apr 8 18:50:56 2022 -0400

    gdb/debuginfod: Prevent out_of_range exception
    
    Trailing whitespace in the string of debuginfod URLs causes an
    out_of_range exception during the printing of URLs for the first
    use notice.
    
    To fix this, stop printing URLs when the substring to be printed
    consists only of whitespace.
    
    Also add first use notice testcases.
    
    Co-Authored-By: Pedro Alves <pedro@palves.net>

Diff:
---
 gdb/debuginfod-support.c                           |  7 +--
 .../gdb.debuginfod/fetch_src_and_symbols.exp       | 62 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/gdb/debuginfod-support.c b/gdb/debuginfod-support.c
index 6c2d3fb2951..4ce2e786b6a 100644
--- a/gdb/debuginfod-support.c
+++ b/gdb/debuginfod-support.c
@@ -187,10 +187,11 @@ debuginfod_is_enabled ()
       gdb::string_view url_view (urls);
       while (true)
 	{
-	  url_view = url_view.substr (url_view.find_first_not_of (' '));
-	  if (url_view.empty ())
+	  size_t off = url_view.find_first_not_of (' ');
+	  if (off == gdb::string_view::npos)
 	    break;
-	  size_t off = url_view.find_first_of (' ');
+	  url_view = url_view.substr (off);
+	  off = url_view.find_first_of (' ');
 	  gdb_printf
 	    (_("  <%ps>\n"),
 	     styled_string (file_name_style.style (),
diff --git a/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp b/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp
index f12ed7d486c..6da9a86faa8 100644
--- a/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp
+++ b/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp
@@ -186,6 +186,28 @@ proc no_url { } {
     gdb_test "core $::corefile" ".*in ?? ().*" "file [file tail $::corefile]"
 }
 
+# Test that GDB prints the debuginfod URLs when loading files.  URLS
+# is the string set in the DEBUGINFOD_URLS environment variable.
+# PATTERN_RE is the URLs pattern we expect to see out of GDB.  TEST is
+# the test name.
+
+proc test_urls {urls pattern_re test} {
+    setenv DEBUGINFOD_URLS $urls
+    clean_restart
+
+    if {$pattern_re != ""} {
+	set urls_re " +${pattern_re}\r\n"
+    } else {
+	set urls_re ""
+    }
+
+    # Use "with confirm off" to avoid having to deal with the
+    # "Enable debuginfod for this session? (y or [n])" question.
+    gdb_test "with confirm off -- file $::binfile" \
+	"following URLs:\r\n${urls_re}Debuginfod .*" \
+	$test
+}
+
 proc local_url { } {
     global binfile outputdir db debugdir
 
@@ -279,6 +301,46 @@ proc local_url { } {
     gdb_test_no_output "set debuginfod enabled on"
     gdb_test "file $binfile" ".*Reading symbols from.*debuginfo.*" \
 	"file [file tail $binfile] cmd on"
+
+    # Test that URLs are printed correctly for the first-use notice.
+
+    # Empty URLS disables Debuginfod.
+    setenv DEBUGINFOD_URLS ""
+    clean_restart
+    # Disable confirmation to avoid having to deal with a query.  See
+    # test_urls.
+    gdb_test_multiple "with confirm off -- file $binfile" "notice empty URL" {
+	-re ".*Enable debuginfod.*" {
+	    fail $gdb_test_name
+	}
+	-re -wrap "" {
+	    pass $gdb_test_name
+	}
+    }
+
+    test_urls "   " \
+        "" \
+        "notice whitespace URL"
+
+    set url "http://127.0.0.1:$port"
+
+    test_urls $url \
+	"<$url>" \
+	"notice 1 URL"
+
+    test_urls "  $url  " \
+	"<$url>" \
+	"notice 1 URL with whitespace"
+
+    set url2 "127.0.0.1:$port"
+
+    test_urls "$url $url2" \
+	"<$url>\r\n +<$url2>" \
+	"notice 2 URLs"
+
+    test_urls "  $url $url2  " \
+	"<$url>\r\n +<$url2>" \
+	"notice 2 URLs with whitespace"
 }
 
 set envlist \


                 reply	other threads:[~2022-04-20 22:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220420220839.0A61E3858D1E@sourceware.org \
    --to=amerey@sourceware.org \
    --cc=gdb-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).