From e37531b444212892175b49d38a8dee1a12b1e58b Mon Sep 17 00:00:00 2001 From: Mohan Yelugoti Date: Mon, 4 Nov 2024 21:38:03 -0500 Subject: [PATCH] lib/gis: Fix out of scope memory access error in file_name() When execution takes else path, pname, a pointer, is set to point to a local variable array which has limited scope. This same pointer is accessed outside of the block containing the local variable, essentially creating a scenario where we are accessing memory outside its score, which is undefined behavior. Move the variable array out of the loop, so that it has the same scope as pname. This was found using cppcheck tool. Signed-off-by: Mohan Yelugoti --- lib/gis/file_name.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gis/file_name.c b/lib/gis/file_name.c index 7bf7b9d2539..a3e7bd5d786 100644 --- a/lib/gis/file_name.c +++ b/lib/gis/file_name.c @@ -161,13 +161,13 @@ char *file_name(char *path, const char *dir, const char *element, const char *name, const char *mapset, const char *base) { const char *pname = name; + char xname[GNAME_MAX] = {'\0'}; if (base && *base) { sprintf(path, "%s", base); } else { - char xname[GNAME_MAX]; - char xmapset[GMAPSET_MAX]; + char xmapset[GMAPSET_MAX] = {'\0'}; char *location = G__location_path(); /*