[SLOF] GCC compiler warning in create_crc.c
Thomas Huth
thuth at redhat.com
Wed Jul 18 02:04:38 AEST 2018
On 17.07.2018 17:24, Segher Boessenkool wrote:
> Hi!
>
> On Tue, Jul 17, 2018 at 01:33:42PM +0200, Thomas Huth wrote:
>> when compiling SLOF with GCC 8.1, I currently get these compiler warnings:
>>
>> create_crc.c: In function ‘createHeaderImage’:
>> create_crc.c:110:2: warning: ‘strncpy’ output truncated before
>> terminating nul copying 8 bytes from a string of the same length
>> [-Wstringop-truncation]
>> strncpy(uHeader.stHeader.magic, FLASHFS_MAGIC, 8);
>> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> create_crc.c:86:3: warning: ‘strncpy’ specified bound 16 equals
>> destination size [-Wstringop-truncation]
>> strncpy(uHeader.stHeader.version, pcVersion, 16);
>> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> create_crc.c:84:3: warning: ‘strncpy’ specified bound 16 equals
>> destination size [-Wstringop-truncation]
>> strncpy(uHeader.stHeader.version, pcVersion, 16);
>> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> As far as I can see, the code is prefectly fine, since the fields in the
>> header do not have to be NUL-terminated here. Still, it's annoying to
>> see a compiler warning here every time you re-compile SLOF.
>
> Yes, this is a very annoying warning; it warns for *correct* usage of
> strncpy very often (I don't see it warn for incorrect usage much, I don't
> work with that kind of code, maybe that happens a lot too though ;-) )
>
>> Does anybody have a good suggestion how to silence these?
>
> Use memcpy instead?
The source strings could also be less bytes, so you'd end up with
something like this:
memset(dest, 0, sizeof(dst);
memcpy(dest, src,
strlen(src) < sizeof(dest) ? strlen(src) : sizeof(dst));
That looks rather cumbersome compared to the strncpy().
>> Simply use
>> "-Wno-stringop-truncation" for compiling that file?
>
> That works, too; but you do not want to disable warnings, in general.
Sure, that's why I wrote "for that file" ... I don't want to disable
that in general either.
>> ... but then we
>> first have to check whether the compiler supports that flag, and that's
>> also kind of ugly.
>
> All -Wno-* are ignored (unless there is some other error, then they are
> reported).
Ah, that's good to know, thanks! So far I assumed that older version of
GCC might complain about a "-Wno-stringop-truncation", but I just tried,
and you're right, at least gcc 4.8 simply ignores it.
Thomas
More information about the SLOF
mailing list