realloc of NULL is like malloc

ANSI C, and OpenSSL's malloc wrapper do this, also.

Reviewed-by: Richard Levitte <levitte@openssl.org>
This commit is contained in:
Rich Salz 2015-04-28 16:34:52 -04:00
parent b196e7d936
commit 2d29e2df0c
8 changed files with 11 additions and 36 deletions

View file

@ -205,10 +205,7 @@ int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
if ((a->length < (w + 1)) || (a->data == NULL)) {
if (!value)
return (1); /* Don't need to set */
if (a->data == NULL)
c = OPENSSL_malloc(w + 1);
else
c = OPENSSL_realloc_clean(a->data, a->length, w + 1);
c = OPENSSL_realloc_clean(a->data, a->length, w + 1);
if (c == NULL) {
ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
return 0;

View file

@ -317,11 +317,7 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
}
if ((str->length < len) || (str->data == NULL)) {
c = str->data;
if (c == NULL)
str->data = OPENSSL_malloc(len + 1);
else
str->data = OPENSSL_realloc(c, len + 1);
str->data = OPENSSL_realloc(c, len + 1);
if (str->data == NULL) {
ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
str->data = c;

View file

@ -151,10 +151,7 @@ int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
}
i /= 2;
if (num + i > slen) {
if (s == NULL)
sp = OPENSSL_malloc((unsigned int)num + i * 2);
else
sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
if (sp == NULL) {
ASN1err(ASN1_F_A2I_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE);
if (s != NULL)

View file

@ -165,10 +165,7 @@ int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)
}
i /= 2;
if (num + i > slen) {
if (s == NULL)
sp = OPENSSL_malloc((unsigned int)num + i * 2);
else
sp = OPENSSL_realloc_clean(s, slen, num + i * 2);
sp = OPENSSL_realloc_clean(s, slen, num + i * 2);
if (sp == NULL) {
ASN1err(ASN1_F_A2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
if (s != NULL)

View file

@ -157,10 +157,7 @@ int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
}
i /= 2;
if (num + i > slen) {
if (s == NULL)
sp = OPENSSL_malloc((unsigned int)num + i * 2);
else
sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
if (sp == NULL) {
ASN1err(ASN1_F_A2I_ASN1_STRING, ERR_R_MALLOC_FAILURE);
if (s != NULL)

View file

@ -673,12 +673,9 @@ int BIO_accept(int sock, char **addr)
break;
nl = strlen(h) + strlen(s) + 2;
p = *addr;
if (p) {
if (p)
*p = '\0';
p = OPENSSL_realloc(p, nl);
} else {
p = OPENSSL_malloc(nl);
}
p = OPENSSL_realloc(p, nl);
if (p == NULL) {
BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
goto end;

View file

@ -114,10 +114,7 @@ size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
return 0;
}
n = (len + 3) / 3 * 4;
if (str->data == NULL)
ret = OPENSSL_malloc(n);
else
ret = OPENSSL_realloc(str->data, n);
ret = OPENSSL_realloc(str->data, n);
if (ret == NULL) {
BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);
len = 0;
@ -151,10 +148,7 @@ size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
return 0;
}
n = (len + 3) / 3 * 4;
if (str->data == NULL)
ret = OPENSSL_malloc(n);
else
ret = OPENSSL_realloc_clean(str->data, str->max, n);
ret = OPENSSL_realloc_clean(str->data, str->max, n);
if (ret == NULL) {
BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);
len = 0;

View file

@ -969,8 +969,8 @@ void ERR_add_error_vdata(int num, va_list args)
if (p == NULL) {
OPENSSL_free(str);
return;
} else
str = p;
}
str = p;
}
BUF_strlcat(str, a, (size_t)s + 1);
}