ipc: add sscanf return checks and clean up formatting

This commit is contained in:
stormshadow 2026-04-20 01:01:18 +05:30 committed by Anirvan Banerjee
parent fa8d198082
commit 523fa92a09
2 changed files with 33 additions and 19 deletions

View file

@ -187,9 +187,12 @@ print_json(const char *data, bool pretty)
return; return;
} }
int flags = JSON_C_TO_STRING_NOSLASHESCAPE int flags = JSON_C_TO_STRING_NOSLASHESCAPE;
| (pretty ? (JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED) if (pretty) {
: JSON_C_TO_STRING_PLAIN); flags |= JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED;
} else {
flags |= JSON_C_TO_STRING_PLAIN;
}
printf("%s\n", json_object_to_json_string_ext(obj, flags)); printf("%s\n", json_object_to_json_string_ext(obj, flags));
json_object_put(obj); json_object_put(obj);
@ -217,7 +220,7 @@ check_command_success(const char *data)
/* Print error to stderr */ /* Print error to stderr */
struct json_object *err = NULL; struct json_object *err = NULL;
if (json_object_object_get_ex(item, if (json_object_object_get_ex(item,
"error", &err)) { "error", &err)) {
fprintf(stderr, "Error: %s\n", fprintf(stderr, "Error: %s\n",
json_object_get_string( json_object_get_string(
err)); err));
@ -374,7 +377,7 @@ main(int argc, char *argv[])
while (1) { while (1) {
resp_payload = NULL; resp_payload = NULL;
if (!ipc_recv(fd, &resp_type, &resp_payload, if (!ipc_recv(fd, &resp_type, &resp_payload,
&resp_len)) { &resp_len)) {
break; break;
} }
if (!quiet && resp_payload) { if (!quiet && resp_payload) {

View file

@ -55,14 +55,14 @@
/* =================================================================== /* ===================================================================
* Section 1: Protocol & Client Management * Section 1: Protocol & Client Management
* =================================================================== */ * ===================================================================
*/
static int ipc_socket_fd = -1; static int ipc_socket_fd = -1;
static char *ipc_socket_path; static char *ipc_socket_path;
static struct wl_event_source *ipc_event_source; static struct wl_event_source *ipc_event_source;
static struct wl_list ipc_clients; static struct wl_list ipc_clients;
static void ipc_client_disconnect(struct ipc_client *client); static void ipc_client_disconnect(struct ipc_client *client);
static void ipc_handle_message(struct ipc_client *client, uint32_t type, static void ipc_handle_message(struct ipc_client *client, uint32_t type,
const char *payload, uint32_t len); const char *payload, uint32_t len);
@ -317,7 +317,8 @@ ipc_finish(void)
/* =================================================================== /* ===================================================================
* Section 2: JSON Builders * Section 2: JSON Builders
* =================================================================== */ * ===================================================================
*/
static struct json_object * static struct json_object *
ipc_json_position(struct wlr_box box) ipc_json_position(struct wlr_box box)
@ -756,7 +757,9 @@ ipc_json_get_version(void)
/* Parse version string X.Y.Z */ /* Parse version string X.Y.Z */
const char *ver = LABWC_VERSION; const char *ver = LABWC_VERSION;
int major = 0, minor = 0, patch = 0; int major = 0, minor = 0, patch = 0;
sscanf(ver, "%d.%d.%d", &major, &minor, &patch); if (sscanf(ver, "%d.%d.%d", &major, &minor, &patch) < 3) {
wlr_log(WLR_ERROR, "IPC: failed to parse version: %s", ver);
}
json_object_object_add(obj, "major", json_object_new_int(major)); json_object_object_add(obj, "major", json_object_new_int(major));
json_object_object_add(obj, "minor", json_object_new_int(minor)); json_object_object_add(obj, "minor", json_object_new_int(minor));
@ -864,8 +867,8 @@ ipc_json_input_device(struct input *input)
const char *active_name = const char *active_name =
xkb_keymap_layout_get_name(keymap, active); xkb_keymap_layout_get_name(keymap, active);
json_object_object_add(obj, "xkb_active_layout_name", json_object_object_add(obj, "xkb_active_layout_name",
json_object_new_string(active_name ? active_name json_object_new_string(
: "")); active_name ? active_name : ""));
json_object_object_add(obj, "xkb_active_layout_index", json_object_object_add(obj, "xkb_active_layout_index",
json_object_new_int(active)); json_object_new_int(active));
@ -874,8 +877,8 @@ ipc_json_input_device(struct input *input)
const char *name = const char *name =
xkb_keymap_layout_get_name(keymap, i); xkb_keymap_layout_get_name(keymap, i);
json_object_array_add(layouts, json_object_array_add(layouts,
json_object_new_string(name ? name json_object_new_string(
: "")); name ? name : ""));
} }
json_object_object_add(obj, "xkb_layout_names", json_object_object_add(obj, "xkb_layout_names",
layouts); layouts);
@ -932,7 +935,8 @@ ipc_json_get_seats(void)
/* =================================================================== /* ===================================================================
* Section 3: Sway-Compatible Command Parser * Section 3: Sway-Compatible Command Parser
* =================================================================== */ * ===================================================================
*/
/* /*
* Helper to create an action, add a single string arg, execute it via * Helper to create an action, add a single string arg, execute it via
@ -1251,7 +1255,9 @@ execute_single_command(const char *cmd)
return cmd_result(false, "No focused window"); return cmd_result(false, "No focused window");
} }
int x = 0, y = 0; int x = 0, y = 0;
sscanf(arg + 9, "%d %d", &x, &y); if (sscanf(arg + 9, "%d %d", &x, &y) < 2) {
return cmd_result(false, "Invalid position arguments");
}
struct action *a = action_create("MoveTo"); struct action *a = action_create("MoveTo");
if (a) { if (a) {
action_arg_add_int(a, "x", x); action_arg_add_int(a, "x", x);
@ -1358,7 +1364,9 @@ execute_single_command(const char *cmd)
/* resize set W H */ /* resize set W H */
if (!strncmp(arg, "set ", 4)) { if (!strncmp(arg, "set ", 4)) {
int w = 0, h = 0; int w = 0, h = 0;
sscanf(arg + 4, "%d %d", &w, &h); if (sscanf(arg + 4, "%d %d", &w, &h) < 2) {
return cmd_result(false, "Invalid resize arguments");
}
struct action *a = action_create("ResizeTo"); struct action *a = action_create("ResizeTo");
if (a) { if (a) {
action_arg_add_int(a, "width", w); action_arg_add_int(a, "width", w);
@ -1467,7 +1475,8 @@ ipc_cmd_run(const char *payload)
/* =================================================================== /* ===================================================================
* Section 3b: SUBSCRIBE handler * Section 3b: SUBSCRIBE handler
* =================================================================== */ * ===================================================================
*/
static struct json_object * static struct json_object *
ipc_cmd_subscribe(struct ipc_client *client, const char *payload) ipc_cmd_subscribe(struct ipc_client *client, const char *payload)
@ -1512,7 +1521,8 @@ ipc_cmd_subscribe(struct ipc_client *client, const char *payload)
/* =================================================================== /* ===================================================================
* Message dispatch * Message dispatch
* =================================================================== */ * ===================================================================
*/
static void static void
ipc_handle_message(struct ipc_client *client, uint32_t type, ipc_handle_message(struct ipc_client *client, uint32_t type,
@ -1604,7 +1614,8 @@ ipc_handle_message(struct ipc_client *client, uint32_t type,
/* =================================================================== /* ===================================================================
* Section 4: Event Emitters * Section 4: Event Emitters
* =================================================================== */ * ===================================================================
*/
static void static void
ipc_broadcast_event(uint32_t event_type, uint32_t sub_bit, ipc_broadcast_event(uint32_t event_type, uint32_t sub_bit,