[PATCH] avoid GCC 10 warning -Wstringop-truncation

Joel Sherrill joel at rtems.org
Thu Jul 30 16:37:38 UTC 2020


My recollection is that memcpy is undefined on overlapping memory regions.
You should use memmove.

A bigger question is why do these overlap and the code still thinks the
copy is
needed.

--joel

On Thu, Jul 30, 2020 at 10:37 AM Gedare Bloom <gedare at rtems.org> wrote:

> This looks good to me. memcpy followed by explicit delimiter
> assignment is better than strncpy. however, note below:
>
> On Thu, Jul 30, 2020 at 5:36 AM Aschref Ben-Thabet
> <aschref.ben-thabet at embedded-brains.de> wrote:
> >
> > From: Aschref Ben Thabet <aschref.ben-thabet at embedded-brains.de>
> >
> > GCC 10 warns about an overlapping using strncpy.
> > -> Replace some calls of strncpy with a memcpy to avoid this issue.
> > ---
> >  cpukit/libblock/src/bdpart-mount.c   | 4 ++--
> >  testsuites/psxtests/psxndbm01/init.c | 2 +-
> >  2 files changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/cpukit/libblock/src/bdpart-mount.c
> b/cpukit/libblock/src/bdpart-mount.c
> > index cfc08ead30..f689b18ebb 100644
> > --- a/cpukit/libblock/src/bdpart-mount.c
> > +++ b/cpukit/libblock/src/bdpart-mount.c
> > @@ -54,7 +54,7 @@ rtems_status_code rtems_bdpart_mount(
> >    if (logical_disk_name == NULL) {
> >      return RTEMS_NO_MEMORY;
> >    }
> > -  strncpy( logical_disk_name, disk_name, disk_name_size);
> > +  memcpy( logical_disk_name, disk_name, disk_name_size);
> >
> is it guaranteed to have a NUL delimiter in disk_name up to
> disk_name_size? (I don't think it does.)
>
> It should be safer to follow this with:
> logical_disk_name[disk_name_size] = '\0';
>
> >    /* Get disk file name */
> >    if (disk_file_name != NULL) {
> > @@ -148,7 +148,7 @@ rtems_status_code rtems_bdpart_unmount(
> >      esc = RTEMS_NO_MEMORY;
> >      goto cleanup;
> >    }
> > -  strncpy( mount_point, mount_base, mount_base_size);
> > +  memcpy( mount_point, mount_base, mount_base_size);
> >    mount_point [mount_base_size] = '/';
> >    strncpy( mount_point + mount_base_size + 1, disk_file_name,
> disk_file_name_size);
>
> I guess this one doesn't give a warning? Would it still make any sense
> to replace it?
>
> >
> > diff --git a/testsuites/psxtests/psxndbm01/init.c
> b/testsuites/psxtests/psxndbm01/init.c
> > index a13afa7315..b524aff0df 100644
> > --- a/testsuites/psxtests/psxndbm01/init.c
> > +++ b/testsuites/psxtests/psxndbm01/init.c
> > @@ -218,7 +218,7 @@ rtems_task Init(rtems_task_argument ignored)
> >
> >    puts( "Fetch non-existing record and confirm error." );
> >    test_strings = (char*)malloc(6);
> > -  strncpy( test_strings, "Hello", 5 );
> > +  memcpy( test_strings, "Hello", 5 );
> >
> >    test_strings[5] = '\0';
> >
> > --
> > 2.26.2
> >
> > _______________________________________________
> > devel mailing list
> > devel at rtems.org
> > http://lists.rtems.org/mailman/listinfo/devel
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20200730/ac8b0563/attachment-0001.html>


More information about the devel mailing list