[PATCH 2/4] bsps/shared/ofw: Use memcpy instead of strncpy

Niteesh G. S. niteesh.gs at gmail.com
Sat Feb 6 05:05:16 UTC 2021


On Fri, Feb 5, 2021 at 11:52 PM Gedare Bloom <gedare at rtems.org> wrote:

>
>
> On Fri, Feb 5, 2021 at 10:41 AM G S Niteesh Babu <niteesh.gs at gmail.com>
> wrote:
>
>> Changed rtems_ofw_get_prop to use memcpy instead of strncpy
>> ---
>>  bsps/shared/ofw/ofw.c | 10 +++++++++-
>>  1 file changed, 9 insertions(+), 1 deletion(-)
>>
>> diff --git a/bsps/shared/ofw/ofw.c b/bsps/shared/ofw/ofw.c
>> index fa94bfbf05..9dec310247 100644
>> --- a/bsps/shared/ofw/ofw.c
>> +++ b/bsps/shared/ofw/ofw.c
>> @@ -198,7 +198,15 @@ ssize_t rtems_ofw_get_prop(
>>
>>    if (prop == NULL && strcmp(propname, "name") == 0) {
>>      prop = fdt_get_name(fdtp, offset, &len);
>> -    strncpy(buf, prop, bufsize);
>> +
>> +    bufsize = MIN(len, bufsize - 1);
>>
> ok, reserving 1 byte for the \0. It could be worth adding a comment here
> to that effect
>
Will do.

> +    memcpy(buf, prop, bufsize);
>> +
>> +    /* Null terminate the buffer */
>> +    *((char *)buf + bufsize) = 0;
>>
> that gets written here. looks fine to me.
>
>
>> +
>> +    /* Return the length of the name including the null byte */
>> +    /* This is the behaviour in libBSD ofw_fdt_getprop */
>>      return len + 1;
>>
> shouldn't it be bufsize+1? if it got truncated by the MIN?
>

We have to return the size of the value instead of the amount copied.
https://www.gsp.com/cgi-bin/man.cgi?section=9&topic=OF_GETPROP

   }
>>
>> --
>> 2.17.1
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20210206/c43de425/attachment-0001.html>


More information about the devel mailing list