[SLOF] GCC compiler warning in create_crc.c

Thomas Huth thuth at redhat.com
Wed Jul 18 22:00:30 AEST 2018


On 17.07.2018 20:14, Segher Boessenkool wrote:
> 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.

Static initialisation is a great idea here, and it works great for the
FLASHFS_MAGIC! However, this does not work for the fields that are
intialized dynamically with a string from getenv()...

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

I guess I'll simply suggest a patch with memcpy for the dynamic lines
... it's also not the nicest solution, but I think it's still better
than the other options that we've discussed.

 Thomas


More information about the SLOF mailing list