Skip to content

Commit

Permalink
lib/gis: Fix out of scope memory access error in file_name()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ymdatta committed Nov 7, 2024
1 parent cb3d12b commit e37531b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/gis/file_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/*
Expand Down

0 comments on commit e37531b

Please sign in to comment.