[PATCH] rtems-fdt / shell - Fix string truncation warning

Frank Kühndel frank.kuehndel at embedded-brains.de
Thu Oct 15 13:06:13 UTC 2020

On 10/15/20 2:09 PM, Sebastian Huber wrote:
>> From: Frank Kühndel<frank.kuehndel at embedded-brains.de>
>> The compiler warning was:
>> ../../../cpukit/libmisc/rtems-fdt/rtems-fdt.c:267:5: warning:
>> 'strncpy' specified bound depends on the length of the source argument
>>    267 |     strncpy(path, name, namelen);
>>        |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> It turns out that the `strncpy()` nor the buffer `path` is needed when
>> one uses `strncmp()` instead of `strcmp()`. This needs some change to
>> the algorithm but has the advantage that `name` is never truncated
>> to the size of the buffer `path`.
> Does it help to use bsearch() to simplify this code further?

Nice! I did not even know (or remember about) bsearch().

No, it wont help. bsearch() works on "similar" objects (e.g. two
strings). The function I modified searches a string in an array of
'rtems_fdt_index_entry' objects. To make that work, one must create a
dummy 'rtems_fdt_index_entry' containing that string.

Moreover, there remains the problem that the 'name' string has an
artificially restricted length which must be convoyed to the 'compar()'
function used by 'bseach()'.

Finally, rtems-fdt.c is - most likely - dead code. So, I would end up
spending my time on brushing up dead code.


More information about the devel mailing list