[parted-devel] [PATCH 3/3] libparted/labels/dvh.c: Replace strncpy()

Shin'ichiro Kawasaki kawasaki at juno.dti.ne.jp
Sun Jun 30 13:49:44 BST 2019


GCC 9 fails to compile libparted/labels/dvh.c with an error:

  CC       dvh.lo
In file included from /usr/include/string.h:494,
                 from ../../lib/string.h:27,
                 from ../../include/parted/parted.h:44,
                 from dvh.c:20:
In function 'strncpy',
    inlined from '_generate_boot_file' at dvh.c:433:2,
    inlined from 'dvh_write' at dvh.c:487:4:
/usr/include/bits/string_fortified.h:106:10: error: '__builtin_strncpy' output may be truncated copying 8 bytes from a string of length 8 [-Werror=stringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[4]: *** [Makefile:1250: dvh.lo] Error 1

The error happens for a strncpy() call with copy length VDNAMESIZE. Copy
destination array has exact VDNAMESIZE bytes, then GCC warns that the
copied string may not be null-terminated.

To avoid the error, use strcpy() or memcpy() in place of strncpy().
Check strlen() to choose one of the appropriate copy functions.

Signed-off-by: Shin'ichiro Kawasaki <kawasaki at juno.dti.ne.jp>
---
 libparted/labels/dvh.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/libparted/labels/dvh.c b/libparted/labels/dvh.c
index 158b0b3..d542fe5 100644
--- a/libparted/labels/dvh.c
+++ b/libparted/labels/dvh.c
@@ -429,8 +429,11 @@ _generate_boot_file (PedPartition* part, struct volume_directory* vd)
 	vd->vd_nbytes = PED_CPU_TO_BE32 (dvh_part_data->real_file_size);
 	vd->vd_lbn = PED_CPU_TO_BE32 (part->geom.start);
 
-	memset (vd->vd_name, 0, VDNAMESIZE);
-	strncpy (vd->vd_name, dvh_part_data->name, VDNAMESIZE);
+	if (strlen(dvh_part_data->name) < VDNAMESIZE) {
+		memset (vd->vd_name, 0, VDNAMESIZE);
+		strcpy(vd->vd_name, dvh_part_data->name);
+	} else
+		memcpy (vd->vd_name, dvh_part_data->name, VDNAMESIZE);
 }
 
 static int
-- 
2.22.0




More information about the parted-devel mailing list