From c2b17f169247e8c1e5f1a8f7e45f8637e883f2d7 Mon Sep 17 00:00:00 2001 From: Roger Light Date: Mon, 28 Apr 2014 21:22:02 +0100 Subject: [PATCH] Fix topic matching edge case. Thanks to Tobias Assarsson. Change-Id: If98891597ac47488a072389d483dba15cf54c520 --- src/paho/mqtt/client.py | 14 +++++++------- test/lib/python/09-util-topic-matching.test | 3 +++ test/lib/python3/09-util-topic-matching.test | 3 +++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index 652acad..6b01eba 100755 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -206,6 +206,13 @@ def topic_matches_sub(sub, topic): while spos < slen and tpos < tlen: if sub[spos] == topic[tpos]: + if tpos == tlen-1: + # Check for e.g. foo matching foo/# + if spos == slen-3 and sub[spos+1] == '/' and sub[spos+2] == '#': + result = True + multilevel_wildcard = True + break + spos += 1 tpos += 1 @@ -235,13 +242,6 @@ def topic_matches_sub(sub, topic): result = False break - if tpos == tlen-1: - # Check for e.g. foo matching foo/# - if spos == slen-3 and sub[spos+1] == '/' and sub[spos+2] == '#': - result = True - multilevel_wildcard = True - break - if not multilevel_wildcard and (tpos < tlen or spos < slen): result = False diff --git a/test/lib/python/09-util-topic-matching.test b/test/lib/python/09-util-topic-matching.test index 4ffe39a..f7fa69a 100755 --- a/test/lib/python/09-util-topic-matching.test +++ b/test/lib/python/09-util-topic-matching.test @@ -8,10 +8,13 @@ def do_check(sub, topic, bad_res): print("ERROR: "+sub+" "+topic) sys.exit(1) +do_check("test/6/#", "test/3", True) + do_check("foo/bar", "foo/bar", False) do_check("foo/+", "foo/bar", False) do_check("foo/+/baz", "foo/bar/baz", False) do_check("foo/+/#", "foo/bar/baz", False) +do_check("A/B/+/#", "A/B/B/C", False) do_check("#", "foo/bar/baz", False) do_check("foo/bar", "foo", True) diff --git a/test/lib/python3/09-util-topic-matching.test b/test/lib/python3/09-util-topic-matching.test index 76c9350..18a1b32 100755 --- a/test/lib/python3/09-util-topic-matching.test +++ b/test/lib/python3/09-util-topic-matching.test @@ -8,10 +8,13 @@ def do_check(sub, topic, bad_res): print("ERROR: "+sub+" "+topic) sys.exit(1) +do_check("test/6/#", "test/3", True) + do_check("foo/bar", "foo/bar", False) do_check("foo/+", "foo/bar", False) do_check("foo/+/baz", "foo/bar/baz", False) do_check("foo/+/#", "foo/bar/baz", False) +do_check("A/B/+/#", "A/B/B/C", False) do_check("#", "foo/bar/baz", False) do_check("foo/bar", "foo", True) -- 2.39.5