diff options
author | Dominique Martinet <asmadeus@codewreck.org> | 2018-07-05 07:07:59 +0900 |
---|---|---|
committer | Dominique Martinet <asmadeus@codewreck.org> | 2018-07-05 13:11:02 +0900 |
commit | fe72e3b349f0905519481b77b22c525aca9c704d (patch) | |
tree | 3b63d204bf5b1fef09814d45786030c46a317b86 | |
parent | workspace_next_name: free targets later than these already found in order (diff) | |
download | sway-fe72e3b349f0905519481b77b22c525aca9c704d.tar.gz sway-fe72e3b349f0905519481b77b22c525aca9c704d.tar.zst sway-fe72e3b349f0905519481b77b22c525aca9c704d.zip |
cmd_results_to_json: return copied string and properly free the json
The only user of this function would copy the string right away
to get rid of the const flag anyway, and freeing a const string
afterwards might work but is not meant to be done according to the
json-c API.
-rw-r--r-- | include/sway/commands.h | 2 | ||||
-rw-r--r-- | sway/commands.c | 8 | ||||
-rw-r--r-- | sway/ipc-server.c | 7 |
3 files changed, 8 insertions, 9 deletions
diff --git a/include/sway/commands.h b/include/sway/commands.h index 7ca0bda8..6d17144a 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h | |||
@@ -79,7 +79,7 @@ void free_cmd_results(struct cmd_results *results); | |||
79 | * | 79 | * |
80 | * Free the JSON string later on. | 80 | * Free the JSON string later on. |
81 | */ | 81 | */ |
82 | const char *cmd_results_to_json(struct cmd_results *results); | 82 | char *cmd_results_to_json(struct cmd_results *results); |
83 | 83 | ||
84 | struct cmd_results *add_color(const char *name, | 84 | struct cmd_results *add_color(const char *name, |
85 | char *buffer, const char *color); | 85 | char *buffer, const char *color); |
diff --git a/sway/commands.c b/sway/commands.c index 5b67e1ec..ef477f38 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -527,7 +527,7 @@ void free_cmd_results(struct cmd_results *results) { | |||
527 | free(results); | 527 | free(results); |
528 | } | 528 | } |
529 | 529 | ||
530 | const char *cmd_results_to_json(struct cmd_results *results) { | 530 | char *cmd_results_to_json(struct cmd_results *results) { |
531 | json_object *result_array = json_object_new_array(); | 531 | json_object *result_array = json_object_new_array(); |
532 | json_object *root = json_object_new_object(); | 532 | json_object *root = json_object_new_object(); |
533 | json_object_object_add(root, "success", | 533 | json_object_object_add(root, "success", |
@@ -542,9 +542,9 @@ const char *cmd_results_to_json(struct cmd_results *results) { | |||
542 | } | 542 | } |
543 | json_object_array_add(result_array, root); | 543 | json_object_array_add(result_array, root); |
544 | const char *json = json_object_to_json_string(result_array); | 544 | const char *json = json_object_to_json_string(result_array); |
545 | free(result_array); | 545 | char *res = strdup(json); |
546 | free(root); | 546 | json_object_put(result_array); |
547 | return json; | 547 | return res; |
548 | } | 548 | } |
549 | 549 | ||
550 | /** | 550 | /** |
diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 01b80b05..96889b39 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c | |||
@@ -481,11 +481,10 @@ void ipc_client_handle_command(struct ipc_client *client) { | |||
481 | case IPC_COMMAND: | 481 | case IPC_COMMAND: |
482 | { | 482 | { |
483 | struct cmd_results *results = execute_command(buf, NULL); | 483 | struct cmd_results *results = execute_command(buf, NULL); |
484 | const char *json = cmd_results_to_json(results); | 484 | char *json = cmd_results_to_json(results); |
485 | char reply[256]; | 485 | int length = strlen(json); |
486 | int length = snprintf(reply, sizeof(reply), "%s", json); | 486 | client_valid = ipc_send_reply(client, json, (uint32_t)length); |
487 | free(json); | 487 | free(json); |
488 | client_valid = ipc_send_reply(client, reply, (uint32_t)length); | ||
489 | free_cmd_results(results); | 488 | free_cmd_results(results); |
490 | goto exit_cleanup; | 489 | goto exit_cleanup; |
491 | } | 490 | } |