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

libvector/vedit: fix memory leaks #3619

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/vector/vedit/break.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ int Vedit_split_lines(struct Map_info *Map, struct ilist *List,

struct line_pnts *Points, *Points2;
struct line_cats *Cats;
struct ilist *List_in_box;

nlines_modified = 0;

Points = Vect_new_line_struct();
Points2 = Vect_new_line_struct();
Cats = Vect_new_cats_struct();
List_in_box = Vect_new_list();

for (i = 0; i < List->n_values; i++) {
line = List->value[i];
Expand Down Expand Up @@ -101,7 +99,8 @@ int Vedit_split_lines(struct Map_info *Map, struct ilist *List,
else
newline = Vect_write_line(Map, type, Points2, Cats);
if (newline < 0) {
return -1;
nlines_modified = -1;
goto free_exit;
}
if (List_updated)
Vect_list_append(List_updated, newline);
Expand All @@ -118,7 +117,8 @@ int Vedit_split_lines(struct Map_info *Map, struct ilist *List,
/* rewrite the line */
newline = Vect_write_line(Map, type, Points2, Cats);
if (newline < 0) {
return -1;
nlines_modified = -1;
goto free_exit;
}
if (List_updated)
Vect_list_append(List_updated, newline);
Expand All @@ -127,10 +127,10 @@ int Vedit_split_lines(struct Map_info *Map, struct ilist *List,
} /* for each bounding box */
} /* for each selected line */

free_exit:
Vect_destroy_line_struct(Points);
Vect_destroy_line_struct(Points2);
Vect_destroy_cats_struct(Cats);
Vect_destroy_list(List_in_box);

return nlines_modified;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/vector/vedit/delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ int Vedit_delete_area(struct Map_info *Map, int area)
int i, line, centroid, left, right;
struct ilist *list;

list = Vect_new_list();

G_debug(3, "Vedit_delete_area(): area=%d", area);
centroid = Vect_get_area_centroid(Map, area);
if (centroid != 0) {
Expand All @@ -105,6 +103,7 @@ int Vedit_delete_area(struct Map_info *Map, int area)
G_warning(_("Area %d without centroid"), area);
return 0;
}
list = Vect_new_list();
Vect_get_area_boundaries(Map, area, list);
if (list->n_values > 0) {
for (i = 0; i < list->n_values; i++) {
Expand All @@ -119,6 +118,7 @@ int Vedit_delete_area(struct Map_info *Map, int area)
}
else {
G_warning(_("Area %d has no boundaries"), area);
Vect_destroy_list(list);
return 0;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/vector/vedit/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ int Vedit_merge_lines(struct Map_info *Map, struct ilist *List)
if (Points->n_points > 0) {
line = Vect_rewrite_line(Map, line1, type1, Points, Cats1);
if (line < 0) {
return -1;
nlines_merged = -1;
goto free_exit;
}

if (line1 <= nlines)
Expand All @@ -195,6 +196,7 @@ int Vedit_merge_lines(struct Map_info *Map, struct ilist *List)
}
} /* for each line */

free_exit:
/* destroy structures */
Vect_destroy_line_struct(Points1);
Vect_destroy_line_struct(Points2);
Expand All @@ -203,6 +205,8 @@ int Vedit_merge_lines(struct Map_info *Map, struct ilist *List)
Vect_destroy_cats_struct(Cats1);
Vect_destroy_cats_struct(Cats2);

Vect_destroy_list(List_in_box);

return nlines_merged;
}

Expand Down
4 changes: 3 additions & 1 deletion lib/vector/vedit/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ struct robject *draw_line(struct Map_info *Map, int line, int draw_flag)
G_debug(3, " draw_line(): type=%d rtype=%d npoints=%d draw=%d", state.type,
obj->type, state.Points->n_points, draw);

if (!draw)
if (!draw) {
G_free(obj);
return NULL;
}

obj->npoints = state.Points->n_points;
obj->point =
Expand Down
8 changes: 5 additions & 3 deletions lib/vector/vedit/snap.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ int Vedit_snap_line(struct Map_info *Map, struct Map_info **BgMap, int nbgmaps,

struct line_cats *Cats;

Cats = Vect_new_cats_struct();

G_debug(3, "Vedit_snap_line(): thresh=%g, to_vertex=%d", thresh, to_vertex);

if (line > 0 && !Vect_line_alive(Map, line))
return -1;

Cats = Vect_new_cats_struct();

npoints = Points->n_points;
x = Points->x;
y = Points->y;
Expand Down Expand Up @@ -190,13 +190,15 @@ int Vedit_snap_lines(struct Map_info *Map, struct Map_info **BgMap, int nbgmaps,
if (Vedit_snap_line(Map, BgMap, nbgmaps, line, Points, thresh,
to_vertex) == 1) {
if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
return -1;
nlines_modified = -1;
goto free_exit;
}

nlines_modified++;
}
}

free_exit:
Vect_destroy_line_struct(Points);
Vect_destroy_cats_struct(Cats);

Expand Down
10 changes: 5 additions & 5 deletions lib/vector/vedit/vertex.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ int Vedit_move_vertex(struct Map_info *Map, struct Map_info **BgMap,
double *x, *y, *z;
char *moved;

struct line_pnts *Points, *Points_snap;
struct line_pnts *Points;
struct line_cats *Cats;

nvertices_moved = nvertices_snapped = 0;
moved = NULL;

Points = Vect_new_line_struct();
Points_snap = Vect_new_line_struct();
Cats = Vect_new_cats_struct();

for (i = 0; i < List->n_values; i++) {
Expand Down Expand Up @@ -157,16 +156,17 @@ int Vedit_move_vertex(struct Map_info *Map, struct Map_info **BgMap,

if (rewrite) {
if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
return -1;
nvertices_moved = -1;
goto free_exit;
}
}
} /* for each selected line */

free_exit:
/* destroy structures */
Vect_destroy_line_struct(Points);
Vect_destroy_line_struct(Points_snap);
Vect_destroy_cats_struct(Cats);
/* G_free ((void *) moved); */
G_free(moved);

return nvertices_moved;
}
Expand Down
9 changes: 6 additions & 3 deletions lib/vector/vedit/zbulk.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ int Vedit_bulk_labeling(struct Map_info *Map, struct ilist *List, double x1,
/* write temporary line */
temp_line = Vect_write_line(Map, GV_LINE, Points_se, Cats);
if (temp_line < 0) {
return -1;
nlines_modified = -1;
goto free_exit;
}

Vect_line_box(Points_se, &box_se);
Expand Down Expand Up @@ -118,17 +119,19 @@ int Vedit_bulk_labeling(struct Map_info *Map, struct ilist *List, double x1,
}

if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
return -1;
nlines_modified = -1;
goto free_exit;
}
nlines_modified++;

value += step;
}

if (Vect_delete_line(Map, temp_line) < 0) {
return -1;
nlines_modified = -1;
Copy link
Member

Choose a reason for hiding this comment

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

should this also have a goto like all other parts of the function, in prevision of future code changes that adds code underneath? (to prevent semantic conflicts in the future)

}

free_exit:
db_CatValArray_free(&cv);
Vect_destroy_line_struct(Points);
Vect_destroy_line_struct(Points_se);
Expand Down