From 9de1b757773b06c25282b7a8783d309419f529f5 Mon Sep 17 00:00:00 2001 From: Jaewook Jung Date: Mon, 21 Nov 2016 11:27:38 +0900 Subject: [PATCH] added fail case for coap_add_option If coap_add_option fails, it has to return fail while sending data. Otherwise, it causes a crash error when it tries to use a pdu wrong. The below is the comment for coap_add_option on libcoap-4.1.1 /** * Adds option of given type to pdu that is passed as first parameter. * coap_add_option() destroys the PDU's data, so coap_add_data() must be called * after all options have been added. As coap_add_token() destroys the options * following the token, the token must be added before coap_add_option() is * called. This function returns the number of bytes written or @c 0 on error. */ size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data); Change-Id: I1add40ab44e0e57e675f56db0211b12414dfa5ae Signed-off-by: Jaewook Jung Reviewed-on: https://gerrit.iotivity.org/gerrit/14539 Tested-by: jenkins-iotivity Reviewed-by: Ziran Sun Reviewed-by: Phil Coval Reviewed-by: Dan Mihai Reviewed-by: Ashok Babu Channa --- resource/csdk/connectivity/src/cablockwisetransfer.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/resource/csdk/connectivity/src/cablockwisetransfer.c b/resource/csdk/connectivity/src/cablockwisetransfer.c index c49c024..e206db6 100644 --- a/resource/csdk/connectivity/src/cablockwisetransfer.c +++ b/resource/csdk/connectivity/src/cablockwisetransfer.c @@ -1452,20 +1452,24 @@ CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info, { OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.", COAP_OPTION_DATA(*(coap_option *) opt->data)); - OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", (*pdu)->length); - coap_add_option(*pdu, COAP_OPTION_KEY(*(coap_option *) opt->data), - COAP_OPTION_LENGTH(*(coap_option *) opt->data), - COAP_OPTION_DATA(*(coap_option *) opt->data)); + + if (0 == coap_add_option(*pdu, COAP_OPTION_KEY(*(coap_option *) opt->data), + COAP_OPTION_LENGTH(*(coap_option *) opt->data), + COAP_OPTION_DATA(*(coap_option *) opt->data))) + { + OIC_LOG(ERROR, TAG, "coap_add_option has failed"); + res = CA_STATUS_FAILED; + goto exit; + } } } - OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", (*pdu)->length); // if response data is so large. it have to send as block transfer if (!coap_add_data(*pdu, dataLength, (const unsigned char *) info->payload)) { - OIC_LOG(INFO, TAG, "it have to use block"); + OIC_LOG(INFO, TAG, "it has to use block"); res = CA_STATUS_FAILED; goto exit; } -- 2.7.4