Skip to content

Commit

Permalink
[#399] Fix segmentation fault in conf get JSON format
Browse files Browse the repository at this point in the history
The `conf get` implementation in JSON format was trying to extract the
command status after having freed the JSON object.
This commit changes the `pgagroal_json_print_and_free_json_object()`
function making it returning the status code for the command
contained (possibly) in the JSON object.
In this way, it is possible to call the fuction in a way like:

     int status = pgagroal_json_print_and_free_json_object( json );

and get back the status leaving the function to free the object.

Close #399
  • Loading branch information
fluca1978 committed Feb 16, 2024
1 parent 8b13185 commit 20a1650
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/include/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,14 @@ pgagroal_json_is_command_name_equals_to(cJSON* json, char* command_name);
* when there is the need to print out the information
* contained in a json object.
*
* Since the JSON object will be invalidated, the method
* returns the status of the JSON command within it
* to be used.
*
* @param json the json object to print
* @return the command status within the JSON object
*/
void
int
pgagroal_json_print_and_free_json_object(cJSON* json);

/**
Expand Down
4 changes: 3 additions & 1 deletion src/libpgagroal/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,11 @@ pgagroal_json_get_command_object_status(cJSON* json)

}

void
int
pgagroal_json_print_and_free_json_object(cJSON* json)
{
int status = pgagroal_json_command_object_exit_status(json);
printf("%s\n", cJSON_Print(json));
cJSON_Delete(json);
return status;
}
4 changes: 1 addition & 3 deletions src/libpgagroal/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -1812,8 +1812,7 @@ pgagroal_management_read_config_get(int socket, char* config_key, char* expected

if (output_format == COMMAND_OUTPUT_FORMAT_JSON)
{
pgagroal_json_print_and_free_json_object(json);
goto end;
return pgagroal_json_print_and_free_json_object(json);
}

// if here, print out in text format
Expand All @@ -1829,7 +1828,6 @@ pgagroal_management_read_config_get(int socket, char* config_key, char* expected
printf("%s\n", value->valuestring);
}

end:
return pgagroal_json_command_object_exit_status(json);

error:
Expand Down

0 comments on commit 20a1650

Please sign in to comment.