[PATCH 3/3] tests/amdgpu: Fix buffer overflow

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch fixes the following warning:
-Wformat-overflow=

Signed-off-by: Luben Tuikov <luben.tuikov@xxxxxxx>
---
 tests/amdgpu/meson.build |  1 +
 tests/amdgpu/ras_tests.c | 41 ++++++++++++++++++++++++++++------------
 2 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/tests/amdgpu/meson.build b/tests/amdgpu/meson.build
index 2a48b43f..faaf0ee6 100644
--- a/tests/amdgpu/meson.build
+++ b/tests/amdgpu/meson.build
@@ -28,6 +28,7 @@ if dep_cunit.found()
     ),
     dependencies : [dep_cunit, dep_threads],
     include_directories : [inc_root, inc_drm, include_directories('../../amdgpu')],
+    link_args : ['-lbsd'],
     link_with : [libdrm, libdrm_amdgpu],
     install : with_install_tests,
   )
diff --git a/tests/amdgpu/ras_tests.c b/tests/amdgpu/ras_tests.c
index f745166b..c96cde45 100644
--- a/tests/amdgpu/ras_tests.c
+++ b/tests/amdgpu/ras_tests.c
@@ -30,6 +30,10 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include "xf86drm.h"
+#include <bsd/string.h>
+#include <limits.h>
+
+#define PATH_SIZE PATH_MAX
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
 
@@ -498,7 +502,7 @@ static int get_file_contents(char *file, char *buf, int size);
 
 static int amdgpu_ras_lookup_id(drmDevicePtr device)
 {
-	char path[1024];
+	char path[PATH_SIZE];
 	char str[128];
 	drmPciBusInfo info;
 	int i;
@@ -525,8 +529,8 @@ static int amdgpu_ras_lookup_id(drmDevicePtr device)
 //helpers
 
 static int test_card;
-static char sysfs_path[1024];
-static char debugfs_path[1024];
+static char sysfs_path[PATH_SIZE];
+static char debugfs_path[PATH_SIZE];
 static uint32_t ras_mask;
 static amdgpu_device_handle device_handle;
 
@@ -605,10 +609,11 @@ static int amdgpu_ras_is_feature_supported(enum amdgpu_ras_block block)
 
 static int amdgpu_ras_invoke(struct ras_debug_if *data)
 {
-	char path[1024];
+	char path[PATH_SIZE];
 	int ret;
 
-	sprintf(path, "%s%s", get_ras_debugfs_root(), "ras_ctrl");
+	snprintf(path, sizeof(path), "%s", get_ras_debugfs_root());
+	strlcpy(path, "ras_ctrl", sizeof(path));
 
 	ret = set_file_contents(path, (char *)data, sizeof(*data))
 		- sizeof(*data);
@@ -619,14 +624,16 @@ static int amdgpu_ras_query_err_count(enum amdgpu_ras_block block,
 		unsigned long *ue, unsigned long *ce)
 {
 	char buf[64];
-	char name[1024];
+	char name[PATH_SIZE];
 
 	*ue = *ce = 0;
 
 	if (amdgpu_ras_is_feature_supported(block) <= 0)
 		return -1;
 
-	sprintf(name, "%s%s%s", get_ras_sysfs_root(), ras_block_str(block), "_err_count");
+	snprintf(name, sizeof(name), "%s", get_ras_sysfs_root());
+	strlcpy(name, ras_block_str(block), sizeof(name));
+	strlcpy(name, "_err_count", sizeof(name));
 
 	if (is_file_ok(name, O_RDONLY))
 		return 0;
@@ -837,7 +844,7 @@ static void amdgpu_ras_basic_test(void)
 	int i;
 	int j;
 	uint32_t features;
-	char path[1024];
+	char path[PATH_SIZE];
 
 	ret = is_file_ok("/sys/module/amdgpu/parameters/ras_mask", O_RDONLY);
 	CU_ASSERT_EQUAL(ret, 0);
@@ -849,11 +856,15 @@ static void amdgpu_ras_basic_test(void)
 				sizeof(features), &features);
 		CU_ASSERT_EQUAL(ret, 0);
 
-		sprintf(path, "%s%s", get_ras_debugfs_root(), "ras_ctrl");
+		snprintf(path, sizeof(path), "%s", get_ras_debugfs_root());
+		strlcpy(path, "ras_ctrl", sizeof(path));
+
 		ret = is_file_ok(path, O_WRONLY);
 		CU_ASSERT_EQUAL(ret, 0);
 
-		sprintf(path, "%s%s", get_ras_sysfs_root(), "features");
+		snprintf(path, sizeof(path), "%s", get_ras_sysfs_root());
+		strlcpy(path, "features", sizeof(path));
+
 		ret = is_file_ok(path, O_RDONLY);
 		CU_ASSERT_EQUAL(ret, 0);
 
@@ -865,11 +876,17 @@ static void amdgpu_ras_basic_test(void)
 			if (!((1 << j) & ras_block_mask_basic))
 				continue;
 
-			sprintf(path, "%s%s%s", get_ras_sysfs_root(), ras_block_str(j), "_err_count");
+			snprintf(path, sizeof(path), "%s", get_ras_sysfs_root());
+			strlcpy(path, ras_block_str(j), sizeof(path));
+			strlcpy(path, "_err_count", sizeof(path));
+
 			ret = is_file_ok(path, O_RDONLY);
 			CU_ASSERT_EQUAL(ret, 0);
 
-			sprintf(path, "%s%s%s", get_ras_debugfs_root(), ras_block_str(j), "_err_inject");
+			snprintf(path, sizeof(path), "%s", get_ras_debugfs_root());
+			strlcpy(path, ras_block_str(j), sizeof(path));
+			strlcpy(path, "_err_inject", sizeof(path));
+
 			ret = is_file_ok(path, O_WRONLY);
 			CU_ASSERT_EQUAL(ret, 0);
 		}
-- 
2.24.0.390.g083378cc35

_______________________________________________
amd-gfx mailing list
amd-gfx@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/amd-gfx




[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux