From cfc23bfc4445f635ab73eec2b113dc1f8b174430 Mon Sep 17 00:00:00 2001 From: Nishant Bansal Date: Fri, 25 Oct 2024 22:58:18 +0530 Subject: [PATCH] Improve Resource Cleanup on Fatal Errors Signed-off-by: Nishant Bansal --- vector/v.info/print.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/vector/v.info/print.c b/vector/v.info/print.c index 3d0fe42e9f..5d00f2d120 100644 --- a/vector/v.info/print.c +++ b/vector/v.info/print.c @@ -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); @@ -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); } @@ -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);