Skip to contentSkip to conversation

lib/tst_mkfs.c: Fix fall through warning

PATCH New Cyril Hrubis 1 participant 3 messages May 12, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 lib/tst_mkfs.c: Fix fall through warning

tst_mkfs.c: In function 'tst_mkfs_':
../include/old/test.h:139:5: warning: this statement may fall through [-Wimplicit-fallthrough=]

  139 |  if (tst_test) \
      |     ^
tst_mkfs.c:98:3: note: in expansion of macro 'tst_brkm'
   98 |   tst_brkm(TCONF, cleanup_fn,
      |   ^~~~~~~~
tst_mkfs.c:100:2: note: here
  100 |  default:
      |  ^~~~~~~
---
 lib/tst_mkfs.c | 1 +
 1 file changed, 1 insertion(+)
lib/tst_mkfs.c +1 -0
index 3860545eb..38b2e7151 100644
@@ -97,6 +97,7 @@ void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void),
 	case 255:
 		tst_brkm(TCONF, cleanup_fn,
 			 "%s:%d: %s not found in $PATH", file, lineno, mkfs);
+	break;
 	default:
 		tst_brkm(TBROK, cleanup_fn,
 			 "%s:%d: %s failed with %i", file, lineno, mkfs, ret);
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/3 lib: Fix two sprintf() warnings.

tst_test.c: In function 'do_setup':
tst_test.c:69:21: warning: '%s' directive writing up to 1023 bytes into a region of size 1011 [-Wformat-overflow=]

tst_kconfig.c: In function 'tst_kconfig_read':
tst_kconfig.c:67:37: warning: '%s' directive output may be truncated writing up to 1023 bytes into a region of size 1018 [-Wformat-truncation=]

The buffer that we print the path along with variable name has to be bigger than the path obviously.

---
 lib/tst_kconfig.c | 2 +-
 lib/tst_test.c    | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
lib/tst_kconfig.c +1 -1
index 4b51413e5..d49187b6f 100644
@@ -52,7 +52,7 @@ static char is_gzip;
 static FILE *open_kconfig(void)
 {
 	FILE *fp;
-	char buf[1024];
+	char buf[1064];
 	char path_buf[1024];
 	const char *path = kconfig_path(path_buf, sizeof(path_buf));
 
lib/tst_test.c +1 -1
index 64cd3ac33..0e58060e0 100644
@@ -68,7 +68,7 @@ extern unsigned int tst_max_futexes;
 
 #define IPC_ENV_VAR "LTP_IPC_PATH"
 
-static char ipc_path[1024];
+static char ipc_path[1064];
 const char *tst_ipc_path = ipc_path;
 
 static char shm_path[1024];
Signature · 1 line
2.26.2
#2 lore raw

PATCH 3/3 lib: Fix two strncpy() destination equal bounds warnings

In reply to Cyril Hrubis

In function 'strncpy',
inlined from 'tst_sys_conf_save_str' at tst_sys_conf.c:29:2:
/usr/include/bits/string_fortified.h:106:10: warning: '__builtin_strncpy' specified bound 4096 equals destination size [-Wstringop-truncation]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function 'strncpy',
    inlined from 'tst_sys_conf_save_str' at tst_sys_conf.c:30:2:
/usr/include/bits/string_fortified.h:106:10: warning: '__builtin_strncpy' specified bound 4096 equals destination size [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In short we have to always copy dest-1 bytes and zero the last one, that may still make us loose part of the path, but at least we will not crash.

Maybe we should rewrite the code to just use strdup() instead.

---
 lib/tst_sys_conf.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
lib/tst_sys_conf.c +5 -2
index 1ae875387..4ad9f8b9b 100644
@@ -26,8 +26,11 @@ int tst_sys_conf_save_str(const char *path, const char *value)
 {
 	struct tst_sys_conf *n = SAFE_MALLOC(sizeof(*n));
 
-	strncpy(n->path, path, sizeof(n->path));
-	strncpy(n->value, value, sizeof(n->value));
+	strncpy(n->path, path, sizeof(n->path)-1);
+	strncpy(n->value, value, sizeof(n->value)-1);
+
+	n->path[sizeof(n->path) - 1] = 0;
+	n->value[sizeof(n->value) - 1] = 0;
 
 	n->next = save_restore_data;
 	save_restore_data = n;
Signature · 1 line
2.26.2