paho.mqtt.c
paho.mqtt.c copied to clipboard
Access freed memory in connect on username
Hello,
I noticed the following behavior on connect with different connection option.
- Connect with username/password
- Connect without username/password
On second connect the first username/password is used again.
Using the current "master" Branch.
IMHO:
On connect the username/password is copied from options in MQTTClient.username/password. If I call connect a second time without username (null) in options, the memory of MQTTClient.username is freed, but the pointer is not set to zero ;). Now the freed pointer still directing to the previous username is used for connect. Its lucky, if the memory is still valid.
https://github.com/eclipse/paho.mqtt.c/blob/master/src/MQTTClient.c#L1614
if (m->c->username)
free((void*)m->c->username);
if (options->username)
m->c->username = MQTTStrdup(options->username);
It seems m->c->username should be zeroed after free or set to zero in else-branch of options->username check ?
Perhaps I missed something.....
Yes, setting the pointer to NULL after being freed should be done.