From 5cd49fb6008d9bb2ef0cbec7d38efbf02b76bfdd Mon Sep 17 00:00:00 2001 From: Horatio Zhang Date: Sun, 29 Sep 2024 17:02:18 +0800 Subject: [PATCH] wsl/hsakmt: Resolve snprintf Truncation Warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch addresses a warning encountered during the build process: warning: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size between 0 and 544 [-Wformat-truncation=] snprintf(path, MAXPATHSIZE, "%s/%s/cache", node_dir, dir->d_name); ^~ note: ‘snprintf’ output between 8 and 807 bytes into a destination of size 545 snprintf(path, MAXPATHSIZE, "%s/%s/cache", node_dir, dir->d_name); ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Horatio Zhang Reviewed-by: Aaron Liu Part-of: --- topology.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/topology.cpp b/topology.cpp index 1f93340ffd..b62a340e2c 100644 --- a/topology.cpp +++ b/topology.cpp @@ -870,7 +870,14 @@ topology_create_temp_cpu_cache_list(int node, struct proc_cpuinfo *cpuinfo, continue; if (!isdigit(dir->d_name[3])) /* ignore files like cpulist */ continue; - snprintf(path, MAXPATHSIZE, "%s/%s/cache", node_dir, dir->d_name); + if (strlen(node_dir) + strlen(dir->d_name) + strlen("/cache") + 2 < MAXPATHSIZE) { + std::string path_str = std::string(node_dir) + "/" + dir->d_name + "/cache"; + strncpy(path, path_str.c_str(), MAXPATHSIZE); + path[MAXPATHSIZE - 1] = '\0'; + } else { + pr_err("Path is too long and was truncated.\n"); + goto exit; + } this_cpu->num_caches = num_subdirs(path, "index"); this_cpu->cache_prop = (HsaCacheProperties *)calloc( this_cpu->num_caches, sizeof(HsaCacheProperties));