public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: Kevin Buettner <kevinb@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Throw error when creating an overly large gdb-index file
Date: Sat, 9 Sep 2023 10:20:19 +0200	[thread overview]
Message-ID: <e4b62fde-2bbe-2e9a-6f85-8dfce011e189@suse.de> (raw)
In-Reply-To: <20230909025521.3128935-2-kevinb@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 376 bytes --]

On 9/9/23 04:55, Kevin Buettner via Gdb-patches wrote:
> I wish I could provide a test case, but due to the sizes of both the 
> input and output files, I think that testing resources would be strained 
> or exceeded in many environments.

How about this unit test approach?  This fails on master, and could be 
updated to catch the error thrown by the patch.

Thanks,
- Tom


[-- Attachment #2: 0001-selftest.patch --]
[-- Type: text/x-patch, Size: 2814 bytes --]

From f745574563df068b1cde52726935bf82a1a3c22d Mon Sep 17 00:00:00 2001
From: Tom de Vries <tdevries@suse.de>
Date: Sat, 9 Sep 2023 10:15:01 +0200
Subject: [PATCH] selftest

---
 gdb/dwarf2/index-write.c | 53 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)

diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index 11f254e263a..ae1707d827f 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -38,6 +38,7 @@
 #include "objfiles.h"
 #include "ada-lang.h"
 #include "dwarf2/tag.h"
+#include "gdbsupport/selftest.h"
 
 #include <algorithm>
 #include <cmath>
@@ -1071,9 +1072,10 @@ assert_file_size (FILE *file, size_t expected_size)
 }
 
 /* Write a gdb index file to OUT_FILE from all the sections passed as
-   arguments.  */
+   arguments, and return the amount of bytes written.  If OUT_FILE is nullptr,
+   return the amount of bytes that is supposed to be written.  */
 
-static void
+static size_t
 write_gdbindex_1 (FILE *out_file,
 		  const data_buf &cu_list,
 		  const data_buf &types_cu_list,
@@ -1110,6 +1112,9 @@ write_gdbindex_1 (FILE *out_file,
 
   gdb_assert (contents.size () == size_of_header);
 
+  if (out_file == nullptr)
+    return total_len;
+
   contents.file_write (out_file);
   cu_list.file_write (out_file);
   types_cu_list.file_write (out_file);
@@ -1118,6 +1123,8 @@ write_gdbindex_1 (FILE *out_file,
   constant_pool.file_write (out_file);
 
   assert_file_size (out_file, total_len);
+
+  return total_len;
 }
 
 /* Write the contents of the internal "cooked" index.  */
@@ -1530,10 +1537,52 @@ save_gdb_index_command (const char *arg, int from_tty)
     }
 }
 
+#if GDB_SELF_TEST
+
+namespace {
+
+class pretend_data_buf : public data_buf {
+public:
+  void set_pretend_size (size_t s) {
+    m_pretend_size = s;
+  }
+  size_t size () {
+    return m_pretend_size;
+  }
+private:
+  size_t m_pretend_size;
+};
+
+void
+test_index_write ()
+{
+  pretend_data_buf cu_list;
+  pretend_data_buf types_cu_list;
+  pretend_data_buf addr_vec;
+  pretend_data_buf symtab_vec;
+  pretend_data_buf constant_pool;
+
+  symtab_vec.set_pretend_size (~(offset_type)0);
+  constant_pool.set_pretend_size (1);
+
+  size_t res
+    = write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec, symtab_vec, constant_pool);
+
+  /* Check that silent wraparound does not occur.  */
+  SELF_CHECK (res != 24);
+}
+
+} /* anonymous namespace */
+#endif
+
 void _initialize_dwarf_index_write ();
 void
 _initialize_dwarf_index_write ()
 {
+#if GDB_SELF_TEST
+  selftests::register_test ("test_index_write", test_index_write);
+#endif
+
   cmd_list_element *c = add_cmd ("gdb-index", class_files,
 				 save_gdb_index_command, _("\
 Save a gdb-index file.\n\

base-commit: 38a984fa440c7686c741b7804eae06a528849aa7
-- 
2.35.3


  parent reply	other threads:[~2023-09-09  8:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-09  2:55 Kevin Buettner
2023-09-09  5:19 ` Kevin Buettner
2023-09-09  8:20 ` Tom de Vries [this message]
2023-09-15  0:22   ` Kevin Buettner
2023-09-15  9:09     ` Tom de Vries
2023-09-12 16:07 ` Tom Tromey
2023-09-13  2:43   ` Kevin Buettner
2023-09-13 14:24     ` Tom Tromey
2023-09-15  0:10       ` Kevin Buettner

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=e4b62fde-2bbe-2e9a-6f85-8dfce011e189@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    --cc=kevinb@redhat.com \
    /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).