[rtems commit] libblock: Fix string truncation warning

Joel Sherrill joel at rtems.org
Sat Oct 10 23:47:17 UTC 2020


On Sat, Oct 10, 2020, 8:16 AM Sebastian Huber <sebh at rtems.org> wrote:

> Module:    rtems
> Branch:    master
> Commit:    bc7ac71f8aaaff0c73469246c526741ec5b181ed
> Changeset:
> http://git.rtems.org/rtems/commit/?id=bc7ac71f8aaaff0c73469246c526741ec5b181ed
>
> Author:    Frank Kühndel <frank.kuehndel at embedded-brains.de>
> Date:      Mon Oct  5 16:37:23 2020 +0200
>
> libblock: Fix string truncation warning
>
> This patch does not only fix the compiler warning below. memcpy() is the
> better function at this place as the terminating NUL character is never
> copied here. Instead more characters will be appended to the
> 'logical_disk_name' later on.
>
> ../../../cpukit/libblock/src/bdpart-register.c:41:5:
> warning: 'strncpy' output truncated before terminating nul copying
> as many bytes from a string as its length [-Wstringop-truncation]
>
> ---
>
>  cpukit/libblock/src/bdpart-register.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/cpukit/libblock/src/bdpart-register.c
> b/cpukit/libblock/src/bdpart-register.c
> index 9956e61..8a1de61 100644
> --- a/cpukit/libblock/src/bdpart-register.c
> +++ b/cpukit/libblock/src/bdpart-register.c
> @@ -38,7 +38,7 @@ static char *create_logical_disk_name( const char
> *disk_name, char **marker)
>    char *logical_disk_name = malloc( disk_name_size +
> RTEMS_BDPART_NUMBER_SIZE);
>
>    if (logical_disk_name != NULL) {
> -    strncpy( logical_disk_name, disk_name, disk_name_size);
> +    memcpy( logical_disk_name, disk_name, disk_name_size);
>

Could we have a comment in the code explaining why this is safe?

Please do that for everywhere memcpy is being inserted. Next time someone
looks at this code, it will not be obvious.

     *marker = logical_disk_name + disk_name_size;
>    }
>
>
> _______________________________________________
> vc mailing list
> vc at rtems.org
> http://lists.rtems.org/mailman/listinfo/vc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20201010/9bb4a6cc/attachment.html>


More information about the devel mailing list