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/raster: Fix Resource Leak issue in cats.c #5398

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

ShubhamDesai
Copy link
Contributor

This pull request fixes issue identified by Coverity Scan (CID : 1270327)
Used G_free() to fix this issue.

@github-actions github-actions bot added raster Related to raster data processing C Related code is in C libraries labels Mar 15, 2025
Copy link
Contributor

@nilason nilason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a total of nine similar issues left in /lib/raster/*, please address them all here.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@ShubhamDesai
Copy link
Contributor Author

There are a total of nine similar issues left in /lib/raster/*, please address them all here.

before this comment i had already opened PR for CIDs 1208145, 1208146, 1415666, 1415672 in #5407
Remaining 5 are here 1270327, 1208045, 1208055, 1208056, 1299726

Copy link
Contributor

@nilason nilason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions below. And I miss 1208045 from color_rule.c.

@@ -151,6 +151,7 @@ static int read_row_ptrs(int nrows, int old, off_t *row_ptr, int fd)
return 1;

badread:
G_free(buf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buf is only malloced in L129, which means the goto statements in L112, L124 and L126 will free NULL. Although that is possible (given if buf is initialised at definition), I'd say it is clearer if we change the use of goto in those lines to return -1.

if (type < 0)
if (type < 0) {
G_free(rname);
G_free(rmapset);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is trickier than first appears.

The typical (perhaps exclusive) use of Rast_is_reclass() is by passing (static) arrays for rname and rmapset:

char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
int is_reclass = (Rast_is_reclass(name, mapset, rname, rmapset) > 0);

However reclass_type(), unfortunately allows for using both a static and a dynamic array, by either strcpy to the static array or by allocating a new string with G_store. Only in the latter case, we are allowed to free the memory. Given that the latter case, currently never happens, we should skip CID 1208055 and 1208056 this time.

(A possible solution, for another PR, could be to update the documentation of Rast_is_reclass() to emphasise that rname and rmapset must be a pointer to an (allocated) character array; update reclass_type() according to this fact (remove G_store etc.); and make sure all calls to Rast_is_reclass() use static arrays which are NUL initialised.)

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 raster Related to raster data processing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants