Skip to content

Commit

Permalink
Improve Resource Cleanup on Fatal Errors
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Oct 25, 2024
1 parent 9742ca1 commit cfc23bf
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions vector/v.info/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ void print_columns(struct Map_info *Map, const char *input_opt,
num_dblinks = Vect_get_num_dblinks(Map);

if (num_dblinks <= 0) {
Vect_close(&Map);
G_fatal_error(
_("Database connection for map <%s> is not defined in DB file"),
input_opt);
Expand All @@ -198,30 +199,43 @@ void print_columns(struct Map_info *Map, const char *input_opt,
"layer <%s>:"),
field_opt);

if ((fi = Vect_get_field2(Map, field_opt)) == NULL)
if ((fi = Vect_get_field2(Map, field_opt)) == NULL) {
Vect_close(&Map);
G_fatal_error(
_("Database connection not defined for layer <%s> of <%s>"),
field_opt, input_opt);
}
driver = db_start_driver(fi->driver);
if (driver == NULL)
if (driver == NULL) {
Vect_close(&Map);
G_fatal_error(_("Unable to open driver <%s>"), fi->driver);
}
db_init_handle(&handle);
db_set_handle(&handle, fi->database, NULL);
if (db_open_database(driver, &handle) != DB_OK)
if (db_open_database(driver, &handle) != DB_OK) {
db_shutdown_driver(driver);
Vect_close(&Map);
G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
fi->database, fi->driver);
}
db_init_string(&table_name);
db_set_string(&table_name, fi->table);
if (db_describe_table(driver, &table_name, &table) != DB_OK)
if (db_describe_table(driver, &table_name, &table) != DB_OK) {
db_close_database(driver);
db_shutdown_driver(driver);
Vect_close(&Map);
G_fatal_error(_("Unable to describe table <%s>"), fi->table);
}

JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_object(root_value);

JSON_Value *columns_value = json_value_init_array();
JSON_Array *columns_array = json_array(columns_value);
JSON_Value *root_value = NULL, *columns_value = NULL;
JSON_Object *root_object = NULL;
JSON_Array *columns_array = NULL;

if (format == JSON) {
root_value = json_value_init_object();
root_object = json_object(root_value);
columns_value = json_value_init_array();
columns_array = json_array(columns_value);
json_object_set_value(root_object, "columns", columns_value);
}

Expand Down Expand Up @@ -260,6 +274,10 @@ void print_columns(struct Map_info *Map, const char *input_opt,
char *serialized_string = NULL;
serialized_string = json_serialize_to_string_pretty(root_value);
if (serialized_string == NULL) {
json_value_free(root_value);
db_close_database(driver);
db_shutdown_driver(driver);
Vect_close(&Map);
G_fatal_error(_("Failed to initialize pretty JSON string."));
}
puts(serialized_string);
Expand Down

0 comments on commit cfc23bf

Please sign in to comment.