netutils/libcoap : Fix problem from static analysis tool
authorJin-Seong Kim <jseong82.kim@samsung.com>
Tue, 1 Aug 2017 08:40:48 +0000 (17:40 +0900)
committerEunBong Song <eunb.song@samsung.com>
Wed, 30 Aug 2017 04:15:49 +0000 (21:15 -0700)
This commit is patch for fixing problems fro static analysis tool
- Add check return value of pthread_attr_init

Change-Id: I7ae7369c0c0cfbabcd9c0298836943d5260e7e4e
Signed-off-by: Jin-Seong Kim <jseong82.kim@samsung.com>
Signed-off-by: EunBong Song <eunb.song@samsung.com>
apps/netutils/libcoap/block.c
apps/netutils/libcoap/debug.c
apps/netutils/libcoap/pdu.c

index fe6e56b..46d7fb1 100644 (file)
@@ -50,11 +50,13 @@ int coap_get_block2(coap_pdu_t *pdu, unsigned short type, coap_block_t *block, c
        memset(block, 0, sizeof(coap_block_t));
 
        if (pdu && (option = coap_check_option2(pdu, type, &opt_iter, transport))) {
-               block->szx = COAP_OPT_BLOCK_SZX(option);
-               if (COAP_OPT_BLOCK_MORE(option)) {
-                       block->m = 1;
+               if (option) { /* to prevent dereference null options */
+                       block->szx = COAP_OPT_BLOCK_SZX(option);
+                       if (COAP_OPT_BLOCK_MORE(option)) {
+                               block->m = 1;
+                       }
+                       block->num = coap_opt_block_num(option);
                }
-               block->num = coap_opt_block_num(option);
                return 1;
        }
 
index 25acb1d..fb66cc4 100644 (file)
@@ -236,21 +236,75 @@ size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, si
 #ifndef WITH_CONTIKI
 void coap_show_pdu(const coap_pdu_t *pdu)
 {
-#ifndef WITH_TCP
-       unsigned char buf[COAP_MAX_PDU_SIZE]; /* need some space for output creation */
-#else
-       unsigned char buf[COAP_TCP_LENGTH_LIMIT_32_BIT]; /* need some space for output creation */
-#endif
+
+       unsigned int buf_len = 0;
+       unsigned char *buf = NULL;
+
        int encode = 0, have_options = 0;
        coap_opt_iterator_t opt_iter;
        coap_opt_t *option;
-       /* TODO : Considering TCP Case */
-       fprintf(COAP_DEBUG_FD, "v:%d t:%d tkl:%d c:%d id:%u", pdu->transport_hdr->udp.version,
-                       pdu->transport_hdr->udp.type, pdu->transport_hdr->udp.token_length,
-                                   pdu->transport_hdr->udp.code, ntohs(pdu->transport_hdr->udp.id));
+
+       coap_transport_t transport = COAP_UDP;
+
+       buf_len = pdu->length + 1;
+       buf = (unsigned char *)malloc((size_t)(buf_len * sizeof(unsigned char)));
+
+       if (!buf) {
+               warn("coap_show_pdu : failed to create buf, len %d\n", buf_len);
+               return;
+       }
+
+       memset(buf, 0, buf_len);
+
+       transport = coap_get_tcp_header_type_from_size(pdu->length);
+
+       switch (transport) {
+       case COAP_UDP:
+               fprintf(COAP_DEBUG_FD, "v:%d t:%d tkl:%d c:%d id:%u\n",
+                               pdu->transport_hdr->udp.version,
+                               pdu->transport_hdr->udp.type,
+                               pdu->transport_hdr->udp.token_length,
+                               pdu->transport_hdr->udp.code,
+                               ntohs(pdu->transport_hdr->udp.id));
+               break;
+       case COAP_TCP:
+               fprintf(COAP_DEBUG_FD, "len:%d tkl:%d c:%d\n",
+                               ((pdu->transport_hdr->tcp.header_data[0] >> 4) & 0x0f),
+                               (pdu->transport_hdr->tcp.header_data[0] & 0x0f),
+                               pdu->transport_hdr->tcp.header_data[1]);
+               break;
+       case COAP_TCP_8BIT:
+               fprintf(COAP_DEBUG_FD, "len:%d tkl:%d extlen:%x c:%d\n",
+                               ((pdu->transport_hdr->tcp_8bit.header_data[0] >> 4) & 0x0f),
+                               (pdu->transport_hdr->tcp_8bit.header_data[0] & 0x0f),
+                               pdu->transport_hdr->tcp_8bit.header_data[1],
+                               pdu->transport_hdr->tcp_8bit.header_data[2]);
+               break;
+       case COAP_TCP_16BIT:
+               fprintf(COAP_DEBUG_FD, "len:%d tkl:%d extlen:%x%x c:%d\n",
+                               ((pdu->transport_hdr->tcp_16bit.header_data[0] >> 4) & 0x0f),
+                               (pdu->transport_hdr->tcp_16bit.header_data[0] & 0x0f),
+                               pdu->transport_hdr->tcp_16bit.header_data[1],
+                               pdu->transport_hdr->tcp_16bit.header_data[2],
+                               pdu->transport_hdr->tcp_16bit.header_data[3]);
+               break;
+       case COAP_TCP_32BIT:
+               fprintf(COAP_DEBUG_FD, "len:%d tkl:%d extlen:%x%x%x%x c:%d\n",
+                               ((pdu->transport_hdr->tcp_32bit.header_data[0] >> 4) & 0x0f),
+                               (pdu->transport_hdr->tcp_32bit.header_data[0] & 0x0f),
+                               pdu->transport_hdr->tcp_32bit.header_data[1],
+                               pdu->transport_hdr->tcp_32bit.header_data[2],
+                               pdu->transport_hdr->tcp_32bit.header_data[3],
+                               pdu->transport_hdr->tcp_32bit.header_data[4],
+                               pdu->transport_hdr->tcp_32bit.header_data[5]);
+               break;
+       default:
+               /* Should not enter here */
+               break;
+       }
 
        /* show options, if any */
-       coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
+       coap_option_iterator_init2((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
 
        while ((option = coap_option_next(&opt_iter))) {
                if (!have_options) {
@@ -280,10 +334,12 @@ void coap_show_pdu(const coap_pdu_t *pdu)
        }
 
        if (pdu->data) {
-               assert(pdu->data < (unsigned char *)pdu->hdr + pdu->length);
-               print_readable(pdu->data, (unsigned char *)pdu->hdr + pdu->length - pdu->data, buf, sizeof(buf), 0);
+               assert(pdu->data < (unsigned char *)pdu->transport_hdr + pdu->length);
+               print_readable(pdu->data, (unsigned char *)pdu->transport_hdr + pdu->length - pdu->data, buf, sizeof(buf), 0);
                fprintf(COAP_DEBUG_FD, " d:%s", buf);
        }
+
+       free(buf);
        fprintf(COAP_DEBUG_FD, "\n");
        fflush(COAP_DEBUG_FD);
 }
index 6489f99..cbe4401 100644 (file)
@@ -994,7 +994,7 @@ coap_pdu_t *coap_convert_to_tcp_pdu(coap_pdu_t *pdu)
                transport = coap_get_tcp_header_type_from_size((pdu->length - COAP_UDP_HEADER));
        }
 
-       if ((pdu->length - COAP_UDP_HEADER) < 0) {
+       if ((int)(pdu->length - COAP_UDP_HEADER) < 0) {
                newsize = 0;
        } else {
                newsize = (pdu->length - COAP_UDP_HEADER);