[Intel-gfx] [PATCH V2 i-g-t 1/3] Avoid truncate string in __igt_lsof_fds

Rodrigo Siqueira rodrigosiqueiramelo at gmail.com
Sun Jun 17 00:34:14 UTC 2018


Note that 'proc_path' parameter in __igt_lsof_fds receives a string
which was initialized with the size of PATH_MAX and the local variable
'path' has the same size, but it also have to append: '/', '\0', and the
directory name. This situation caused the warning described below.

warning: ‘%s’ directive output may be truncated writing up to 255 bytes
into a region of size between 0 and 4095 [-Wformat-truncation=]
snprintf(path, sizeof(path), "%s/%s", proc_path, d->d_name);
note: ‘snprintf’ output between 2 and 4352 bytes into a destination of
size 4096 [..]

This commit fixes this problem by changing the string size passed by
__igt_lsoft to __igt_lsof_fds; basically, the max size for the string is
calculated in a directive and then used to declare the array.

Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo at gmail.com>
---
 lib/igt_aux.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/igt_aux.c b/lib/igt_aux.c
index acafb713..1ea52efe 100644
--- a/lib/igt_aux.c
+++ b/lib/igt_aux.c
@@ -48,6 +48,8 @@
 #include <sys/utsname.h>
 #include <termios.h>
 #include <assert.h>
+#include <math.h>
+#include <limits.h>
 
 #include <proc/readproc.h>
 #include <libudev.h>
@@ -71,6 +73,8 @@
 #include <libgen.h>   /* for dirname() */
 #endif
 
+#define MAX_CWD_LEN (unsigned int)(sizeof("/proc//cwd") + ceil(log10(INT_MAX)))
+
 /**
  * SECTION:igt_aux
  * @short_description: Auxiliary libraries and support functions
@@ -1485,7 +1489,7 @@ __igt_lsof(const char *dir)
 	PROCTAB *proc;
 	proc_t *proc_info;
 
-	char path[PATH_MAX];
+	char path[MAX_CWD_LEN];
 	char *name_lnk;
 	struct stat st;
 	int state = 0;
-- 
2.17.1



More information about the Intel-gfx mailing list