diff --git a/include/sway/commands.h b/include/sway/commands.h index 7ca0bda88..6d17144a9 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -79,7 +79,7 @@ void free_cmd_results(struct cmd_results *results); * * Free the JSON string later on. */ -const char *cmd_results_to_json(struct cmd_results *results); +char *cmd_results_to_json(struct cmd_results *results); struct cmd_results *add_color(const char *name, char *buffer, const char *color); diff --git a/sway/commands.c b/sway/commands.c index 5b67e1ec5..ef477f389 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -527,7 +527,7 @@ void free_cmd_results(struct cmd_results *results) { free(results); } -const char *cmd_results_to_json(struct cmd_results *results) { +char *cmd_results_to_json(struct cmd_results *results) { json_object *result_array = json_object_new_array(); json_object *root = json_object_new_object(); json_object_object_add(root, "success", @@ -542,9 +542,9 @@ const char *cmd_results_to_json(struct cmd_results *results) { } json_object_array_add(result_array, root); const char *json = json_object_to_json_string(result_array); - free(result_array); - free(root); - return json; + char *res = strdup(json); + json_object_put(result_array); + return res; } /** diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 01b80b05e..96889b395 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -481,11 +481,10 @@ void ipc_client_handle_command(struct ipc_client *client) { case IPC_COMMAND: { struct cmd_results *results = execute_command(buf, NULL); - const char *json = cmd_results_to_json(results); - char reply[256]; - int length = snprintf(reply, sizeof(reply), "%s", json); + char *json = cmd_results_to_json(results); + int length = strlen(json); + client_valid = ipc_send_reply(client, json, (uint32_t)length); free(json); - client_valid = ipc_send_reply(client, reply, (uint32_t)length); free_cmd_results(results); goto exit_cleanup; }