From f0a178697f5d36a1cf3101da5433a1ecd857c094 Mon Sep 17 00:00:00 2001 From: Shubham Vasudeo Desai Date: Mon, 28 Oct 2024 20:37:24 -0400 Subject: [PATCH 1/2] fix Resource Leak issue --- lib/vector/Vlib/break_polygons.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/vector/Vlib/break_polygons.c b/lib/vector/Vlib/break_polygons.c index 2b82d8eff62..b991577efb2 100644 --- a/lib/vector/Vlib/break_polygons.c +++ b/lib/vector/Vlib/break_polygons.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -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)); + } RTree = RTreeCreateTree(fd, 0, 2); - remove(filename); + if (remove(filename) != 0) { + G_warning(_("Failed to remove temporary file: %s"), filename); + } + 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); + 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(); From a811270e38a22a4004968f25f553c695abd1efb1 Mon Sep 17 00:00:00 2001 From: Shubham Vasudeo Desai Date: Thu, 31 Oct 2024 18:43:38 -0400 Subject: [PATCH 2/2] changes --- lib/vector/Vlib/break_polygons.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/vector/Vlib/break_polygons.c b/lib/vector/Vlib/break_polygons.c index b991577efb2..1eb548f96c1 100644 --- a/lib/vector/Vlib/break_polygons.c +++ b/lib/vector/Vlib/break_polygons.c @@ -131,29 +131,19 @@ 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)); - } RTree = RTreeCreateTree(fd, 0, 2); - if (remove(filename) != 0) { - G_warning(_("Failed to remove temporary file: %s"), filename); - } + (void)remove(filename); G_free(filename); filename = G_tempfile(); xpntfd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600); if (xpntfd < 0) { - RTreeDestroyTree(RTree); - close(fd); + close(RTree->fd); G_free(filename); 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); - } + (void)remove(filename); G_free(filename); BPoints = Vect_new_line_struct(); @@ -670,6 +660,7 @@ void Vect_break_polygons_mem(struct Map_info *Map, int type, Vect_destroy_line_struct(Points); Vect_destroy_line_struct(BPoints); Vect_destroy_cats_struct(Cats); + Vect_destroy_cats_struct(ErrCats); G_verbose_message(_("Breaks: %d"), nbreaks); }