From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id BEA9C3858CDA for ; Fri, 15 Sep 2023 00:22:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BEA9C3858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694737348; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tiPfKmWT1OYh1S6QfbjYSZc00Z9/f6VqRmggz5o7OCI=; b=b94HxcL3o5UgSsCpWzYsAgEuw1KrxYsTEfy4K3dXxy5Bh+D9GfLFiZGC4ezLOf5H2cs/mH npbWbDbwqzYIfKqGCocX5O1H9BmH5DgL31HpVZOKzK2+hch0vG9YONMEI/7bqd3OqsrYnZ quucTeMR9kvwb5dVRwILG4MOsdg65L4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-391-TOf0bYcoNT2pa_tbRg8WZA-1; Thu, 14 Sep 2023 20:22:26 -0400 X-MC-Unique: TOf0bYcoNT2pa_tbRg8WZA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7EE3A185A70C; Fri, 15 Sep 2023 00:22:26 +0000 (UTC) Received: from f37-zws-nv (unknown [10.22.32.95]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2963A10F1BE7; Fri, 15 Sep 2023 00:22:26 +0000 (UTC) Date: Thu, 14 Sep 2023 17:22:24 -0700 From: Kevin Buettner To: Tom de Vries Cc: gdb-patches@sourceware.org Subject: Re: [PATCH] Throw error when creating an overly large gdb-index file Message-ID: <20230914172224.68a2d70c@f37-zws-nv> In-Reply-To: References: <20230909025521.3128935-2-kevinb@redhat.com> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Sat, 9 Sep 2023 10:20:19 +0200 Tom de Vries wrote: > 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=20 > > input and output files, I think that testing resources would be straine= d=20 > > or exceeded in many environments. =20 >=20 > How about this unit test approach? This fails on master, and could be=20 > updated to catch the error thrown by the patch. I like it. I made several tweaks resulting in the version below. Does it look okay to you? >From 1999dc14bf8c498c3f0db83eeb8f7e6d7376e8ff Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Sat, 9 Sep 2023 10:15:01 +0200 Subject: [PATCH] Add unit test for overly large gdb-index file This commit adds a test which checks that write_gdb_index_1 will throw an error when the size of the file would exceed the maximum value capable of being represented by 'offset_type'. Co-Authored-By: Kevin Buettner diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index 3827a810130..fd9605700c8 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -137,7 +137,7 @@ class data_buf } =20 /* Return the size of the buffer. */ - size_t size () const + virtual size_t size () const { return m_vec.size (); } @@ -1117,6 +1117,9 @@ write_gdbindex_1 (FILE *out_file, if (total_len > max_size) error (_("gdb-index maximum file size of %zu exceeded"), max_size); =20 + if (out_file =3D=3D nullptr) + return; + contents.file_write (out_file); cu_list.file_write (out_file); types_cu_list.file_write (out_file); @@ -1537,10 +1540,70 @@ save_gdb_index_command (const char *arg, int from_t= ty) } } =20 +#if GDB_SELF_TEST +#include "gdbsupport/selftest.h" + +namespace selftests { + +class pretend_data_buf : public data_buf +{ +public: + /* Set the pretend size. */ + void set_pretend_size (size_t s) { + m_pretend_size =3D s; + } + + /* Override size method of data_buf, returning the pretend size instead.= */ + size_t size () const override { + return m_pretend_size; + } + +private: + size_t m_pretend_size; +}; + +static void +gdb_index () +{ + 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; + + /* Test that an overly large index will throw an error. */ + symtab_vec.set_pretend_size (~(offset_type)0); + constant_pool.set_pretend_size (1); + + bool saw_exception =3D false; + try + { + write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec, + symtab_vec, constant_pool); + } + catch (const gdb_exception_error &e) + { + SELF_CHECK (e.reason =3D=3D RETURN_ERROR); + SELF_CHECK (e.error =3D=3D GENERIC_ERROR); + SELF_CHECK (e.message->find (_("gdb-index maximum file size of"))=20 + !=3D std::string::npos); + SELF_CHECK (e.message->find (_("exceeded")) !=3D std::string::npos); + saw_exception =3D true; + } + SELF_CHECK (saw_exception); +} + +} /* selftests namespace. */ +#endif + void _initialize_dwarf_index_write (); void _initialize_dwarf_index_write () { +#if GDB_SELF_TEST + selftests::register_test ("gdb_index", selftests::gdb_index); +#endif + cmd_list_element *c =3D add_cmd ("gdb-index", class_files, =09=09=09=09 save_gdb_index_command, _("\ Save a gdb-index file.\n\