BUF_strndup: tidy
authorEmilia Kasper <emilia@openssl.org>
Thu, 17 Sep 2015 11:27:05 +0000 (13:27 +0200)
committerEmilia Kasper <emilia@openssl.org>
Tue, 22 Sep 2015 17:51:18 +0000 (19:51 +0200)
Fix comment, add another overflow check, tidy style

Reviewed-by: Matt Caswell <matt@openssl.org>
crypto/buffer/buf_str.c
include/openssl/buffer.h

index bca363c28e5b69c28795368771e394cd025a7e9f..1f3e8a4e798f2c234ed519baf1de8b3c8d301607 100644 (file)
@@ -73,7 +73,7 @@ size_t BUF_strnlen(const char *str, size_t maxlen)
 char *BUF_strdup(const char *str)
 {
     if (str == NULL)
-        return (NULL);
+        return NULL;
     return BUF_strndup(str, strlen(str));
 }
 
@@ -82,17 +82,17 @@ char *BUF_strndup(const char *str, size_t siz)
     char *ret;
 
     if (str == NULL)
-        return (NULL);
+        return NULL;
 
     siz = BUF_strnlen(str, siz);
 
     if (siz >= INT_MAX)
-        return (NULL);
+        return NULL;
 
     ret = OPENSSL_malloc(siz + 1);
     if (ret == NULL) {
         BUFerr(BUF_F_BUF_STRNDUP, ERR_R_MALLOC_FAILURE);
-        return (NULL);
+        return NULL;
     }
 
     memcpy(ret, str, siz);
@@ -105,13 +105,13 @@ void *BUF_memdup(const void *data, size_t siz)
 {
     void *ret;
 
-    if (data == NULL)
-        return (NULL);
+    if (data == NULL || siz >= INT_MAX)
+        return NULL;
 
     ret = OPENSSL_malloc(siz);
     if (ret == NULL) {
         BUFerr(BUF_F_BUF_MEMDUP, ERR_R_MALLOC_FAILURE);
-        return (NULL);
+        return NULL;
     }
     return memcpy(ret, data, siz);
 }
index 61cff9ca36e09a75101729a0489fd3f38efae739..f22ed09713484a9b17e07458848b1e3cc5f83384 100644 (file)
@@ -92,8 +92,8 @@ size_t BUF_strnlen(const char *str, size_t maxlen);
 char *BUF_strdup(const char *str);
 
 /*
- * Returns a pointer to a new string which is a duplicate of the string |str|,
- * but guarantees to never read past the first |siz| bytes of |str|.
+ * Like strndup, but in addition, explicitly guarantees to never read past the
+ * first |siz| bytes of |str|.
  */
 char *BUF_strndup(const char *str, size_t siz);