[gdb/build] Fix Wstringop-truncation in coff_getfilename
authorTom de Vries <tdevries@suse.de>
Wed, 26 Jul 2023 15:06:23 +0000 (17:06 +0200)
committerTom de Vries <tdevries@suse.de>
Wed, 26 Jul 2023 15:06:23 +0000 (17:06 +0200)
When building gdb with -O2 -fsanitize-threads, I ran into
a Werror=stringop-truncation.

The problem is here in coff_getfilename in coffread.c:
...
      strncpy (buffer, aux_entry->x_file.x_n.x_fname, FILNMLEN);
      buffer[FILNMLEN] = '\0';
...

The constant FILNMLEN is expected to designate the size of
aux_entry->x_file.x_n.x_fname, but that's no longer the case since commit
60ebc257517 ("Fixes a buffer overflow when compiling assembler for the MinGW
targets.").

Fix this by using "sizeof (aux_entry->x_file.x_n.x_fname)" instead.

Likewise in xcoffread.c.

Tested on x86_64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
PR build/30669
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30669

gdb/coffread.c
gdb/xcoffread.c

index 33fb2ba1fcafcccee52f73ea664cd0401386c1bd..6ec341c61c2a5f890940b29f12d69131bcc336ee 100644 (file)
@@ -1371,8 +1371,9 @@ coff_getfilename (union internal_auxent *aux_entry)
     }
   else
     {
-      strncpy (buffer, aux_entry->x_file.x_n.x_fname, FILNMLEN);
-      buffer[FILNMLEN] = '\0';
+      size_t x_fname_len = sizeof (aux_entry->x_file.x_n.x_fname);
+      strncpy (buffer, aux_entry->x_file.x_n.x_fname, x_fname_len);
+      buffer[x_fname_len] = '\0';
     }
   result = buffer;
 
index 1538d1c823d366acd0b0dc9219d9f887f05380cf..8930cf1bc35f8ef30ea63d62f410a7fb9116fda7 100644 (file)
@@ -1598,8 +1598,9 @@ coff_getfilename (union internal_auxent *aux_entry, struct objfile *objfile)
                     + aux_entry->x_file.x_n.x_n.x_offset));
   else
     {
-      strncpy (buffer, aux_entry->x_file.x_n.x_fname, FILNMLEN);
-      buffer[FILNMLEN] = '\0';
+      size_t x_fname_len = sizeof (aux_entry->x_file.x_n.x_fname);
+      strncpy (buffer, aux_entry->x_file.x_n.x_fname, x_fname_len);
+      buffer[x_fname_len] = '\0';
     }
   return (buffer);
 }