Commit 2ae1bb7ea11f67217c87fb8392bb3c206aa7bd99
1 parent
aaeb3603
Integrate valid UTF-8 and publish char checking
Profiling showed it was significant enough to do so.
Showing
3 changed files
with
7 additions
and
10 deletions
mqttpacket.cpp
| ... | ... | @@ -439,15 +439,9 @@ void MqttPacket::handlePublish() |
| 439 | 439 | |
| 440 | 440 | topic = std::string(readBytes(variable_header_length), variable_header_length); |
| 441 | 441 | |
| 442 | - if (!isValidUtf8(topic)) | |
| 442 | + if (!isValidUtf8(topic, true)) | |
| 443 | 443 | { |
| 444 | - logger->logf(LOG_WARNING, "Client '%s' published a message with invalid UTF8. Dropping.", sender->repr().c_str()); | |
| 445 | - return; | |
| 446 | - } | |
| 447 | - | |
| 448 | - if (!isValidPublishPath(topic)) | |
| 449 | - { | |
| 450 | - logger->logf(LOG_WARNING, "Client '%s' published a message with invalid publish path '%s'. Dropping.", sender->repr().c_str(), topic.c_str()); | |
| 444 | + logger->logf(LOG_WARNING, "Client '%s' published a message with invalid UTF8 or +/# in it. Dropping.", sender->repr().c_str()); | |
| 451 | 445 | return; |
| 452 | 446 | } |
| 453 | 447 | ... | ... |
utils.cpp
| ... | ... | @@ -77,7 +77,7 @@ bool topicsMatch(const std::string &subscribeTopic, const std::string &publishTo |
| 77 | 77 | return result; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | -bool isValidUtf8(const std::string &s) | |
| 80 | +bool isValidUtf8(const std::string &s, bool alsoCheckInvalidPublishChars) | |
| 81 | 81 | { |
| 82 | 82 | int multibyte_remain = 0; |
| 83 | 83 | int cur_code_point = 0; |
| ... | ... | @@ -86,6 +86,9 @@ bool isValidUtf8(const std::string &s) |
| 86 | 86 | if (x == 0) |
| 87 | 87 | return false; |
| 88 | 88 | |
| 89 | + if (alsoCheckInvalidPublishChars && (x == '#' || x == '+')) | |
| 90 | + return false; | |
| 91 | + | |
| 89 | 92 | if(!multibyte_remain) |
| 90 | 93 | { |
| 91 | 94 | cur_code_point = 0; | ... | ... |
utils.h
| ... | ... | @@ -49,7 +49,7 @@ std::vector<std::string> splitToVector(const std::string &input, const char sep, |
| 49 | 49 | |
| 50 | 50 | bool topicsMatch(const std::string &subscribeTopic, const std::string &publishTopic); |
| 51 | 51 | |
| 52 | -bool isValidUtf8(const std::string &s); | |
| 52 | +bool isValidUtf8(const std::string &s, bool alsoCheckInvalidPublishChars = false); | |
| 53 | 53 | |
| 54 | 54 | bool strContains(const std::string &s, const std::string &needle); |
| 55 | 55 | ... | ... |