Merge pull request #655 from abderraouf-adjal/strnlen-patch-1

Using strnlen() instead of strlen()
This commit is contained in:
Nick O'Leary 2020-05-09 22:30:23 +01:00 committed by GitHub
commit aca970d2d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

View File

@ -375,11 +375,11 @@ boolean PubSubClient::loop() {
}
boolean PubSubClient::publish(const char* topic, const char* payload) {
return publish(topic,(const uint8_t*)payload, payload ? strlen(payload) : 0,false);
return publish(topic,(const uint8_t*)payload, payload ? strnlen(payload, MQTT_MAX_PACKET_SIZE)) : 0,false);
}
boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) {
return publish(topic,(const uint8_t*)payload, payload ? strlen(payload) : 0,retained);
return publish(topic,(const uint8_t*)payload, payload ? strnlen(payload, MQTT_MAX_PACKET_SIZE)) : 0,retained);
}
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
@ -388,7 +388,7 @@ boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigne
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
if (connected()) {
if (MQTT_MAX_PACKET_SIZE < MQTT_MAX_HEADER_SIZE + 2+strlen(topic) + plength) {
if (MQTT_MAX_PACKET_SIZE < MQTT_MAX_HEADER_SIZE + 2+strnlen(topic, MQTT_MAX_PACKET_SIZE) + plength) {
// Too long
return false;
}
@ -413,7 +413,7 @@ boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigne
}
boolean PubSubClient::publish_P(const char* topic, const char* payload, boolean retained) {
return publish_P(topic, (const uint8_t*)payload, payload ? strlen(payload) : 0, retained);
return publish_P(topic, (const uint8_t*)payload, payload ? strnlen(payload, MQTT_MAX_PACKET_SIZE) : 0, retained);
}
boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
@ -431,7 +431,7 @@ boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsig
return false;
}
tlen = strlen(topic);
tlen = strnlen(topic, MQTT_MAX_PACKET_SIZE);
header = MQTTPUBLISH;
if (retained) {
@ -548,7 +548,7 @@ boolean PubSubClient::subscribe(const char* topic) {
}
boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
size_t topicLength = strlen(topic);
size_t topicLength = strnlen(topic, MQTT_MAX_PACKET_SIZE);
if (topic == 0) {
return false;
}
@ -576,7 +576,7 @@ boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
}
boolean PubSubClient::unsubscribe(const char* topic) {
size_t topicLength = strlen(topic);
size_t topicLength = strnlen(topic, MQTT_MAX_PACKET_SIZE);
if (topic == 0) {
return false;
}

View File

@ -83,7 +83,7 @@
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
#endif
#define CHECK_STRING_LENGTH(l,s) if (l+2+strlen(s) > MQTT_MAX_PACKET_SIZE) {_client->stop();return false;}
#define CHECK_STRING_LENGTH(l,s) if (l+2+strnlen(s, MQTT_MAX_PACKET_SIZE) > MQTT_MAX_PACKET_SIZE) {_client->stop();return false;}
class PubSubClient : public Print {
private: