don't collect strings when matching if we don't have to

This commit is contained in:
hippoz 2023-02-01 18:49:08 +02:00
parent c112f9416e
commit 73f29429e4
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
2 changed files with 17 additions and 11 deletions

11
match.c
View file

@ -125,14 +125,18 @@ MatchRule *match_rule_from_string(char *d)
} else if (strcmp(key, "destination") == 0 && !rule->destination) { } else if (strcmp(key, "destination") == 0 && !rule->destination) {
rule->destination = string_dup(acc); rule->destination = string_dup(acc);
} else if (strcmp(key, "arg0namespace") == 0 && !rule->arg0namespace) { } else if (strcmp(key, "arg0namespace") == 0 && !rule->arg0namespace) {
rule->has_arg_rules = true;
rule->arg0namespace = string_dup(acc); rule->arg0namespace = string_dup(acc);
} else if (strncmp(key, "arg", 3) == 0) { } else if (strncmp(key, "arg", 3) == 0) {
rule->has_arg_rules = true;
char *part = key + 3; char *part = key + 3;
if (!*part) { if (!*part) {
goto fail; goto fail;
} }
// thanks: https://github.com/bus1/dbus-broker/blob/55fe5443d88ee2b93afa340b82ad89508ff017e2/src/bus/match.c#L105 // thanks: https://github.com/bus1/dbus-broker/blob/55fe5443d88ee2b93afa340b82ad89508ff017e2/src/bus/match.c#L105
// TODO: is this right?
size_t partn = strlen(part); size_t partn = strlen(part);
uint32_t index = 0; uint32_t index = 0;
for (unsigned int i = 0; i < 2 && partn; i++, part++, --partn) { for (unsigned int i = 0; i < 2 && partn; i++, part++, --partn) {
@ -203,7 +207,7 @@ int match_rule_check(BusClient *sender_client, MatchRule *rule, WireMsg *msg, Wi
} }
if (!sender_client) { if (!sender_client) {
// if the sender is negative, we assume the message is coming from the message bus // if the sender is null, we assume the message is coming from the message bus
if (rule->sender && strcmp(rule->sender, "org.freedesktop.DBus") != 0) { if (rule->sender && strcmp(rule->sender, "org.freedesktop.DBus") != 0) {
return -1; return -1;
} }
@ -219,8 +223,8 @@ int match_rule_check(BusClient *sender_client, MatchRule *rule, WireMsg *msg, Wi
/* todo: path_namespace */ /* todo: path_namespace */
_check_header_field_str(destination, DBUS_HEADER_FIELD_DESTINATION); _check_header_field_str(destination, DBUS_HEADER_FIELD_DESTINATION);
WireMsgBodyString strings[MATCH_RULE_MAX_ARG]; if (rule->has_arg_rules) {
memset(strings, 0, sizeof(strings)); WireMsgBodyString strings[MATCH_RULE_MAX_ARG] = {0};
/* TODO: handle failure */ /* TODO: handle failure */
TRYST(wire_collect_strings(ctx, msg, strings, MATCH_RULE_MAX_ARG)); TRYST(wire_collect_strings(ctx, msg, strings, MATCH_RULE_MAX_ARG));
@ -232,6 +236,7 @@ int match_rule_check(BusClient *sender_client, MatchRule *rule, WireMsg *msg, Wi
} }
/* todo: arg0namespace */ /* todo: arg0namespace */
}
return 0; return 0;
} }

View file

@ -10,6 +10,7 @@
typedef struct { typedef struct {
uint8_t type; uint8_t type;
bool has_arg_rules;
bool eavesdrop; bool eavesdrop;
char *sender, *interface, *member, *path, *path_namespace, *destination, *arg0namespace, *rule_string; char *sender, *interface, *member, *path, *path_namespace, *destination, *arg0namespace, *rule_string;
char *arg[MATCH_RULE_MAX_ARG]; /* TODO: spec states that indexes 0 to 63 should be supported */ char *arg[MATCH_RULE_MAX_ARG]; /* TODO: spec states that indexes 0 to 63 should be supported */