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

Initialize callback to NULL to avoid exception on calling uninitialized callback #840

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Bascy
Copy link

@Bascy Bascy commented Mar 16, 2021

Not initializing the callback pointer will cause null pointer exceptions if it is not set manually. If not initialized it will (probably) not be 0 and therefore i.e. PubSubClient.cpp, line 405 will try to call a random address

Some constructors actually set the callback to NULL but most are not.
To make sure that callback will always be set to NULL unless overwritten in a specific constructor or by calling setCallBack() it should be initialized on declaration

@knolleary
Copy link
Owner

Please add a description to explain what this change is and why it is necessary. Sure it looks simple enough, but why is that needed?

@Bascy Bascy changed the title Initialize callback to nullptr Initialize callback to NULL to avoid exception on calling uninitialized callback Mar 22, 2021
@Bascy
Copy link
Author

Bascy commented Mar 29, 2021

Updated original comment to explain

@hmueller01
Copy link

hmueller01 commented Mar 8, 2024

I would rather use setCallback(NULL); in all PubSubClient constructors that do not contain the MQTT_CALLBACK_SIGNATURE parameter. Currently only PubSubClient()does it.

What do you think about changing all these repetitions of

PubSubClient::PubSubClient(Client& client) {
    this->_state = MQTT_DISCONNECTED;
    setClient(client);
    this->stream = NULL;
    this->bufferSize = 0;
    setBufferSize(MQTT_MAX_PACKET_SIZE);
    setKeepAlive(MQTT_KEEPALIVE);
    setSocketTimeout(MQTT_SOCKET_TIMEOUT);
}

to

PubSubClient::PubSubClient(Client& client) {
    PubSubClient(); // default initialize all members
    setClient(client);
}

and so on ...

@Bascy
Copy link
Author

Bascy commented Mar 8, 2024

I dont think this repo is maintained anymore ... last changes are from 4 years ago

@hmueller01
Copy link

There is a similar approach here #746. @knolleary is willing to do some minor changes #1045, not sure if this will make it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants