Skip to content

Commit 55f7fec

Browse files
authored
Merge pull request #559 from ftapajos/empty-match
Prevent undefined behavior empty topic matching
2 parents 17ff3dc + 9c9e515 commit 55f7fec

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

src/topic.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ bool topic_filter::matches(const string& topic) const
109109
{
110110
auto n = fields_.size();
111111

112+
// Edge case of topic_filter("")
113+
if (n == 0) {
114+
return false;
115+
}
116+
112117
auto topic_fields = topic::split(topic);
113118
auto nt = topic_fields.size();
114119

@@ -126,7 +131,7 @@ bool topic_filter::matches(const string& topic) const
126131
// MQTT v5 Spec, Section 4.7.2:
127132
// https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901246
128133

129-
if (n > 0 && is_wildcard(fields_[0]) && nt > 0 && topic_fields[0].size() > 0 &&
134+
if (is_wildcard(fields_[0]) && nt > 0 && topic_fields[0].size() > 0 &&
130135
topic_fields[0][0] == '$') {
131136
return false;
132137
}

test/unit/test_topic.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,5 +273,8 @@ TEST_CASE("topic matches", "[topic_filter]")
273273
REQUIRE(!topic_filter{"#"}.matches("$SYS/bar"));
274274
REQUIRE(!topic_filter{"$BOB/bar"}.matches("$SYS/bar"));
275275
REQUIRE(!topic_filter{"+/bar"}.matches("$SYS/bar"));
276+
REQUIRE(!topic_filter{""}.matches("foo"));
277+
REQUIRE(!topic_filter{""}.matches("foo/bar"));
278+
REQUIRE(!topic_filter{"foo/bar"}.matches(""));
276279
}
277280
}

0 commit comments

Comments
 (0)