Commit 34df18e7 authored by Ian Craggs's avatar Ian Craggs

C++ compatibility updates

parent 96faf843
......@@ -20,6 +20,7 @@
* Ian Craggs - fix for bug 420851
* Ian Craggs - fix for bug 432903 - queue persistence
* Ian Craggs - MQTT 3.1.1 support
* Rong Xiang, Ian Craggs - C++ compatibility
*******************************************************************************/
/**
......@@ -396,8 +397,7 @@ int MQTTAsync_create(MQTTAsync* handle, const char* serverURI, const char* clien
m->ssl = 1;
}
#endif
m->serverURI = malloc(strlen(serverURI)+1);
MQTTStrncpy(m->serverURI, serverURI,strlen(serverURI)+1);
m->serverURI = MQTTStrdup(serverURI);
m->responses = ListInitialize();
ListAppend(handles, m, sizeof(MQTTAsyncs));
......@@ -407,8 +407,7 @@ int MQTTAsync_create(MQTTAsync* handle, const char* serverURI, const char* clien
m->c->outboundMsgs = ListInitialize();
m->c->inboundMsgs = ListInitialize();
m->c->messageQueue = ListInitialize();
m->c->clientID = malloc(strlen(clientId)+1);
MQTTStrncpy(m->c->clientID, clientId,strlen(clientId)+1);
m->c->clientID = MQTTStrdup(clientId);
#if !defined(NO_PERSISTENCE)
rc = MQTTPersistence_create(&(m->c->persistence), persistence_type, persistence_context);
......@@ -1348,8 +1347,7 @@ void MQTTAsync_destroy(MQTTAsync* handle)
if (m->c)
{
int saved_socket = m->c->net.socket;
char* saved_clientid = malloc(strlen(m->c->clientID)+1);
MQTTStrncpy(saved_clientid, m->c->clientID,strlen(m->c->clientID)+1);
char* saved_clientid = MQTTStrdup(m->c->clientID);
#if !defined(NO_PERSISTENCE)
MQTTPersistence_close(m->c);
#endif
......@@ -1964,12 +1962,10 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
if (options->will && options->will->struct_version == 0)
{
m->c->will = malloc(sizeof(willMessages));
m->c->will->msg = malloc(strlen(options->will->message) + 1);
MQTTStrncpy(m->c->will->msg, options->will->message,strlen(options->will->message) + 1);
m->c->will->msg = MQTTStrdup(options->will->message);
m->c->will->qos = options->will->qos;
m->c->will->retained = options->will->retained;
m->c->will->topic = malloc(strlen(options->will->topicName) + 1);
MQTTStrncpy(m->c->will->topic, options->will->topicName,strlen(options->will->topicName) + 1);
m->c->will->topic = MQTTStrdup(options->will->topicName);
}
#if defined(OPENSSL)
......@@ -2044,10 +2040,7 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
conn->command.details.conn.serverURIcount = options->serverURIcount;
conn->command.details.conn.serverURIs = malloc(options->serverURIcount * sizeof(char*));
for (i = 0; i < options->serverURIcount; ++i)
{
conn->command.details.conn.serverURIs[i] = malloc(strlen(options->serverURIs[i]) + 1);
MQTTStrncpy(conn->command.details.conn.serverURIs[i], options->serverURIs[i],strlen(options->serverURIs[i]) + 1);
}
conn->command.details.conn.serverURIs[i] = MQTTStrdup(options->serverURIs[i]);
conn->command.details.conn.currentURI = 0;
}
}
......@@ -2188,8 +2181,7 @@ int MQTTAsync_subscribeMany(MQTTAsync handle, size_t count, char* const* topic,
sub->command.details.sub.qoss = malloc(sizeof(int) * count);
for (i = 0; i < count; ++i)
{
sub->command.details.sub.topics[i] = malloc(strlen(topic[i]) + 1);
MQTTStrncpy(sub->command.details.sub.topics[i], topic[i],strlen(topic[i]) + 1);
sub->command.details.sub.topics[i] = MQTTStrdup(topic[i]);
sub->command.details.sub.qoss[i] = qos[i];
}
rc = MQTTAsync_addCommand(sub, sizeof(sub));
......@@ -2257,10 +2249,7 @@ int MQTTAsync_unsubscribeMany(MQTTAsync handle, size_t count, char* const* topic
unsub->command.details.unsub.count = count;
unsub->command.details.unsub.topics = malloc(sizeof(char*) * count);
for (i = 0; i < count; ++i)
{
unsub->command.details.unsub.topics[i] = malloc(strlen(topic[i]) + 1);
MQTTStrncpy(unsub->command.details.unsub.topics[i], topic[i],strlen(topic[i]) + 1);
}
unsub->command.details.unsub.topics[i] = MQTTStrdup(topic[i]);
rc = MQTTAsync_addCommand(unsub, sizeof(unsub));
exit:
......@@ -2313,8 +2302,7 @@ int MQTTAsync_send(MQTTAsync handle, const char* destinationName, size_t payload
pub->command.onFailure = response->onFailure;
pub->command.context = response->context;
}
pub->command.details.pub.destinationName = malloc(strlen(destinationName) + 1);
MQTTStrncpy(pub->command.details.pub.destinationName, destinationName,strlen(destinationName) + 1);
pub->command.details.pub.destinationName = MQTTStrdup(destinationName);
pub->command.details.pub.payloadlen = payloadlen;
pub->command.details.pub.payload = malloc(payloadlen);
memcpy(pub->command.details.pub.payload, payload, payloadlen);
......
......@@ -23,6 +23,7 @@
* Ian Craggs - fix for bug 432903 - queue persistence
* Ian Craggs - MQTT 3.1.1 support
* Ian Craggs - fix for bug 438176 - MQTT version selection
* Rong Xiang, Ian Craggs - C++ compatibility
*******************************************************************************/
/**
......@@ -276,8 +277,7 @@ int MQTTClient_create(MQTTClient* handle, const char* serverURI, const char* cli
m->ssl = 1;
}
#endif
m->serverURI = malloc(strlen(serverURI)+1);
MQTTStrncpy(m->serverURI, serverURI,strlen(serverURI)+1);
m->serverURI = MQTTStrdup(serverURI);
ListAppend(handles, m, sizeof(MQTTClients));
m->c = malloc(sizeof(Clients));
......@@ -286,8 +286,7 @@ int MQTTClient_create(MQTTClient* handle, const char* serverURI, const char* cli
m->c->outboundMsgs = ListInitialize();
m->c->inboundMsgs = ListInitialize();
m->c->messageQueue = ListInitialize();
m->c->clientID = malloc(strlen(clientId)+1);
MQTTStrncpy(m->c->clientID, clientId,strlen(clientId)+1);
m->c->clientID = MQTTStrdup(clientId);
m->connect_sem = Thread_create_sem();
m->connack_sem = Thread_create_sem();
m->suback_sem = Thread_create_sem();
......@@ -367,8 +366,7 @@ void MQTTClient_destroy(MQTTClient* handle)
if (m->c)
{
int saved_socket = m->c->net.socket;
char* saved_clientid = malloc(strlen(m->c->clientID)+1);
MQTTStrncpy(saved_clientid, m->c->clientID,strlen(m->c->clientID)+1);
char* saved_clientid = MQTTStrdup(m->c->clientID);
#if !defined(NO_PERSISTENCE)
MQTTPersistence_close(m->c);
#endif
......@@ -962,12 +960,10 @@ int MQTTClient_connectURI(MQTTClient handle, MQTTClient_connectOptions* options,
if (options->will && options->will->struct_version == 0)
{
m->c->will = malloc(sizeof(willMessages));
m->c->will->msg = malloc(strlen(options->will->message) + 1);
MQTTStrncpy(m->c->will->msg , options->will->message,strlen(options->will->message) + 1);
m->c->will->msg = MQTTStrdup(options->will->message);
m->c->will->qos = options->will->qos;
m->c->will->retained = options->will->retained;
m->c->will->topic = malloc(strlen(options->will->topicName) + 1);
MQTTStrncpy(m->c->will->topic , options->will->topicName,strlen(options->will->topicName) + 1);
m->c->will->topic = MQTTStrdup(options->will->topicName);
}
#if defined(OPENSSL)
......@@ -992,30 +988,15 @@ int MQTTClient_connectURI(MQTTClient handle, MQTTClient_connectOptions* options,
m->c->sslopts = malloc(sizeof(MQTTClient_SSLOptions));
memset(m->c->sslopts, '\0', sizeof(MQTTClient_SSLOptions));
if (options->ssl->trustStore)
{
m->c->sslopts->trustStore = malloc(strlen(options->ssl->trustStore) + 1);
MQTTStrncpy(m->c->sslopts->trustStore , options->ssl->trustStore,strlen(options->ssl->trustStore)+1);
}
m->c->sslopts->trustStore = MQTTStrdup(options->ssl->trustStore);
if (options->ssl->keyStore)
{
m->c->sslopts->keyStore = malloc(strlen(options->ssl->keyStore) + 1);
MQTTStrncpy(m->c->sslopts->keyStore , options->ssl->keyStore,strlen(options->ssl->keyStore)+1);
}
m->c->sslopts->keyStore = MQTTStrdup(options->ssl->keyStore);
if (options->ssl->privateKey)
{
m->c->sslopts->privateKey = malloc(strlen(options->ssl->privateKey) + 1);
MQTTStrncpy(m->c->sslopts->privateKey , options->ssl->privateKey,strlen(options->ssl->privateKey)+1);
}
m->c->sslopts->privateKey = MQTTStrdup(options->ssl->privateKey);
if (options->ssl->privateKeyPassword)
{
m->c->sslopts->privateKeyPassword = malloc(strlen(options->ssl->privateKeyPassword) + 1);
MQTTStrncpy(m->c->sslopts->privateKeyPassword , options->ssl->privateKeyPassword,strlen(options->ssl->privateKeyPassword)+1);
}
m->c->sslopts->privateKeyPassword = MQTTStrdup(options->ssl->privateKeyPassword);
if (options->ssl->enabledCipherSuites)
{
m->c->sslopts->enabledCipherSuites = malloc(strlen(options->ssl->enabledCipherSuites) + 1);
MQTTStrncpy(m->c->sslopts->enabledCipherSuites , options->ssl->enabledCipherSuites,strlen(options->ssl->enabledCipherSuites)+1);
}
m->c->sslopts->enabledCipherSuites = MQTTStrdup(options->ssl->enabledCipherSuites);
m->c->sslopts->enableServerCertAuth = options->ssl->enableServerCertAuth;
}
#endif
......
......@@ -14,6 +14,7 @@
* Ian Craggs - initial API and implementation and/or initial documentation
* Ian Craggs, Allan Stockdill-Mander - SSL updates
* Ian Craggs - MQTT 3.1.1 support
* Rong Xiang, Ian Craggs - C++ compatibility
*******************************************************************************/
/**
......@@ -214,7 +215,7 @@ void* MQTTPacket_suback(unsigned char aHeader, char* data, size_t datalen)
pack->header.byte = aHeader;
pack->msgId = readInt(&curdata);
pack->qoss = ListInitialize();
while (curdata - data < datalen)
while ((size_t)(curdata - data) < datalen)
{
int* newint;
newint = malloc(sizeof(int));
......
......@@ -15,6 +15,7 @@
* Ian Craggs, Allan Stockdill-Mander - SSL updates
* Ian Craggs - fix for bug 413429 - connectionLost not called
* Ian Craggs - fix for bug 421103 - trying to write to same socket, in retry
* Rong Xiang, Ian Craggs - C++ compatibility
*******************************************************************************/
/**
......@@ -716,14 +717,14 @@ void MQTTProtocol_freeMessageList(List* msgList)
/**
* Copy not more than dest_size characters from the string pointed to by src to the array pointed to by dest.
* Copy no more than dest_size -1 characters from the string pointed to by src to the array pointed to by dest.
* The destination string will always be null-terminated.
* @param dest the array which characters copy to
* @param src the source string which characters copy from
* @param dest_size the length of characters need copy
* @param dest_size the size of the memory pointed to by dest: copy no more than this -1 (allow for null). Must be >= 1
* @return the destination string pointer
*/
char *MQTTStrncpy(char *dest, const char *src, size_t dest_size)
char* MQTTStrncpy(char *dest, const char *src, size_t dest_size)
{
size_t count = dest_size;
char *temp = dest;
......@@ -741,3 +742,17 @@ char *MQTTStrncpy(char *dest, const char *src, size_t dest_size)
FUNC_EXIT;
return dest;
}
/**
* Duplicate a string, safely, allocating space on the heap
* @param src the source string which characters copy from
* @return the duplicated, allocated string
*/
char* MQTTStrdup(const char* src)
{
size_t mlen = strlen(src) + 1;
char* temp = malloc(mlen);
MQTTStrncpy(temp, src, mlen);
return temp;
}
......@@ -14,6 +14,7 @@
* Ian Craggs - initial API and implementation and/or initial documentation
* Ian Craggs, Allan Stockdill-Mander - SSL updates
* Ian Craggs - MQTT 3.1.1 updates
* Rong Xiang, Ian Craggs - C++ compatibility
*******************************************************************************/
#if !defined(MQTTPROTOCOLCLIENT_H)
......@@ -47,5 +48,6 @@ void MQTTProtocol_freeClient(Clients* client);
void MQTTProtocol_emptyMessageList(List* msgList);
void MQTTProtocol_freeMessageList(List* msgList);
char *MQTTStrncpy(char *dest, const char *src, size_t num);
char* MQTTStrncpy(char *dest, const char* src, size_t num);
char* MQTTStrdup(const char* src);
#endif
......@@ -15,6 +15,7 @@
* Ian Craggs, Allan Stockdill-Mander - SSL updates
* Ian Craggs - fix for buffer overflow in addressPort bug #433290
* Ian Craggs - MQTT 3.1.1 support
* Rong Xiang, Ian Craggs - C++ compatibility
*******************************************************************************/
/**
......@@ -43,7 +44,7 @@ extern ClientStates* bstate;
char* MQTTProtocol_addressPort(const char* uri, int* port)
{
char* colon_pos = strrchr(uri, ':'); /* reverse find to allow for ':' in IPv6 addresses */
char* buf;
char* buf = (char*)uri;
int len;
FUNC_ENTRY;
......@@ -59,16 +60,9 @@ char* MQTTProtocol_addressPort(const char* uri, int* port)
buf = malloc(addr_len + 1);
*port = atoi(colon_pos + 1);
MQTTStrncpy(buf, uri, addr_len+1);
buf[addr_len] = '\0';
}
else
{
int addr_len = strlen(uri)+1;
*port = DEFAULT_PORT;
buf = malloc(addr_len);
MQTTStrncpy(buf, uri, addr_len+1);
buf[addr_len] = '\0';
}
len = strlen(buf);
if (buf[len - 1] == ']')
......
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