From a227f3b9819070044ee0968d646d7e558d65c58a Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Mon, 27 Apr 2020 11:47:26 -0400 Subject: [PATCH 1/2] Fix #435, Changes for string ops warnings Resize buffers and tweak string copies to avoid truncation warnings in GCC 9. Note the code was generally OK (handled truncation) but the new compiler still generates warnings even with correct usage of certain functions. In the UT_os_printf macro, be sure to "use" the return value of the snprintf() call, which avoids warnings about truncation. --- src/os/shared/inc/os-shared-filesys.h | 4 ++-- src/os/shared/src/osapi-filesys.c | 11 +++++++---- src/unit-tests/inc/ut_os_support.h | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/os/shared/inc/os-shared-filesys.h b/src/os/shared/inc/os-shared-filesys.h index f9dd56b6e..d578e57bf 100644 --- a/src/os/shared/inc/os-shared-filesys.h +++ b/src/os/shared/inc/os-shared-filesys.h @@ -96,8 +96,8 @@ typedef struct typedef struct { char device_name[OS_MAX_API_NAME]; /**< The name of the underlying block device, if applicable */ - char volume_name[OS_MAX_API_NAME]; - char system_mountpt[OS_MAX_PATH_LEN]; /**< The name/prefix where the contents are accessible in the host operating system */ + char volume_name[OS_FS_VOL_NAME_LEN]; + char system_mountpt[OS_MAX_LOCAL_PATH_LEN]; /**< The name/prefix where the contents are accessible in the host operating system */ char virtual_mountpt[OS_MAX_PATH_LEN]; /**< The name/prefix in the OSAL Virtual File system exposed to applications */ char *address; uint32 blocksize; diff --git a/src/os/shared/src/osapi-filesys.c b/src/os/shared/src/osapi-filesys.c index 71022321d..c2638ad70 100644 --- a/src/os/shared/src/osapi-filesys.c +++ b/src/os/shared/src/osapi-filesys.c @@ -116,18 +116,21 @@ int32 OS_FileSys_InitLocalFromVolTable(OS_filesys_internal_record_t *local, cons strcmp(Vol->DeviceName,"unused") != 0) { strncpy(local->volume_name, Vol->VolumeName, sizeof(local->volume_name)-1); + local->volume_name[sizeof(local->volume_name)-1] = 0; } if (isgraph((int)Vol->PhysDevName[0]) && strcmp(Vol->PhysDevName,"unused") != 0) { strncpy(local->system_mountpt, Vol->PhysDevName, sizeof(local->system_mountpt)-1); + local->system_mountpt[sizeof(local->system_mountpt)-1] = 0; } if (isgraph((int)Vol->MountPoint[0]) && strcmp(Vol->MountPoint,"unused") != 0) { strncpy(local->virtual_mountpt, Vol->MountPoint, sizeof(local->virtual_mountpt)-1); + local->virtual_mountpt[sizeof(local->virtual_mountpt)-1] = 0; } /* @@ -471,10 +474,10 @@ int32 OS_FileSysAddFixedMap(uint32 *filesys_id, const char *phys_path, const cha memset(local, 0, sizeof(*local)); global->name_entry = local->device_name; - strcpy(local->device_name, dev_name); - strcpy(local->volume_name, dev_name); - strcpy(local->system_mountpt, phys_path); - strcpy(local->virtual_mountpt, virt_path); + strncpy(local->device_name, dev_name, sizeof(local->device_name)-1); + strncpy(local->volume_name, dev_name, sizeof(local->volume_name)-1); + strncpy(local->system_mountpt, phys_path, sizeof(local->system_mountpt)-1); + strncpy(local->virtual_mountpt, virt_path, sizeof(local->virtual_mountpt)-1); /* * mark the entry that it is a fixed disk diff --git a/src/unit-tests/inc/ut_os_support.h b/src/unit-tests/inc/ut_os_support.h index cc60a1d84..f1f133af7 100644 --- a/src/unit-tests/inc/ut_os_support.h +++ b/src/unit-tests/inc/ut_os_support.h @@ -79,7 +79,7 @@ static inline bool UtOsalImplemented(int32 Fn, const char *File, uint32 Line) /*--------------------------------------------------------------------------------*/ #define UT_os_sprintf(buf,...) \ - snprintf(buf,sizeof(buf),__VA_ARGS__) + do { int x = snprintf(buf,sizeof(buf),__VA_ARGS__); if (x > 0) buf[x] = 0; } while (0) /*--------------------------------------------------------------------------------*/ From 61751717c85301dabdf05369da4e43e4fa74a277 Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Sun, 10 May 2020 23:02:27 -0400 Subject: [PATCH 2/2] Update #435, add upper bound check to snprintf return --- src/unit-tests/inc/ut_os_support.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unit-tests/inc/ut_os_support.h b/src/unit-tests/inc/ut_os_support.h index f1f133af7..353402aa4 100644 --- a/src/unit-tests/inc/ut_os_support.h +++ b/src/unit-tests/inc/ut_os_support.h @@ -79,7 +79,7 @@ static inline bool UtOsalImplemented(int32 Fn, const char *File, uint32 Line) /*--------------------------------------------------------------------------------*/ #define UT_os_sprintf(buf,...) \ - do { int x = snprintf(buf,sizeof(buf),__VA_ARGS__); if (x > 0) buf[x] = 0; } while (0) + do { int x = snprintf(buf,sizeof(buf),__VA_ARGS__); if (x > 0 && x < sizeof(buf)) buf[x] = 0; } while (0) /*--------------------------------------------------------------------------------*/