coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length, const uint8_t *data)
{
OIC_LOG(DEBUG, TAG, "CACreateNewOptionNode IN");
+
+ if (!data)
+ {
+ OIC_LOG(ERROR, TAG, "invalid pointer parameter");
+ return NULL;
+ }
+
coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
if (!option)
{
{
OIC_LOG(DEBUG, TAG, "CAGetInfoFromPDU IN");
+ if (!pdu || !outCode || !outInfo || !outUri)
+ {
+ OIC_LOG(ERROR, TAG, "NULL pointer param");
+ return;
+ }
+
coap_opt_iterator_t opt_iter;
coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
// init HeaderOption list
uint32_t count = CAGetOptionCount(opt_iter);
- if (!outInfo)
- {
- OIC_LOG(ERROR, TAG, "outInfo is null");
- return;
- }
memset(outInfo, 0, sizeof(*outInfo));
outInfo->numOptions = count;
// set type
}
}
- char buf[COAP_MAX_PDU_SIZE] =
- { 0 };
+ char buf[COAP_MAX_PDU_SIZE] = { 0 };
coap_opt_t *option;
- char optionResult[CA_MAX_URI_LENGTH] =
- { 0 };
+ char optionResult[CA_MAX_URI_LENGTH] = { 0 };
uint32_t idx = 0;
uint32_t optionLength = 0;
bool isfirstsetflag = false;
isfirstsetflag = true;
optionResult[optionLength] = '/';
optionLength++;
- memcpy(optionResult + optionLength, buf, bufLength);
- optionLength += bufLength;
+ // Make sure there is enough room in the optionResult buffer
+ if ((optionLength + bufLength) < sizeof(optionResult))
+ {
+ memcpy(optionResult + optionLength, buf, bufLength);
+ optionLength += bufLength;
+ }
+ else
+ {
+ goto exit;
+ }
}
else
{
if (COAP_OPTION_URI_PATH == opt_iter.type)
{
- optionResult[optionLength] = '/';
- optionLength++;
+ // Make sure there is enough room in the optionResult buffer
+ if (optionLength < sizeof(optionResult))
+ {
+ optionResult[optionLength] = '/';
+ optionLength++;
+ }
+ else
+ {
+ goto exit;
+ }
}
else if (COAP_OPTION_URI_QUERY == opt_iter.type)
{
if(false == isQueryBeingProcessed)
{
- optionResult[optionLength] = '?';
- optionLength++;
- isQueryBeingProcessed = true;
+ // Make sure there is enough room in the optionResult buffer
+ if (optionLength < sizeof(optionResult))
+ {
+ optionResult[optionLength] = '?';
+ optionLength++;
+ isQueryBeingProcessed = true;
+ }
+ else
+ {
+ goto exit;
+ }
}
else
{
- optionResult[optionLength] = '&';
- optionLength++;
+ // Make sure there is enough room in the optionResult buffer
+ if (optionLength < sizeof(optionResult))
+ {
+ optionResult[optionLength] = '&';
+ optionLength++;
+ }
+ else
+ {
+ goto exit;
+ }
}
}
- memcpy(optionResult + optionLength, buf, bufLength);
- optionLength += bufLength;
+ // Make sure there is enough room in the optionResult buffer
+ if ((optionLength + bufLength) < sizeof(optionResult))
+ {
+ memcpy(optionResult + optionLength, buf, bufLength);
+ optionLength += bufLength;
+ }
+ else
+ {
+ goto exit;
+ }
}
}
else
memcpy(outUri, optionResult, length);
outUri[length] = '\0';
OIC_LOG_V(DEBUG, TAG, "made URL : %s, %s\n", optionResult, outUri);
- }OIC_LOG(DEBUG, TAG, "CAGetInfoFromPDU OUT");
+ }
+ OIC_LOG(DEBUG, TAG, "CAGetInfoFromPDU OUT");
+ return;
+
+exit:
+ OIC_LOG(ERROR, TAG, "buffer too small");
+ OICFree(outInfo->options);
+ return;
}
CAResult_t CAGenerateTokenInternal(CAToken_t *token)
return 0;
}
- uint32_t cnt = 0;
- while (len)
+ if (buflen <= len)
{
- if (cnt == buflen - 1)
- {
- break;
- }
-
- *option++ = *data;
- ++cnt;
- ++data;
- --len;
+ OIC_LOG(ERROR, TAG, "option buffer too small");
+ return 0;
}
- *option = '\0';
- return cnt;
+ memcpy(option, data, len);
+ option[len] = '\0';
+
+ return len;
}
CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
char *coapUri = (char *) OICCalloc(uriLength, sizeof(char));
if (NULL == coapUri)
{
- OIC_LOG(ERROR, TAG, "error");
+ OIC_LOG(ERROR, TAG, "CAGeneratePdu, Memory allocation failed !");
return NULL;
}
{
OIC_LOG(DEBUG, TAG, "IN");
+ if (!data)
+ {
+ OIC_LOG(ERROR, TAG, "invalid pointer parameter");
+ return NULL;
+ }
+
coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
if (!option)
{
{
OIC_LOG(DEBUG, TAG, "IN");
+ if (!pdu || !outCode || !outInfo || !outUri)
+ {
+ OIC_LOG(ERROR, TAG, "NULL pointer param");
+ return;
+ }
+
coap_opt_iterator_t opt_iter;
coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
// init HeaderOption list
uint32_t count = CAGetOptionCount(opt_iter);
- if (!outInfo)
- {
- OIC_LOG(ERROR, TAG, "outInfo is NULL");
- return;
- }
memset(outInfo, 0, sizeof(*outInfo));
outInfo->numOptions = count;
isfirstsetflag = true;
optionResult[optionLength] = '/';
optionLength++;
- memcpy(optionResult + optionLength, buf, bufLength);
- optionLength += bufLength;
+ // Make sure there is enough room in the optionResult buffer
+ if ((optionLength + bufLength) < sizeof(optionResult))
+ {
+ memcpy(optionResult + optionLength, buf, bufLength);
+ optionLength += bufLength;
+ }
+ else
+ {
+ goto exit;
+ }
}
else
{
if (COAP_OPTION_URI_PATH == opt_iter.type)
{
- optionResult[optionLength] = '/';
- optionLength++;
+ // Make sure there is enough room in the optionResult buffer
+ if (optionLength < sizeof(optionResult))
+ {
+ optionResult[optionLength] = '/';
+ optionLength++;
+ }
+ else
+ {
+ goto exit;
+ }
}
else if (COAP_OPTION_URI_QUERY == opt_iter.type)
{
if(false == isQueryBeingProcessed)
{
- optionResult[optionLength] = '?';
- optionLength++;
- isQueryBeingProcessed = true;
+ // Make sure there is enough room in the optionResult buffer
+ if (optionLength < sizeof(optionResult))
+ {
+ optionResult[optionLength] = '?';
+ optionLength++;
+ isQueryBeingProcessed = true;
+ }
+ else
+ {
+ goto exit;
+ }
}
else
{
- optionResult[optionLength] = '&';
- optionLength++;
+ // Make sure there is enough room in the optionResult buffer
+ if (optionLength < sizeof(optionResult))
+ {
+ optionResult[optionLength] = '&';
+ optionLength++;
+ }
+ else
+ {
+ goto exit;
+ }
}
}
- memcpy(optionResult + optionLength, buf, bufLength);
- optionLength += bufLength;
+ // Make sure there is enough room in the optionResult buffer
+ if ((optionLength + bufLength) < sizeof(optionResult))
+ {
+ memcpy(optionResult + optionLength, buf, bufLength);
+ optionLength += bufLength;
+ }
+ else
+ {
+ goto exit;
+ }
}
}
else
}
OIC_LOG(DEBUG, TAG, "OUT");
+ return;
+
+exit:
+ OIC_LOG(ERROR, TAG, "buffer too small");
+ OICFree(outInfo->options);
+ return;
}
CAResult_t CAGenerateTokenInternal(CAToken_t *token)
return 0;
}
- uint32_t cnt = 0;
- while (len)
+ if (buflen <= len)
{
- if (cnt == buflen)
- {
- break;
- }
- *option++ = *data;
- ++cnt;
- ++data;
- --len;
+ OIC_LOG(ERROR, TAG, "option buffer too small");
+ return 0;
}
- *option = '\0';
- return cnt;
+ memcpy(option, data, len);
+ option[len] = '\0';
+
+ return len;
}
CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
headerOpt = (CAHeaderOption_t *) calloc(1, optionNum * sizeof(CAHeaderOption_t));
char* tmpOptionData1 = (char *) "Hello";
+ size_t tmpOptionDataLen = (strlen(tmpOptionData1) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
+ strlen(tmpOptionData1) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
headerOpt[0].optionID = 3000;
- memcpy(headerOpt[0].optionData, tmpOptionData1, strlen(tmpOptionData1));
- headerOpt[0].optionLength = (uint16_t) strlen(tmpOptionData1);
+ memcpy(headerOpt[0].optionData, tmpOptionData1, tmpOptionDataLen);
+ headerOpt[0].optionLength = (uint16_t) tmpOptionDataLen;
char* tmpOptionData2 = (char *) "World";
+ tmpOptionDataLen = (strlen(tmpOptionData2) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
+ strlen(tmpOptionData2) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
headerOpt[1].optionID = 3001;
- memcpy(headerOpt[1].optionData, tmpOptionData2, strlen(tmpOptionData2));
- headerOpt[1].optionLength = (uint16_t) strlen(tmpOptionData2);
+ memcpy(headerOpt[1].optionData, tmpOptionData2, tmpOptionDataLen);
+ headerOpt[1].optionLength = (uint16_t) tmpOptionDataLen;
CAGenerateToken(&tempToken);
headerOpt = (CAHeaderOption_t *) calloc(1, optionNum * sizeof(CAHeaderOption_t));
char* tmpOptionData1 = (char *) "Hello";
+ size_t tmpOptionDataLen = (strlen(tmpOptionData1) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
+ strlen(tmpOptionData1) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
headerOpt[0].optionID = 3000;
- memcpy(headerOpt[0].optionData, tmpOptionData1, strlen(tmpOptionData1));
- headerOpt[0].optionLength = (uint16_t) strlen(tmpOptionData1);
+ memcpy(headerOpt[0].optionData, tmpOptionData1, tmpOptionDataLen);
+ headerOpt[0].optionLength = (uint16_t) tmpOptionDataLen;
char* tmpOptionData2 = (char *) "World";
+ tmpOptionDataLen = (strlen(tmpOptionData2) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
+ strlen(tmpOptionData2) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
headerOpt[1].optionID = 3001;
- memcpy(headerOpt[1].optionData, tmpOptionData2, strlen(tmpOptionData2));
- headerOpt[1].optionLength = (uint16_t) strlen(tmpOptionData2);
+ memcpy(headerOpt[1].optionData, tmpOptionData2, tmpOptionDataLen);
+ headerOpt[1].optionLength = (uint16_t) tmpOptionDataLen;
CAGenerateToken(&tempToken);