BUF_strndup: tidy

Fix comment, add another overflow check, tidy style

Reviewed-by: Matt Caswell <matt@openssl.org>
(cherry picked from commit de8883e11b)
(cherry picked from commit f5afe9ce3f)
This commit is contained in:
Emilia Kasper 2015-09-17 13:27:05 +02:00
parent 6905187488
commit e56c77b8ee
2 changed files with 9 additions and 9 deletions

View File

@ -64,7 +64,7 @@
char *BUF_strdup(const char *str) char *BUF_strdup(const char *str)
{ {
if (str == NULL) if (str == NULL)
return (NULL); return NULL;
return BUF_strndup(str, strlen(str)); return BUF_strndup(str, strlen(str));
} }
@ -73,15 +73,15 @@ char *BUF_strndup(const char *str, size_t siz)
char *ret; char *ret;
if (str == NULL) if (str == NULL)
return (NULL); return NULL;
if (siz >= INT_MAX) if (siz >= INT_MAX)
return (NULL); return NULL;
ret = OPENSSL_malloc(siz + 1); ret = OPENSSL_malloc(siz + 1);
if (ret == NULL) { if (ret == NULL) {
BUFerr(BUF_F_BUF_STRNDUP, ERR_R_MALLOC_FAILURE); BUFerr(BUF_F_BUF_STRNDUP, ERR_R_MALLOC_FAILURE);
return (NULL); return NULL;
} }
memcpy(ret, str, siz); memcpy(ret, str, siz);
@ -94,13 +94,13 @@ void *BUF_memdup(const void *data, size_t siz)
{ {
void *ret; void *ret;
if (data == NULL) if (data == NULL || siz >= INT_MAX)
return (NULL); return NULL;
ret = OPENSSL_malloc(siz); ret = OPENSSL_malloc(siz);
if (ret == NULL) { if (ret == NULL) {
BUFerr(BUF_F_BUF_MEMDUP, ERR_R_MALLOC_FAILURE); BUFerr(BUF_F_BUF_MEMDUP, ERR_R_MALLOC_FAILURE);
return (NULL); return NULL;
} }
return memcpy(ret, data, siz); return memcpy(ret, data, siz);
} }

View File

@ -87,8 +87,8 @@ int BUF_MEM_grow_clean(BUF_MEM *str, size_t len);
char *BUF_strdup(const char *str); char *BUF_strdup(const char *str);
/* /*
* Returns a pointer to a new string which is a duplicate of the string |str|, * Like strndup, but in addition, explicitly guarantees to never read past the
* but guarantees to never read past the first |siz| bytes of |str|. * first |siz| bytes of |str|.
*/ */
char *BUF_strndup(const char *str, size_t siz); char *BUF_strndup(const char *str, size_t siz);