[SLOF] GCC compiler warning in create_crc.c

Segher Boessenkool segher at kernel.crashing.org
Wed Jul 18 01:24:32 AEST 2018


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?

> Simply use
> "-Wno-stringop-truncation" for compiling that file?

That works, too; but you do not want to disable warnings, in general.

> ... 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).

> Or should we rework the code? ... that's certainly
> possible, but in this case, it sounds so unnecessary...
> 
> I really don't have a suggestion for a really nice clean solution
> here... anybody else got a good idea?

I'd just use memcpy, it is closer to what you really want to express
anyway (you're not copying a string, just a hunk of memory (a string ends
in a zero byte)).


Segher


More information about the SLOF mailing list