[SLOF] GCC compiler warning in create_crc.c

Segher Boessenkool segher at kernel.crashing.org
Wed Jul 18 04:14:58 AEST 2018


On Tue, Jul 17, 2018 at 06:04:38PM +0200, Thomas Huth wrote:
> On 17.07.2018 17:24, Segher Boessenkool wrote:
> > 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().

But the size is constant here.

You should never have a strlen in sight in these cases.

Likely, you want a static initialisation *anyway*, and all is moot then.

For the very few cases where there is no better way to avoid warnings
(i.e. you cannot simply write clearer code), there is attribute((nonstring)).


Segher


More information about the SLOF mailing list