Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/gis: Fix out of scope memory access error in file_name function call #4650

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

ymdatta
Copy link
Contributor

@ymdatta ymdatta commented Nov 5, 2024

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 static analysis tool.

@github-actions github-actions bot added C Related code is in C libraries labels Nov 5, 2024
@nilason
Copy link
Contributor

nilason commented Nov 5, 2024

Seems to me that moving the declaration char xname[GNAME_MAX]; to the head of the function solves all related issues.

@nilason nilason added this to the 8.5.0 milestone Nov 5, 2024
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]>
@ymdatta
Copy link
Contributor Author

ymdatta commented Nov 7, 2024

Thanks for the suggestion @nilason. It's much cleaner and effective way to solve the issue. I didn't think of it at all! :)

@echoix echoix merged commit fddbf9f into OSGeo:main Nov 8, 2024
27 checks passed
a0x8o pushed a commit to a0x8o/grass that referenced this pull request Nov 11, 2024
…all (OSGeo#4650)

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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C Related code is in C libraries
Projects
Development

Successfully merging this pull request may close these issues.

3 participants