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

i.ortho.photo: Fix Resource Leak in find_init.c #4388

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

Conversation

ShubhamDesai
Copy link
Contributor

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.

@github-actions github-actions bot added C Related code is in C module imagery labels Sep 25, 2024

element = (char *)G_malloc(80 * sizeof(char));

if (group == NULL || *group == 0)
if (group == NULL || *group == 0) {
G_free(element);
Copy link
Contributor

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

Copy link
Contributor Author

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);
Copy link
Contributor

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.

Suggested change
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

Copy link
Contributor Author

@ShubhamDesai ShubhamDesai Sep 25, 2024

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done changes

Shubham Vasudeo Desai and others added 5 commits September 25, 2024 16:12
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));
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 15 to 17
if (group == NULL || *group == 0) {
return 0;
sprintf(element, "group/%s", group);
return G_find_file(element, "INIT_EXP", G_mapset()) != NULL;
}
Copy link
Member

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Shubham Vasudeo Desai added 2 commits September 25, 2024 17:50
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));
Copy link
Contributor

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...

Copy link
Contributor

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

struct Ortho_Image_Group {
char name[GNAME_MAX];

snprintf may truncate, so you should check for that too.

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 imagery module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants