From: Alan Modra Date: Fri, 27 Apr 2018 00:12:11 +0000 (+0930) Subject: -Wstringop-truncation warnings X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=602f16570454a1597c2af28af66852133432d1f2;p=deliverable%2Fbinutils-gdb.git -Wstringop-truncation warnings This patch is aimed at silencing gcc8 -Wstringop-truncation warnings. Unfortunately adding __attribute__ ((__nonstring)) doesn't work in a number of the places patched here, (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643) so if you have recent glibc headers installed you'll need to configure binutils with --disable-werror to compile using gcc8 or gcc9. include/ * ansidecl.h: Import from gcc. * coff/internal.h (struct internal_scnhdr): Add ATTRIBUTE_NONSTRING to s_name. (struct internal_syment): Add ATTRIBUTE_NONSTRING to _n_name. bfd/ * elf-linux-core.h (struct elf_external_linux_prpsinfo32_ugid32), (struct elf_external_linux_prpsinfo32_ugid16), (struct elf_external_linux_prpsinfo64_ugid32), (struct elf_external_linux_prpsinfo64_ugid16): Add ATTRIBUTE_NONSTRING to pr_fname and pr_psargs fields. Remove GCC diagnostic pragmas. Move comment to.. * elf.c (elfcore_write_prpsinfo): ..here. Indent nested preprocessor directives. * elf32-arm.c (elf32_arm_nabi_write_core_note): Add ATTRIBUTE_NONSTRING to data. * elf32-ppc.c (ppc_elf_write_core_note): Likewise. * elf32-s390.c (elf_s390_write_core_note): Likewise. * elf64-s390.c (elf_s390_write_core_note): Likewise. * elfxx-aarch64.c (_bfd_aarch64_elf_write_core_note): Likewise. * elf64-x86-64.c (elf_x86_64_write_core_note): Add GCC diagnostic pragmas. * peXXigen.c (_bfd_XXi_swap_scnhdr_out): Use strnlen to avoid false positive gcc-8 warning. gas/ * config/obj-evax.c (shorten_identifier): Use memcpy in place of strncpy. * config/obj-macho.c (obj_mach_o_make_or_get_sect): Ensure segname and sectname fields are NUL terminated. --- diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 40daa88c0e..017899875a 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,24 @@ +2018-05-04 Alan Modra + + * elf-linux-core.h (struct elf_external_linux_prpsinfo32_ugid32), + (struct elf_external_linux_prpsinfo32_ugid16), + (struct elf_external_linux_prpsinfo64_ugid32), + (struct elf_external_linux_prpsinfo64_ugid16): Add ATTRIBUTE_NONSTRING + to pr_fname and pr_psargs fields. Remove GCC diagnostic pragmas. + Move comment to.. + * elf.c (elfcore_write_prpsinfo): ..here. Indent nested preprocessor + directives. + * elf32-arm.c (elf32_arm_nabi_write_core_note): Add ATTRIBUTE_NONSTRING + to data. + * elf32-ppc.c (ppc_elf_write_core_note): Likewise. + * elf32-s390.c (elf_s390_write_core_note): Likewise. + * elf64-s390.c (elf_s390_write_core_note): Likewise. + * elfxx-aarch64.c (_bfd_aarch64_elf_write_core_note): Likewise. + * elf64-x86-64.c (elf_x86_64_write_core_note): Add GCC diagnostic + pragmas. + * peXXigen.c (_bfd_XXi_swap_scnhdr_out): Use strnlen to avoid + false positive gcc-8 warning. + 2018-05-04 Tulio Magno Quites Machado Filho * elf32-ppc.c (_bfd_elf_ppc_merge_fp_attributes): Fix the order diff --git a/bfd/cofflink.c b/bfd/cofflink.c index fb1696afff..6ca547ce5b 100644 --- a/bfd/cofflink.c +++ b/bfd/cofflink.c @@ -1839,7 +1839,7 @@ _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd) case C_FCN: if (obj_pe (input_bfd) - && strcmp (isym.n_name, ".bf") != 0 + && strncmp (isym.n_name, ".bf", sizeof isym.n_name) != 0 && isym.n_scnum > 0) { /* For PE, .lf and .ef get their value left alone, diff --git a/bfd/elf-linux-core.h b/bfd/elf-linux-core.h index fa0690320a..f0c4b2fc35 100644 --- a/bfd/elf-linux-core.h +++ b/bfd/elf-linux-core.h @@ -21,25 +21,6 @@ #ifndef ELF_LINUX_CORE_H #define ELF_LINUX_CORE_H -/* gcc-8 warns (*) on all the strncpy calls in this file about - possible string truncation. The "truncation" is not a bug. We - have an external representation of structs with fields that are not - necessarily NULL terminated and corresponding internal - representation fields that are one larger so that they can always - be NULL terminated. - gcc versions between 4.2 and 4.6 do not allow pragma control of - diagnostics inside functions, giving a hard error if you try to use - the finer control available with later versions. - gcc prior to 4.2 warns about diagnostic push and pop. - gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, - unless you also add #pragma GCC diagnostic ignored "-Wpragma". - (*) Depending on your system header files! */ - -#if GCC_VERSION >= 8000 -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wstringop-truncation" -#endif - /* External 32-bit structure for PRPSINFO. This structure is ABI-defined, thus we choose to use char arrays here in order to avoid dealing with different types in different architectures. @@ -64,8 +45,8 @@ struct elf_external_linux_prpsinfo32_ugid32 char pr_ppid[4]; char pr_pgrp[4]; char pr_sid[4]; - char pr_fname[16]; /* Filename of executable. */ - char pr_psargs[80]; /* Initial part of arg list. */ + char pr_fname[16] ATTRIBUTE_NONSTRING; /* Filename of executable. */ + char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list. */ }; /* Helper function to copy an elf_internal_linux_prpsinfo in host @@ -116,8 +97,8 @@ struct elf_external_linux_prpsinfo32_ugid16 char pr_ppid[4]; char pr_pgrp[4]; char pr_sid[4]; - char pr_fname[16]; /* Filename of executable. */ - char pr_psargs[80]; /* Initial part of arg list. */ + char pr_fname[16] ATTRIBUTE_NONSTRING; /* Filename of executable. */ + char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list. */ }; /* Helper function to copy an elf_internal_linux_prpsinfo in host @@ -169,8 +150,8 @@ struct elf_external_linux_prpsinfo64_ugid32 char pr_ppid[4]; char pr_pgrp[4]; char pr_sid[4]; - char pr_fname[16]; /* Filename of executable. */ - char pr_psargs[80]; /* Initial part of arg list. */ + char pr_fname[16] ATTRIBUTE_NONSTRING; /* Filename of executable. */ + char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list. */ }; /* Helper function to copy an elf_internal_linux_prpsinfo in host @@ -222,8 +203,8 @@ struct elf_external_linux_prpsinfo64_ugid16 char pr_ppid[4]; char pr_pgrp[4]; char pr_sid[4]; - char pr_fname[16]; /* Filename of executable. */ - char pr_psargs[80]; /* Initial part of arg list. */ + char pr_fname[16] ATTRIBUTE_NONSTRING; /* Filename of executable. */ + char pr_psargs[80] ATTRIBUTE_NONSTRING; /* Initial part of arg list. */ }; /* Helper function to copy an elf_internal_linux_prpsinfo in host @@ -250,8 +231,4 @@ swap_linux_prpsinfo64_ugid16_out strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs)); } -#if GCC_VERSION >= 8000 -#pragma GCC diagnostic pop -#endif - #endif diff --git a/bfd/elf.c b/bfd/elf.c index bd9ffccee6..21bc4e7ac2 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -10508,9 +10508,22 @@ elfcore_write_note (bfd *abfd, return buf; } +/* gcc-8 warns (*) on all the strncpy calls in this function about + possible string truncation. The "truncation" is not a bug. We + have an external representation of structs with fields that are not + necessarily NULL terminated and corresponding internal + representation fields that are one larger so that they can always + be NULL terminated. + gcc versions between 4.2 and 4.6 do not allow pragma control of + diagnostics inside functions, giving a hard error if you try to use + the finer control available with later versions. + gcc prior to 4.2 warns about diagnostic push and pop. + gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, + unless you also add #pragma GCC diagnostic ignored "-Wpragma". + (*) Depending on your system header files! */ #if GCC_VERSION >= 8000 -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wstringop-truncation" +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wstringop-truncation" #endif char * elfcore_write_prpsinfo (bfd *abfd, @@ -10531,16 +10544,16 @@ elfcore_write_prpsinfo (bfd *abfd, } #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) -#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) +# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) if (bed->s->elfclass == ELFCLASS32) { -#if defined (HAVE_PSINFO32_T) +# if defined (HAVE_PSINFO32_T) psinfo32_t data; int note_type = NT_PSINFO; -#else +# else prpsinfo32_t data; int note_type = NT_PRPSINFO; -#endif +# endif memset (&data, 0, sizeof (data)); strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); @@ -10549,15 +10562,15 @@ elfcore_write_prpsinfo (bfd *abfd, "CORE", note_type, &data, sizeof (data)); } else -#endif +# endif { -#if defined (HAVE_PSINFO_T) +# if defined (HAVE_PSINFO_T) psinfo_t data; int note_type = NT_PSINFO; -#else +# else prpsinfo_t data; int note_type = NT_PRPSINFO; -#endif +# endif memset (&data, 0, sizeof (data)); strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); @@ -10571,7 +10584,7 @@ elfcore_write_prpsinfo (bfd *abfd, return NULL; } #if GCC_VERSION >= 8000 -#pragma GCC diagnostic pop +# pragma GCC diagnostic pop #endif char * diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c index a42db44b2f..5a3b58ff0d 100644 --- a/bfd/elf32-arm.c +++ b/bfd/elf32-arm.c @@ -2168,7 +2168,7 @@ elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz, case NT_PRPSINFO: { - char data[124]; + char data[124] ATTRIBUTE_NONSTRING; va_list ap; va_start (ap, note_type); diff --git a/bfd/elf32-ppc.c b/bfd/elf32-ppc.c index 7f4ed01f20..462c8af311 100644 --- a/bfd/elf32-ppc.c +++ b/bfd/elf32-ppc.c @@ -2405,7 +2405,7 @@ ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...) case NT_PRPSINFO: { - char data[128]; + char data[128] ATTRIBUTE_NONSTRING; va_list ap; va_start (ap, note_type); diff --git a/bfd/elf32-s390.c b/bfd/elf32-s390.c index 18010c0b45..b3603bd865 100644 --- a/bfd/elf32-s390.c +++ b/bfd/elf32-s390.c @@ -3942,7 +3942,7 @@ elf_s390_write_core_note (bfd *abfd, char *buf, int *bufsiz, case NT_PRPSINFO: { - char data[124] = { 0 }; + char data[124] ATTRIBUTE_NONSTRING = { 0 }; const char *fname, *psargs; va_start (ap, note_type); diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c index 2d02e82c54..bfa02340ca 100644 --- a/bfd/elf64-s390.c +++ b/bfd/elf64-s390.c @@ -3751,7 +3751,7 @@ elf_s390_write_core_note (bfd *abfd, char *buf, int *bufsiz, case NT_PRPSINFO: { - char data[136] = { 0 }; + char data[136] ATTRIBUTE_NONSTRING = { 0 }; const char *fname, *psargs; va_start (ap, note_type); diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c index c1237bb2e4..a0ebd2cf69 100644 --- a/bfd/elf64-x86-64.c +++ b/bfd/elf64-x86-64.c @@ -439,6 +439,10 @@ elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) } #ifdef CORE_HEADER +# if GCC_VERSION >= 8000 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wstringop-truncation" +# endif static char * elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...) @@ -524,6 +528,9 @@ elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz, } /* NOTREACHED */ } +# if GCC_VERSION >= 8000 +# pragma GCC diagnostic pop +# endif #endif /* Functions for the x86-64 ELF linker. */ diff --git a/bfd/elfxx-aarch64.c b/bfd/elfxx-aarch64.c index 08e7cd9684..45a732db2b 100644 --- a/bfd/elfxx-aarch64.c +++ b/bfd/elfxx-aarch64.c @@ -653,7 +653,7 @@ _bfd_aarch64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_ty case NT_PRPSINFO: { - char data[136]; + char data[136] ATTRIBUTE_NONSTRING; va_list ap; va_start (ap, note_type); diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c index 32ce195a00..a62d126aff 100644 --- a/bfd/peXXigen.c +++ b/bfd/peXXigen.c @@ -1051,9 +1051,10 @@ _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out) by ld --omagic, or by obcopy --writable-text. */ for (p = known_sections; p->section_name; p++) - if (strcmp (scnhdr_int->s_name, p->section_name) == 0) + if (strncmp (scnhdr_int->s_name, p->section_name, + sizeof scnhdr_int->s_name) == 0) { - if (strcmp (scnhdr_int->s_name, ".text") + if (strncmp (scnhdr_int->s_name, ".text", sizeof scnhdr_int->s_name) || (bfd_get_file_flags (abfd) & WP_TEXT)) scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE; scnhdr_int->s_flags |= p->must_have; @@ -1066,7 +1067,7 @@ _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out) if (coff_data (abfd)->link_info && ! bfd_link_relocatable (coff_data (abfd)->link_info) && ! bfd_link_pic (coff_data (abfd)->link_info) - && strcmp (scnhdr_int->s_name, ".text") == 0) + && strncmp (scnhdr_int->s_name, ".text", sizeof scnhdr_int->s_name) == 0) { /* By inference from looking at MS output, the 32 bit field which is the combination of the number_of_relocs and diff --git a/gas/ChangeLog b/gas/ChangeLog index d7575ad202..a46492ba9a 100644 --- a/gas/ChangeLog +++ b/gas/ChangeLog @@ -1,3 +1,10 @@ +2018-05-04 Alan Modra + + * config/obj-evax.c (shorten_identifier): Use memcpy in place + of strncpy. + * config/obj-macho.c (obj_mach_o_make_or_get_sect): Ensure + segname and sectname fields are NUL terminated. + 2018-05-01 Nick Clifton * po/es.po: Updated Spanish translation. diff --git a/gas/config/obj-evax.c b/gas/config/obj-evax.c index 642025f2ff..07a7067ebd 100644 --- a/gas/config/obj-evax.c +++ b/gas/config/obj-evax.c @@ -470,13 +470,13 @@ shorten_identifier (char *name) newname [MAX_LABEL_LENGTH] = 0; /* Now append the suffix of the original identifier, if any. */ if (suffix_length) - strncpy (newname + MAX_LABEL_LENGTH - suffix_length, - name + len - suffix_length, - suffix_length); - strncpy (newname + final_len, "_h", 2); - strncpy (newname + final_len + 2 , crc_chars, 5); - strncpy (newname + final_len + 2 + 5, encode_16 (len), 3); - strncpy (newname + final_len + 2 + 5 + 3, encode_16 (sum), 3); + strncpy (newname + MAX_LABEL_LENGTH - suffix_length, + name + len - suffix_length, + suffix_length); + memcpy (newname + final_len, "_h", 2); + memcpy (newname + final_len + 2 , crc_chars, 5); + memcpy (newname + final_len + 2 + 5, encode_16 (len), 3); + memcpy (newname + final_len + 2 + 5 + 3, encode_16 (sum), 3); if (!is_truncated_identifier (newname)) abort (); return newname; diff --git a/gas/config/obj-macho.c b/gas/config/obj-macho.c index 4911907c9b..3ee98b9e5a 100644 --- a/gas/config/obj-macho.c +++ b/gas/config/obj-macho.c @@ -250,8 +250,10 @@ obj_mach_o_make_or_get_sect (char * segname, char * sectname, bfd_section_name (stdoutput, sec), bfd_errmsg (bfd_get_error ())); - strncpy (msect->segname, segname, sizeof (msect->segname)); - strncpy (msect->sectname, sectname, sizeof (msect->sectname)); + strncpy (msect->segname, segname, BFD_MACH_O_SEGNAME_SIZE); + msect->segname[BFD_MACH_O_SEGNAME_SIZE] = 0; + strncpy (msect->sectname, sectname, BFD_MACH_O_SECTNAME_SIZE); + msect->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0; msect->align = secalign; msect->flags = sectype | secattr; diff --git a/include/ChangeLog b/include/ChangeLog index cf0b626812..5c9913b7e8 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,3 +1,10 @@ +2018-05-04 Alan Modra + + * ansidecl.h: Import from gcc. + * coff/internal.h (struct internal_scnhdr): Add ATTRIBUTE_NONSTRING + to s_name. + (struct internal_syment): Add ATTRIBUTE_NONSTRING to _n_name. + 2018-04-30 Francois H. Theron * dis-asm.h: Added print_nfp_disassembler_options prototype. diff --git a/include/ansidecl.h b/include/ansidecl.h index c11daffedb..406473ca9e 100644 --- a/include/ansidecl.h +++ b/include/ansidecl.h @@ -283,6 +283,15 @@ So instead we use the macro below and test it against specific values. */ # endif /* GNUC >= 4.9 */ #endif /* ATTRIBUTE_NO_SANITIZE_UNDEFINED */ +/* Attribute 'nonstring' was valid as of gcc 8. */ +#ifndef ATTRIBUTE_NONSTRING +# if GCC_VERSION >= 8000 +# define ATTRIBUTE_NONSTRING __attribute__ ((__nonstring__)) +# else +# define ATTRIBUTE_NONSTRING +# endif +#endif + /* We use __extension__ in some places to suppress -pedantic warnings about GCC extensions. This feature didn't work properly before gcc 2.8. */ diff --git a/include/coff/internal.h b/include/coff/internal.h index 1879ddc0a0..a8efedb7c4 100644 --- a/include/coff/internal.h +++ b/include/coff/internal.h @@ -393,7 +393,7 @@ struct internal_aouthdr struct internal_scnhdr { - char s_name[SCNNMLEN]; /* section name */ + char s_name[SCNNMLEN] ATTRIBUTE_NONSTRING; /* section name */ /* Physical address, aliased s_nlib. In the pei format, this field is the virtual section size @@ -466,7 +466,7 @@ struct internal_syment { union { - char _n_name[SYMNMLEN]; /* old COFF version */ + char _n_name[SYMNMLEN] ATTRIBUTE_NONSTRING; /* old COFF version */ struct { bfd_hostptr_t _n_zeroes; /* new == 0 */