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/vector/Vlib: Fix Resource Leak issue in break_polygons.c #4612

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ShubhamDesai
Copy link
Contributor

This pull request fixes issue identified by Coverity scan (CID: 1207832).
Changes Made:

  • Each call to open(filename, O_RDWR | O_CREAT | O_EXCL, 0600) now checks if the file descriptor (fd or xpntfd) was successfully created.
  • If file creation fails, G_fatal_error() is invoked, and the temporary filename is freed before the function exits.
  • If xpntfd creation fails, it also cleans up by destroying the RTree and closing fd.
  • Also there is a check for remove(filename). I have used G_warning over there to print message. Not sure whether to use G_fatal or G_warning.

@github-actions github-actions bot added vector Related to vector data processing C Related code is in C libraries labels Oct 29, 2024
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.

Some suggestions.
And please address CID 1207831 too with this PR.

Comment on lines +134 to +138
if (fd < 0) {
G_free(filename);
G_fatal_error(_("Failed to create temporary file: %s"),
strerror(errno));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (fd < 0) {
G_free(filename);
G_fatal_error(_("Failed to create temporary file: %s"),
strerror(errno));
}

Actually, RTreeCreateTree creates in-memory RTree if open() fails. So, no error if it fails.

Comment on lines +148 to +150
RTreeDestroyTree(RTree);
close(fd);
G_free(filename);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
RTreeDestroyTree(RTree);
close(fd);
G_free(filename);
close(RTree->fd);

No need to free in-memory objects in case of G_fatal_error, the system will take care of it.

Comment on lines +140 to +142
if (remove(filename) != 0) {
G_warning(_("Failed to remove temporary file: %s"), filename);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (remove(filename) != 0) {
G_warning(_("Failed to remove temporary file: %s"), filename);
}
(void)remove(filename);

As the above open may fail, this may too.

@nilason nilason added this to the 8.5.0 milestone Oct 29, 2024
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 vector Related to vector data processing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants