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

27
match.c
View file

@ -125,14 +125,18 @@ MatchRule *match_rule_from_string(char *d)
} else if (strcmp(key, "destination") == 0 && !rule->destination) {
rule->destination = string_dup(acc);
} else if (strcmp(key, "arg0namespace") == 0 && !rule->arg0namespace) {
rule->has_arg_rules = true;
rule->arg0namespace = string_dup(acc);
} else if (strncmp(key, "arg", 3) == 0) {
rule->has_arg_rules = true;
char *part = key + 3;
if (!*part) {
goto fail;
}
// thanks: https://github.com/bus1/dbus-broker/blob/55fe5443d88ee2b93afa340b82ad89508ff017e2/src/bus/match.c#L105
// TODO: is this right?
size_t partn = strlen(part);
uint32_t index = 0;
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 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) {
return -1;
}
@ -219,19 +223,20 @@ int match_rule_check(BusClient *sender_client, MatchRule *rule, WireMsg *msg, Wi
/* todo: path_namespace */
_check_header_field_str(destination, DBUS_HEADER_FIELD_DESTINATION);
WireMsgBodyString strings[MATCH_RULE_MAX_ARG];
memset(strings, 0, sizeof(strings));
/* TODO: handle failure */
TRYST(wire_collect_strings(ctx, msg, strings, MATCH_RULE_MAX_ARG));
if (rule->has_arg_rules) {
WireMsgBodyString strings[MATCH_RULE_MAX_ARG] = {0};
/* TODO: handle failure */
TRYST(wire_collect_strings(ctx, msg, strings, MATCH_RULE_MAX_ARG));
for (int i = 0; i < MATCH_RULE_MAX_ARG; i++) {
if (rule->arg[i] && (!strings[i].str || strcmp(strings[i].str, rule->arg[i]) != 0)) {
return -1;
for (int i = 0; i < MATCH_RULE_MAX_ARG; i++) {
if (rule->arg[i] && (!strings[i].str || strcmp(strings[i].str, rule->arg[i]) != 0)) {
return -1;
}
/* todo: arg_path */
}
/* todo: arg_path */
}
/* todo: arg0namespace */
/* todo: arg0namespace */
}
return 0;
}

View file

@ -10,6 +10,7 @@
typedef struct {
uint8_t type;
bool has_arg_rules;
bool eavesdrop;
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 */