Skip to content

lib/vector/Vlib: Fix Resource Leak Issue in ascii.c #5606

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
43 changes: 43 additions & 0 deletions lib/vector/Vlib/ascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ int Vect_read_ascii(FILE *ascii, struct Map_info *Map)
continue;
}
G_warning(_("Error reading ASCII file: (bad type) [%s]"), buff);
G_free(xarray);
G_free(yarray);
G_free(zarray);
return -1;
}
if (ctype == '#') {
Expand Down Expand Up @@ -122,6 +125,9 @@ int Vect_read_ascii(FILE *ascii, struct Map_info *Map)
break;
default: {
G_warning(_("Error reading ASCII file: (unknown type) [%s]"), buff);
G_free(xarray);
G_free(yarray);
G_free(zarray);
return -1;
}
}
Expand Down Expand Up @@ -155,17 +161,26 @@ int Vect_read_ascii(FILE *ascii, struct Map_info *Map)
if (sscanf(buff, " %s %s %lf", east_str, north_str, z) < 2) {
G_warning(_("Error reading ASCII file: (bad point) [%s]"),
buff);
G_free(xarray);
G_free(yarray);
G_free(zarray);
return -1;
}
else {
if (!G_scan_easting(east_str, x, G_projection())) {
G_warning(_("Unparsable longitude value: [%s]"),
east_str);
G_free(xarray);
G_free(yarray);
G_free(zarray);
return -1;
}
if (!G_scan_northing(north_str, y, G_projection())) {
G_warning(_("Unparsable latitude value: [%s]"),
north_str);
G_free(xarray);
G_free(yarray);
G_free(zarray);
return -1;
}
}
Expand Down Expand Up @@ -210,6 +225,9 @@ int Vect_read_ascii(FILE *ascii, struct Map_info *Map)

if (sscanf(buff, "%d%d", &catn, &cat) != 2) {
G_warning(_("Error reading categories: [%s]"), buff);
G_free(xarray);
G_free(yarray);
G_free(zarray);
return -1;
}

Expand All @@ -223,11 +241,17 @@ int Vect_read_ascii(FILE *ascii, struct Map_info *Map)
if (0 >
Vect_copy_xyz_to_pnts(Points, xarray, yarray, zarray, n_points)) {
G_warning(_("Unable to copy points"));
G_free(xarray);
G_free(yarray);
G_free(zarray);
return -1;
}

if (type > 0) {
if (-1 == Vect_write_line(Map, type, Points, Cats)) {
G_free(xarray);
G_free(yarray);
G_free(zarray);
return -1;
}
n_lines++;
Expand All @@ -241,6 +265,9 @@ int Vect_read_ascii(FILE *ascii, struct Map_info *Map)

Vect_destroy_line_struct(Points);
Vect_destroy_cats_struct(Cats);
G_free(xarray);
G_free(yarray);
G_free(zarray);

return n_lines;
}
Expand Down Expand Up @@ -427,6 +454,8 @@ int Vect_write_ascii(FILE *ascii, FILE *att, struct Map_info *Map, int ver,
db_set_string(&dbstring, Fi->table);
if (db_describe_table(driver, &dbstring, &Table) != DB_OK) {
G_warning(_("Unable to describe table <%s>"), Fi->table);
db_close_database_shutdown_driver(driver);
Vect_destroy_field_info(Fi);
return -1;
}

Expand Down Expand Up @@ -476,6 +505,8 @@ int Vect_write_ascii(FILE *ascii, FILE *att, struct Map_info *Map, int ver,
G_warning(_("Export cancelled"));
db_close_database(driver);
db_shutdown_driver(driver);
Vect_destroy_field_info(Fi);
G_free(columns);
return -1;
}
}
Expand Down Expand Up @@ -510,6 +541,11 @@ int Vect_write_ascii(FILE *ascii, FILE *att, struct Map_info *Map, int ver,
G_warning(
_("Unknown type of column <%s>, export cancelled"),
columns[i]);
Vect_destroy_field_info(Fi);
G_free(cats);
G_free(all_columns);
G_free(coltypes);
G_free(columns);
return -1;
}
if (i > 0) {
Expand Down Expand Up @@ -577,6 +613,10 @@ int Vect_write_ascii(FILE *ascii, FILE *att, struct Map_info *Map, int ver,
column_names && strcmp(column_names[0], "*") == 0 ? columns
: NULL);
}
Vect_destroy_cats_struct(ACats);
Vect_destroy_list(fcats);
Vect_destroy_field_info(Fi);
G_free(cats);

return -1;
}
Expand Down Expand Up @@ -920,6 +960,9 @@ int Vect_write_ascii(FILE *ascii, FILE *att, struct Map_info *Map, int ver,
Vect_destroy_line_struct(Points);
Vect_destroy_cats_struct(Cats);
Vect_destroy_cats_struct(ACats);
Vect_destroy_list(fcats);
Vect_destroy_field_info(Fi);
G_free(cats);

return count;
}
Expand Down
Loading