Commit b4bdb898 authored by Juergen Kosel's avatar Juergen Kosel

Set pointers to NULL after free

Setting pointers to NULL after free never hurt, but may prevent
double free and other problems.
Signed-off-by: 's avatarJuergen Kosel <juergen.kosel@softing.com>
parent dccbcfc5
...@@ -218,7 +218,10 @@ static int ListUnlink(List* aList, void* content, int(*callback)(void*, void*), ...@@ -218,7 +218,10 @@ static int ListUnlink(List* aList, void* content, int(*callback)(void*, void*),
next = aList->current->next; next = aList->current->next;
if (freeContent) if (freeContent)
{
free(aList->current->content); free(aList->current->content);
aList->current->content = NULL;
}
if (saved == aList->current) if (saved == aList->current)
saveddeleted = 1; saveddeleted = 1;
free(aList->current); free(aList->current);
...@@ -357,7 +360,10 @@ void ListEmpty(List* aList) ...@@ -357,7 +360,10 @@ void ListEmpty(List* aList)
{ {
ListElement* first = aList->first; ListElement* first = aList->first;
if (first->content != NULL) if (first->content != NULL)
{
free(first->content); free(first->content);
first->content = NULL;
}
aList->first = first->next; aList->first = first->next;
free(first); free(first);
} }
......
...@@ -1008,8 +1008,10 @@ static void MQTTAsync_freeServerURIs(MQTTAsyncs* m) ...@@ -1008,8 +1008,10 @@ static void MQTTAsync_freeServerURIs(MQTTAsyncs* m)
for (i = 0; i < m->serverURIcount; ++i) for (i = 0; i < m->serverURIcount; ++i)
free(m->serverURIs[i]); free(m->serverURIs[i]);
m->serverURIcount = 0;
if (m->serverURIs) if (m->serverURIs)
free(m->serverURIs); free(m->serverURIs);
m->serverURIs = NULL;
} }
...@@ -1023,7 +1025,9 @@ static void MQTTAsync_freeCommand1(MQTTAsync_queuedCommand *command) ...@@ -1023,7 +1025,9 @@ static void MQTTAsync_freeCommand1(MQTTAsync_queuedCommand *command)
free(command->command.details.sub.topics[i]); free(command->command.details.sub.topics[i]);
free(command->command.details.sub.topics); free(command->command.details.sub.topics);
command->command.details.sub.topics = NULL;
free(command->command.details.sub.qoss); free(command->command.details.sub.qoss);
command->command.details.sub.qoss = NULL;
} }
else if (command->command.type == UNSUBSCRIBE) else if (command->command.type == UNSUBSCRIBE)
{ {
...@@ -1033,13 +1037,16 @@ static void MQTTAsync_freeCommand1(MQTTAsync_queuedCommand *command) ...@@ -1033,13 +1037,16 @@ static void MQTTAsync_freeCommand1(MQTTAsync_queuedCommand *command)
free(command->command.details.unsub.topics[i]); free(command->command.details.unsub.topics[i]);
free(command->command.details.unsub.topics); free(command->command.details.unsub.topics);
command->command.details.unsub.topics = NULL;
} }
else if (command->command.type == PUBLISH) else if (command->command.type == PUBLISH)
{ {
/* qos 1 and 2 topics are freed in the protocol code when the flows are completed */ /* qos 1 and 2 topics are freed in the protocol code when the flows are completed */
if (command->command.details.pub.destinationName) if (command->command.details.pub.destinationName)
free(command->command.details.pub.destinationName); free(command->command.details.pub.destinationName);
command->command.details.pub.destinationName = NULL;
free(command->command.details.pub.payload); free(command->command.details.pub.payload);
command->command.details.pub.payload = NULL;
} }
} }
......
...@@ -668,11 +668,13 @@ void MQTTProtocol_freeClient(Clients* client) ...@@ -668,11 +668,13 @@ void MQTTProtocol_freeClient(Clients* client)
MQTTProtocol_freeMessageList(client->inboundMsgs); MQTTProtocol_freeMessageList(client->inboundMsgs);
ListFree(client->messageQueue); ListFree(client->messageQueue);
free(client->clientID); free(client->clientID);
client->clientID = NULL;
if (client->will) if (client->will)
{ {
free(client->will->payload); free(client->will->payload);
free(client->will->topic); free(client->will->topic);
free(client->will); free(client->will);
client->will = NULL;
} }
#if defined(OPENSSL) #if defined(OPENSSL)
if (client->sslopts) if (client->sslopts)
...@@ -688,6 +690,7 @@ void MQTTProtocol_freeClient(Clients* client) ...@@ -688,6 +690,7 @@ void MQTTProtocol_freeClient(Clients* client)
if (client->sslopts->enabledCipherSuites) if (client->sslopts->enabledCipherSuites)
free((void*)client->sslopts->enabledCipherSuites); free((void*)client->sslopts->enabledCipherSuites);
free(client->sslopts); free(client->sslopts);
client->sslopts = NULL;
} }
#endif #endif
/* don't free the client structure itself... this is done elsewhere */ /* don't free the client structure itself... this is done elsewhere */
......
...@@ -855,7 +855,10 @@ int SSLSocket_putdatas(SSL* ssl, int socket, char* buf0, size_t buf0len, int cou ...@@ -855,7 +855,10 @@ int SSLSocket_putdatas(SSL* ssl, int socket, char* buf0, size_t buf0len, int cou
for (i = 0; i < count; ++i) for (i = 0; i < count; ++i)
{ {
if (frees[i]) if (frees[i])
{
free(buffers[i]); free(buffers[i]);
buffers[i] = NULL;
}
} }
} }
FUNC_EXIT_RC(rc); FUNC_EXIT_RC(rc);
......
...@@ -818,7 +818,10 @@ int Socket_continueWrite(int socket) ...@@ -818,7 +818,10 @@ int Socket_continueWrite(int socket)
for (i = 0; i < pw->count; i++) for (i = 0; i < pw->count; i++)
{ {
if (pw->frees[i]) if (pw->frees[i])
{
free(pw->iovecs[i].iov_base); free(pw->iovecs[i].iov_base);
pw->iovecs[i].iov_base = NULL;
}
} }
rc = 1; /* signal complete */ rc = 1; /* signal complete */
Log(TRACE_MIN, -1, "ContinueWrite: partial write now complete for socket %d", socket); Log(TRACE_MIN, -1, "ContinueWrite: partial write now complete for socket %d", socket);
...@@ -834,7 +837,10 @@ int Socket_continueWrite(int socket) ...@@ -834,7 +837,10 @@ int Socket_continueWrite(int socket)
for (i = 0; i < pw->count; i++) for (i = 0; i < pw->count; i++)
{ {
if (pw->frees[i]) if (pw->frees[i])
{
free(pw->iovecs[i].iov_base); free(pw->iovecs[i].iov_base);
pw->iovecs[i].iov_base = NULL;
}
} }
} }
#if defined(OPENSSL) #if defined(OPENSSL)
......
...@@ -106,6 +106,7 @@ void SocketBuffer_freeDefQ(void) ...@@ -106,6 +106,7 @@ void SocketBuffer_freeDefQ(void)
{ {
free(def_queue->buf); free(def_queue->buf);
free(def_queue); free(def_queue);
def_queue = NULL;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment