[IOT-2089] Realloc NULL checks added
authorol.beketov <ol.beketov@samsung.com>
Tue, 16 May 2017 15:32:08 +0000 (18:32 +0300)
committerRandeep Singh <randeep.s@samsung.com>
Thu, 18 May 2017 01:15:27 +0000 (01:15 +0000)
Change-Id: Ie1b99a3815c8126e35576d4c3daa91c73ad66c6d
Signed-off-by: ol.beketov <ol.beketov@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/19663
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Dmitriy Zhuravlev <d.zhuravlev@samsung.com>
Reviewed-by: Andrii Shtompel <a.shtompel@samsung.com>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
Reviewed-by: dongik Lee <dongik.lee@samsung.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
resource/csdk/security/src/credresource.c
resource/csdk/security/src/crlresource.c

index 5589430..bbb58ea 100644 (file)
@@ -3526,13 +3526,16 @@ void GetDerKey(ByteArray_t * key, const char * usage)
                     return;
                 }
 
-                key->data = OICRealloc(key->data, ctx.buflen);
-                if (NULL == key->data)
+                uint8_t *tmp = OICRealloc(key->data, ctx.buflen);
+                if (NULL == tmp)
                 {
+                    OICFree(key->data);
+                    key->data = NULL;
                     OIC_LOG(ERROR, TAG, "Failed to allocate memory");
                     mbedtls_pem_free(&ctx);
                     return;
                 }
+                key->data = tmp;
 
                 memcpy(key->data, ctx.buf, ctx.buflen);
                 key->len = ctx.buflen;
@@ -3541,7 +3544,15 @@ void GetDerKey(ByteArray_t * key, const char * usage)
             }
             else if(temp->privateData.encoding == OIC_ENCODING_DER)
             {
-                key->data = OICRealloc(key->data, key->len + temp->privateData.len);
+                uint8_t *tmp = OICRealloc(key->data, key->len + temp->privateData.len);
+                if (NULL == tmp)
+                {
+                    OICFree(key->data);
+                    key->data = NULL;
+                    OIC_LOG(ERROR, TAG, "Failed to allocate memory");
+                    return;
+                }
+                key->data = tmp;
                 memcpy(key->data + key->len, temp->privateData.data, temp->privateData.len);
                 key->len += temp->privateData.len;
                 OIC_LOG_V(DEBUG, TAG, "Key for %s found", usage);
index 9848dcf..fbb3032 100644 (file)
@@ -806,14 +806,17 @@ void GetDerCrl(ByteArray_t* out)
 
     out->len = 0;
 
-    out->data = OICRealloc(out->data, crl->len);
-    if (out->data)
+    uint8_t *tmp = OICRealloc(out->data, crl->len);
+    if (tmp)
     {
+        out->data = tmp;
         memcpy(out->data, crl->data, crl->len);
         out->len = crl->len;
     }
     else
     {
+        OICFree(out->data);
+        out->data = NULL;
         OIC_LOG(ERROR, TAG, "Can't allocate memory for out->data");
     }
     DeleteCrl(crlRes);