Commit 2ae1bb7ea11f67217c87fb8392bb3c206aa7bd99

Authored by Wiebe Cazemier
1 parent aaeb3603

Integrate valid UTF-8 and publish char checking

Profiling showed it was significant enough to do so.
mqttpacket.cpp
@@ -439,15 +439,9 @@ void MqttPacket::handlePublish() @@ -439,15 +439,9 @@ void MqttPacket::handlePublish()
439 439
440 topic = std::string(readBytes(variable_header_length), variable_header_length); 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 return; 445 return;
452 } 446 }
453 447
utils.cpp
@@ -77,7 +77,7 @@ bool topicsMatch(const std::string &subscribeTopic, const std::string &publishTo @@ -77,7 +77,7 @@ bool topicsMatch(const std::string &subscribeTopic, const std::string &publishTo
77 return result; 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 int multibyte_remain = 0; 82 int multibyte_remain = 0;
83 int cur_code_point = 0; 83 int cur_code_point = 0;
@@ -86,6 +86,9 @@ bool isValidUtf8(const std::string &s) @@ -86,6 +86,9 @@ bool isValidUtf8(const std::string &s)
86 if (x == 0) 86 if (x == 0)
87 return false; 87 return false;
88 88
  89 + if (alsoCheckInvalidPublishChars && (x == '#' || x == '+'))
  90 + return false;
  91 +
89 if(!multibyte_remain) 92 if(!multibyte_remain)
90 { 93 {
91 cur_code_point = 0; 94 cur_code_point = 0;
@@ -49,7 +49,7 @@ std::vector<std::string> splitToVector(const std::string &input, const char sep, @@ -49,7 +49,7 @@ std::vector<std::string> splitToVector(const std::string &input, const char sep,
49 49
50 bool topicsMatch(const std::string &subscribeTopic, const std::string &publishTopic); 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 bool strContains(const std::string &s, const std::string &needle); 54 bool strContains(const std::string &s, const std::string &needle);
55 55