From 2774f2dad5f05e68771c07df6ab0fb23baa2118e Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Thu, 31 Aug 2023 09:37:44 +0200 Subject: [gdb/symtab] Factor out type::{alloc_fields,copy_fields} After finding this code in buildsym_compunit::finish_block_internal: ... ftype->set_fields ((struct field *) TYPE_ALLOC (ftype, nparams * sizeof (struct field))); ... and fixing PR30810 by using TYPE_ZALLOC, I wondered if there were more locations that needed fixing. I decided to make things easier to spot by factoring out a new function alloc_fields: ... /* Allocate the fields array of this type, with NFIELDS elements. If INIT, zero-initialize the allocated memory. */ void type::alloc_fields (unsigned int nfields, bool init = true); ... where: - a regular use would be "alloc_fields (nfields)", and - an exceptional use that needed no initialization would be "alloc_fields (nfields, false)". Pretty soon I discovered that most of the latter cases are due to initialization by memcpy, so I added two variants of copy_fields as well. After this rewrite there are 8 uses of set_fields left: ... gdb/coffread.c: type->set_fields (nullptr); gdb/coffread.c: type->set_fields (nullptr); gdb/coffread.c: type->set_fields (nullptr); gdb/eval.c: type->set_fields gdb/gdbtypes.c: type->set_fields (args); gdb/gdbtypes.c: t->set_fields (XRESIZEVEC (struct field, t->fields (), gdb/dwarf2/read.c: type->set_fields (new_fields); gdb/dwarf2/read.c: sub_type->set_fields (sub_type->fields () + 1); ... These fall into the following categories: - set to nullptr (coffread.c), - type not owned by objfile or gdbarch (eval.c), and - modifying an existing fields array, like adding an element at the end or dropping an element at the start (the rest). Tested on x86_64-linux. --- gdb/windows-tdep.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'gdb/windows-tdep.c') diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c index bc19270..0414aea 100644 --- a/gdb/windows-tdep.c +++ b/gdb/windows-tdep.c @@ -745,9 +745,7 @@ create_enum (struct gdbarch *gdbarch, int bit, const char *name, int i; type = type_allocator (gdbarch).new_type (TYPE_CODE_ENUM, bit, name); - type->set_num_fields (count); - type->set_fields - ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * count)); + type->alloc_fields (count); type->set_is_unsigned (true); for (i = 0; i < count; i++) -- cgit v1.1