From ce164d8d5da23f9dfeba9ac20e9211a3e9d81aa2 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Tue, 10 Jan 2023 15:17:50 +0200 Subject: [PATCH] add convenient functions in wire for getting strings with a limited length --- server.c | 46 +++++++++------------------------------------- wire.c | 7 +++++++ wire.h | 6 ++++++ 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/server.c b/server.c index 49970ca..c138c30 100644 --- a/server.c +++ b/server.c @@ -383,13 +383,7 @@ int handle_hello(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire } int handle_request_name(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) { - char *name = TRYPTR(wire_get_string(ctx)); - int name_len = strlen(name); - if (name_len < 1 || name_len > 256) { - __TRY_TRACE(); - return -1; - } - + char *name = TRYPTR(wire_get_name_string(ctx)); char *name_str = TRYPTR(string_dup(name)); int status_code = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; @@ -411,11 +405,7 @@ int handle_request_name(bus_t *s, int i, wire_message_t *msg, wire_context_t *ct } int handle_get_name_owner(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) { - char *name = TRYPTR(wire_get_string(ctx)); - int name_len = strlen(name); - if (name_len < 1 || name_len > 256) { - return -1; - } + char *name = TRYPTR(wire_get_name_string(ctx)); bus_client_t *target = bus_name_find_client(s, name); if (!target || target->unique_name_index < 0) { @@ -430,11 +420,7 @@ int handle_get_name_owner(bus_t *s, int i, wire_message_t *msg, wire_context_t * } int handle_name_has_owner(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) { - char *name = TRYPTR(wire_get_string(ctx)); - int name_len = strlen(name); - if (name_len < 1 || name_len > 256) { - return -1; - } + char *name = TRYPTR(wire_get_name_string(ctx)); bus_client_t *target = bus_name_find_client(s, name); @@ -487,11 +473,8 @@ int handle_list_activatable_names(bus_t *s, int i, wire_message_t *msg, wire_con int handle_start_service_by_name(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) { // TODO: stub (does nothing and always returns success) - char *name = TRYPTR(wire_get_string(ctx)); - int name_len = strlen(name); - if (name_len < 1 || name_len > 256) { - return -1; - } + char *name = TRYPTR(wire_get_name_string(ctx)); + /* unused flags value */ TRYPTR(wire_get_u32(ctx)); @@ -504,11 +487,7 @@ int handle_start_service_by_name(bus_t *s, int i, wire_message_t *msg, wire_cont } int handle_add_match(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) { - char *match = TRYPTR(wire_get_string(ctx)); - int match_len = strlen(match); - if (match_len < 1 || match_len > MATCH_RULE_MAX) { - return -1; - } + char *match = TRYPTR(wire_get_string_check(ctx, 1, MATCH_RULE_MAX)); printf("client index %d adding match rule: '%s'\n", i, match); @@ -521,11 +500,7 @@ int handle_add_match(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, int handle_remove_match(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) { // TODO: stub (does nothing and always returns success) - char *match = TRYPTR(wire_get_string(ctx)); - int match_len = strlen(match); - if (match_len < 1 || match_len > MATCH_RULE_MAX) { - return -1; - } + char *match = TRYPTR(wire_get_string_check(ctx, 1, MATCH_RULE_MAX)); printf("FIXME: STUB: RemoveMatch: %s\n", match); @@ -549,11 +524,8 @@ int handle_get_connection_stats(bus_t *s, int i, wire_message_t *msg, wire_conte int handle_get_connection_unix_process_id(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) { (void)ctx; - char *name = TRYPTR(wire_get_string(ctx)); - int name_len = strlen(name); - if (name_len < 1 || name_len > 256) { - return -1; - } + + char *name = TRYPTR(wire_get_name_string(ctx)); bus_client_t *target = bus_name_find_client(s, name); if (!target || target->unique_name_index < 0 || target->fd < 0) { diff --git a/wire.c b/wire.c index b471d46..7382521 100644 --- a/wire.c +++ b/wire.c @@ -100,6 +100,13 @@ char *wire_set_string_impl(wire_context_t *c, const char *str, bool as_signature return dest; } +char *wire_get_string_check(wire_context_t *c, int min_length, int max_length) { + char *str = TRYPTR_NIL(wire_get_string_impl(c, false)); + int length = strlen(str); + if (length < min_length || length > max_length) return NULL; + return str; +} + int wire_parse_message(wire_context_t *c, wire_message_t *msg) { // SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages diff --git a/wire.h b/wire.h index 4be6b8e..621447b 100644 --- a/wire.h +++ b/wire.h @@ -123,6 +123,7 @@ _WIRE_DECL_TYPE(boolean, uint32_t, 4); char *wire_get_string_impl(wire_context_t *c, bool as_signature); char *wire_set_string_impl(wire_context_t *c, const char *str, bool as_signature); +char *wire_get_string_check(wire_context_t *c, int min_length, int max_length); int wire_parse_message(wire_context_t *c, wire_message_t *msg); int wire_compose_reply(wire_context_t *c, wire_message_t *msg, const char *signature, uint32_t **out_body_length); int wire_compose_error(wire_context_t *c, wire_message_t *msg, const char *error_name); @@ -145,5 +146,10 @@ static inline char *wire_set_string(wire_context_t *c, const char *str) { return wire_set_string_impl(c, str, false); } +// maximum name length is 255 +static inline char *wire_get_name_string(wire_context_t *c) { + return wire_get_string_check(c, 1, 255); +} + #endif // _JITTERBUG__WIRE_H