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 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/vector/Vlib/break_polygons.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <unistd.h>
#include <math.h>
#include <errno.h>
#include <string.h>
#include <grass/vector.h>
#include <grass/glocale.h>

Expand Down Expand Up @@ -130,12 +131,30 @@ void Vect_break_polygons_file(struct Map_info *Map, int type,

filename = G_tempfile();
fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
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.

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

RTree = RTreeCreateTree(fd, 0, 2);
remove(filename);
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.

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

G_free(filename);

filename = G_tempfile();
xpntfd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
remove(filename);
if (xpntfd < 0) {
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.

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

G_fatal_error(_("Failed to create xpnt temporary file: %s"),
strerror(errno));
}
if (remove(filename) != 0) {
G_warning(_("Failed to remove xpnt temporary file: %s"), filename);
}
G_free(filename);

BPoints = Vect_new_line_struct();
Points = Vect_new_line_struct();
Expand Down
Loading