Skip to content

Commit

Permalink
[401] Change JSON based command return paths
Browse files Browse the repository at this point in the history
Fixes the return path of the management functions that use cJSON
objects to ensure that all the objects are freed appropriately.
Also changes the return value to reflect the command exit status as
provided by the JSON object.

Close #401
  • Loading branch information
fluca1978 committed Feb 16, 2024
1 parent 20a1650 commit 610e7b8
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions src/libpgagroal/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,13 @@ pgagroal_management_read_status(SSL* ssl, int socket, char output_format)
// print out the command answer
if (output_format == COMMAND_OUTPUT_FORMAT_JSON)
{
pgagroal_json_print_and_free_json_object(json);
return pgagroal_json_print_and_free_json_object(json);
}
else
{
pgagroal_management_json_print_status_details(json);
return pgagroal_management_json_print_status_details(json);
}

return 0;

error:
pgagroal_log_warn("pgagroal_management_read_status: command error [%s]",
(json == NULL ? "<unknown>" : pgagroal_json_get_command_object_status(json)));
Expand Down Expand Up @@ -936,15 +934,13 @@ pgagroal_management_read_details(SSL* ssl, int socket, char output_format)
// print out the command answer
if (output_format == COMMAND_OUTPUT_FORMAT_JSON)
{
pgagroal_json_print_and_free_json_object(json);
return pgagroal_json_print_and_free_json_object(json);
}
else
{
pgagroal_management_json_print_status_details(json);
return pgagroal_management_json_print_status_details(json);
}

return 0;

error:
pgagroal_log_warn("pgagroal_management_read_details: command error [%s]",
(json == NULL ? "<unknown>" : pgagroal_json_get_command_object_status(json)));
Expand Down Expand Up @@ -1103,7 +1099,7 @@ pgagroal_management_read_isalive(SSL* ssl, int socket, int* status, char output_
cJSON_AddStringToObject(output, "message", "unknown");
}

pgagroal_json_print_and_free_json_object(json);
return pgagroal_json_print_and_free_json_object(json);

}

Expand Down Expand Up @@ -1804,15 +1800,21 @@ pgagroal_management_read_config_get(int socket, char* config_key, char* expected
{

cJSON* json = pgagroal_managment_json_read_config_get(socket, config_key, expected_value);
int status = EXIT_STATUS_OK;

if (!json)
if (!json || pgagroal_json_is_command_object_faulty(json))
{
goto error;
}

// extract the command status
status = pgagroal_json_command_object_exit_status(json);

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

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

return pgagroal_json_command_object_exit_status(json);
goto end;

error:

pgagroal_log_warn("pgagroal_management_read_config_get : error retrieving configuration for <%s> : %s", config_key, strerror(errno));
errno = 0;
return EXIT_STATUS_DATA_ERROR;
status = EXIT_STATUS_DATA_ERROR;
end:
if (json)
{
cJSON_Delete(json);
}

return status;
}

int
Expand Down Expand Up @@ -1987,15 +1996,13 @@ pgagroal_management_read_conf_ls(SSL* ssl, int socket, char output_format)
// print out the command answer
if (output_format == COMMAND_OUTPUT_FORMAT_JSON)
{
pgagroal_json_print_and_free_json_object(json);
return pgagroal_json_print_and_free_json_object(json);
}
else
{
pgagroal_management_json_print_conf_ls(json);
return pgagroal_management_json_print_conf_ls(json);
}

return 0;

error:
pgagroal_log_warn("pgagroal_management_read_conf_ls: read: %d %s", socket, strerror(errno));
errno = 0;
Expand Down Expand Up @@ -2176,11 +2183,12 @@ int
pgagroal_management_json_print_status_details(cJSON* json)
{
bool is_command_details = false; /* is this command 'status details' ? */
int status = EXIT_STATUS_OK;

// sanity check
if (!json || pgagroal_json_is_command_object_faulty(json))
{
return 1;
goto error;
}

// the command must be 'status' or 'status details'
Expand Down Expand Up @@ -2292,11 +2300,15 @@ pgagroal_management_json_print_status_details(cJSON* json)

}

error:
status = 1;
end:
return 0;
if (json)
{
cJSON_Delete(json);
}

error:
return 1;
return status;

}

Expand Down Expand Up @@ -2429,6 +2441,8 @@ pgagroal_management_json_read_conf_ls(SSL* ssl, int socket)
static int
pgagroal_management_json_print_conf_ls(cJSON* json)
{
int status = EXIT_STATUS_OK;

// sanity check
if (!json || pgagroal_json_is_command_object_faulty(json))
{
Expand All @@ -2455,7 +2469,16 @@ pgagroal_management_json_print_conf_ls(cJSON* json)
cJSON_GetObjectItemCaseSensitive(current, "path")->valuestring);
}

status = pgagroal_json_command_object_exit_status(json);
goto end;

error:
cJSON_Delete(json);
return 1;
status = EXIT_STATUS_DATA_ERROR;
end:
if (json)
{
cJSON_Delete(json);
}

return status;
}

0 comments on commit 610e7b8

Please sign in to comment.