From ccac07cb143b3af4629fdd07095e50cb2d81b25e Mon Sep 17 00:00:00 2001 From: Kent Russell Date: Tue, 27 Aug 2019 07:56:18 -0400 Subject: [PATCH] topology: Fix compile warnings regarding snprintf and path snprintf throws a warning from -Wformat-truncation where the string could be truncated. We address this by referencing the maximum size that can be returned from a file according to MAXNAMLEN . This should safely guard us from truncating the path value. Change-Id: If1d208990d8775e9494835b0deb890d2616fd15b Signed-off-by: Kent Russell --- src/topology.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/topology.c b/src/topology.c index 1538cfae83..633afcd392 100644 --- a/src/topology.c +++ b/src/topology.c @@ -1161,8 +1161,14 @@ static void topology_destroy_temp_cpu_cache_list( static int topology_create_temp_cpu_cache_list(int node, struct proc_cpuinfo *cpuinfo, cpu_cacheinfo_t **temp_cpu_ci_list) { + /* Get max path size from /sys/devices/system/node/node%d/%s/cache + * below, which will max out according to the largest filename, + * which can be present twice in the string above. 29 is for the prefix + * and the +6 is for the cache suffix + */ + const uint32_t MAXPATHSIZE = 29 + MAXNAMLEN + (MAXNAMLEN + 6); cpu_cacheinfo_t *p_temp_cpu_ci_list; /* a list of cpu_ci */ - char path[256], node_dir[256]; + char path[MAXPATHSIZE], node_dir[MAXPATHSIZE]; int max_cpus; cpu_cacheinfo_t *this_cpu; /* one cpu_ci in cpu_ci_list */ int cache_cnt = 0; @@ -1177,7 +1183,7 @@ static int topology_create_temp_cpu_cache_list(int node, *temp_cpu_ci_list = NULL; /* Get info from /sys/devices/system/node/nodeX/cpuY/cache */ - snprintf(node_dir, 256, "/sys/devices/system/node/node%d", node); + snprintf(node_dir, MAXPATHSIZE, "/sys/devices/system/node/node%d", node); /* Other than cpuY folders, this dir also has cpulist and cpumap */ max_cpus = num_subdirs(node_dir, "cpu"); if (max_cpus <= 0) { @@ -1199,7 +1205,7 @@ static int topology_create_temp_cpu_cache_list(int node, continue; if (!isdigit(dir->d_name[3])) /* ignore files like cpulist */ continue; - snprintf(path, 256, "/sys/devices/system/node/node%d/%s/cache", + snprintf(path, MAXPATHSIZE, "/sys/devices/system/node/node%d/%s/cache", node, dir->d_name); this_cpu->num_caches = num_subdirs(path, "index"); this_cpu->cache_prop = calloc(this_cpu->num_caches,