add convenient functions in wire for getting strings with a limited length

This commit is contained in:
hippoz 2023-01-10 15:17:50 +02:00
parent 095eb8f403
commit ce164d8d5d
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
3 changed files with 22 additions and 37 deletions

View file

@ -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) {

7
wire.c
View file

@ -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

6
wire.h
View file

@ -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