Silence gcc-8 warnings

Alan Modra amodra@gmail.com
Fri Apr 27 00:08:00 GMT 2018


On Thu, Apr 26, 2018 at 03:18:22PM +0100, Pedro Alves wrote:
> On 04/24/2018 12:58 AM, Alan Modra wrote:
> >> So, the strncpy was:
> >>
> >>  __builtin_strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname))
> >>
> >> sizeof(to->prfname) is 16, but sizeof(from->prfname) is 17, so it is 
> >> indeed conceivable that the from string has 16 characters plus null 
> >> terminator, which would not fit terminated into to->pr_fname, and this is 
> >> what is warned about.  I think this is a genuine bug in bfd (even though 
> >> possibly a harmless one with non-fuzzed prpsinfo structs in core files).
> > No, it is not a bug.  We have internal and external structs, and a
> > field in the external struct that does not need to be NULL
> > terminated.  For convenience, the internal struct field is one larger
> > so that the internal representation is always NULL terminated.  When
> > copying from internal to external representation there is no
> > possibility that we lose anything but the NULL.
> 
> Did you try marking the external prfname with attribute notstring?

No, I hadn't.  I wasn't aware of that attribute.  Using it works for
elf-linux-core.h but we'll still need -Wno-stringop-truncation for
elf.c:elfcore_write_prpsinfo where binutils/gdb is using a struct
defined in a system header.

I'll see about getting ATTRIBUTE_NONSTRING into include/ansidecl.h.

> Seems to have been invented for cases like this:
> 
> https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
> 
> ~~~~~~~~~~~~~~~~~~~~~
> The nonstring variable attribute specifies that an object or member declaration
> with type array of char, signed char, or unsigned char, or pointer to such a type
> is intended to store character arrays that do not necessarily contain a
> terminating NUL. This is useful in detecting uses of such arrays or
> pointers with functions that expect NUL-terminated strings, and to avoid
> warnings when such an array or pointer is used as an argument to a
> bounded string manipulation function such as strncpy.
> ~~~~~~~~~~~~~~~~~~~~~
> 
> Thanks,
> Pedro Alves

-- 
Alan Modra
Australia Development Lab, IBM



More information about the Binutils mailing list