From 349d25dccfacb314dc54a809f7b54b641a3ee172 Mon Sep 17 00:00:00 2001 From: Dmitry Frank Date: Fri, 24 Feb 2017 17:28:47 +0200 Subject: [PATCH] Fix # matching logic E.g. given the expression `foo/#`: - `foo/bar` matches - `foo/` should not match - `foo` should not match PUBLISHED_FROM=40f3290cb9a478b22d9f1ab6382884b5de70e266 --- mongoose.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mongoose.c b/mongoose.c index b5ba29a8..5f84b7b1 100644 --- a/mongoose.c +++ b/mongoose.c @@ -9706,10 +9706,21 @@ static void mg_mqtt_proto_data_destructor(void *proto_data) { int mg_mqtt_match_topic_expression(struct mg_str exp, struct mg_str topic) { /* TODO(mkm): implement real matching */ if (memchr(exp.p, '#', exp.len)) { - exp.len -= 2; - if (topic.len < exp.len) { - exp.len = topic.len; + /* exp `foo/#` will become `foo/` */ + exp.len -= 1; + /* + * topic should be longer than the expression: e.g. topic `foo/bar` does + * match `foo/#`, but neither `foo` nor `foo/` do. + */ + if (topic.len <= exp.len) { + return 0; } + + /* Truncate topic so that it'll pass the next length check */ + topic.len = exp.len; + } + if (topic.len != exp.len) { + return 0; } return strncmp(topic.p, exp.p, exp.len) == 0; }