[PATCH] lib: fwts_modprobe: add helper to build module path name

Colin King colin.king at canonical.com
Tue Jul 28 10:46:20 UTC 2020


From: Colin Ian King <colin.king at canonical.com>

The current method of building a module pathname may cause truncation
and gcc-10 generates build errors because of this. Add a helper that
builds the module path that uses the BSD strlcat for safe string
concatenation with known (and ignored) truncation since we know that
the module path name can't exceed PATH_MAX size.

Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
 src/lib/src/fwts_modprobe.c | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/src/lib/src/fwts_modprobe.c b/src/lib/src/fwts_modprobe.c
index ace2f50c..492bf9b4 100644
--- a/src/lib/src/fwts_modprobe.c
+++ b/src/lib/src/fwts_modprobe.c
@@ -28,9 +28,36 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/utsname.h>
+#include <bsd/string.h>
 
 #include "fwts.h"
 
+/*
+ *  fwts_module_path()
+ *	build a new path based on basename path and
+ *	the new filename (or directory name)
+ */
+static void fwts_module_path(
+	char *newpath,
+	const size_t newpath_len,
+	const char *basepath,
+	const char *filename)
+{
+	char *ptr = newpath;
+	size_t n = newpath_len, len;
+
+	(void)strlcpy(ptr, basepath, n);
+	len = strlen(basepath);
+	ptr += len;
+	n -= len;
+
+	(void)strlcpy(ptr, "/", n);
+	ptr++;
+	n--;
+
+	(void)strlcpy(ptr, filename, n);
+}
+
 /*
  *  fwts_module_find()
  *	recursively search for module from the basepath start
@@ -59,8 +86,7 @@ static bool fwts_module_find(
 
 		switch (de->d_type) {
 		case DT_DIR:
-			(void)snprintf(newpath, sizeof(newpath), "%s/%s",
-				basepath, de->d_name);
+			fwts_module_path(newpath, sizeof(newpath), basepath, de->d_name);
 			if (fwts_module_find(module, newpath, path, path_len)) {
 				(void)closedir(dir);
 				return true;
@@ -68,7 +94,7 @@ static bool fwts_module_find(
 			break;
 		case DT_REG:
 			if (!strcmp(de->d_name, module)) {
-				(void)snprintf(path, path_len, "%s/%s", basepath, module);
+				fwts_module_path(path, path_len, basepath, module);
 				(void)closedir(dir);
 				return true;
 			}
-- 
2.27.0




More information about the fwts-devel mailing list