Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix domain buffer #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.1.0
* Fix setServer(domain, ...) by copy domain string to own buffer (prevent potential dangling string pointer) #10
* Connect to broker only if port != 0 (e.g. in case of incorrect PubSubClient initialization) #10

3.0.2
* Added github workflow to execute tests
* Added github workflow to compile examples using arduino-cli
Expand Down
20 changes: 16 additions & 4 deletions src/PubSubClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ PubSubClient::PubSubClient() {
this->_state = MQTT_DISCONNECTED;
this->_client = NULL;
this->stream = NULL;
this->domain = NULL;
this->port = 0;
setCallback(NULL);
this->bufferSize = 0;
setBufferSize(MQTT_MAX_PACKET_SIZE);
Expand Down Expand Up @@ -95,6 +97,7 @@ PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGN
}

PubSubClient::~PubSubClient() {
free(this->domain);
free(this->buffer);
}

Expand Down Expand Up @@ -122,8 +125,8 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c

if (_client->connected()) {
result = 1;
} else {
if (domain != NULL) {
} else if (this->port != 0) {
if (this->domain != NULL) {
result = _client->connect(this->domain, this->port);
} else {
result = _client->connect(this->ip, this->port);
Expand Down Expand Up @@ -654,13 +657,22 @@ PubSubClient& PubSubClient::setServer(uint8_t* ip, uint16_t port) {
PubSubClient& PubSubClient::setServer(IPAddress ip, uint16_t port) {
this->ip = ip;
this->port = port;
free(this->domain);
this->domain = NULL;
return *this;
}

PubSubClient& PubSubClient::setServer(const char* domain, uint16_t port) {
this->domain = domain;
this->port = port;
char* newDomain = (char*)realloc(this->domain, strlen(domain) + 1);
if (newDomain != NULL) {
strcpy(newDomain, domain);
this->domain = newDomain;
this->port = port;
} else {
free(this->domain);
this->domain = NULL;
this->port = 0;
}
return *this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/PubSubClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class PubSubClient : public Print {
// (MQTT_MAX_HEADER_SIZE - <returned size>) bytes into the buffer
size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
IPAddress ip;
const char* domain;
char* domain;
uint16_t port;
Stream* stream;
int _state;
Expand Down