-
-
Notifications
You must be signed in to change notification settings - Fork 301
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
i.ortho.photo: Fix Resource Leak in find_init.c #4388
base: main
Are you sure you want to change the base?
Conversation
|
||
element = (char *)G_malloc(80 * sizeof(char)); | ||
|
||
if (group == NULL || *group == 0) | ||
if (group == NULL || *group == 0) { | ||
G_free(element); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could return 0 before allocating element
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done changes
return 0; | ||
} | ||
sprintf(element, "group/%s", group); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For another PR: use snprintf instead of sprintf.
sprintf(element, "group/%s", group); | |
snprintf(element, ELEMENT_BUFFER_SIZE, "group/%s", group); |
with #define ELEMENT_BUFFER_SIZE 80 also used for malloc instead of a magic number
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it okay to do the changes in the same PR it will save the workflow check time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done changes
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
return G_find_file(element, "INIT_EXP", G_mapset()) != NULL; | ||
} | ||
|
||
element = (char *)G_malloc(ELEMENT_BUFFER_SIZE * sizeof(char)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GNAME_MAX discussed in other PR applies here too. No need to introduce another constant. Technically, INAME_LEN should be used for a group name, but I think we will remove INAME_LEN in favor of GNAME_MAX, so here use GNAME_MAX.
If anyone thinks otherwise, please let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
if (group == NULL || *group == 0) { | ||
return 0; | ||
sprintf(element, "group/%s", group); | ||
return G_find_file(element, "INIT_EXP", G_mapset()) != NULL; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the {}
which was not originally there and there is no need to add it (to have easier to read diff).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
sprintf(element, "group/%s", group); | ||
return G_find_file(element, "INIT_EXP", G_mapset()) != NULL; | ||
|
||
element = (char *)G_malloc(GNAME_MAX * sizeof(char)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to declare element as:
char element[GNAME_MAX];
then no need to malloc, free...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As group
may be of size GNAME_MAX
grass/imagery/i.ortho.photo/lib/orthophoto.h
Lines 93 to 94 in 2098577
struct Ortho_Image_Group { | |
char name[GNAME_MAX]; |
snprintf may truncate, so you should check for that too.
This pull request addresses resource leak issue identified by coverity scan (CID : 1207903).
Replaced the direct return of G_find_file with a variable (file_exists) to store the result before freeing memory.