From f745574563df068b1cde52726935bf82a1a3c22d Mon Sep 17 00:00:00 2001 From: Tom de Vries 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 #include @@ -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