Use safer sizeof variant in malloc

For a local variable:
        TYPE *p;
Allocations like this are "risky":
        p = OPENSSL_malloc(sizeof(TYPE));
if the type of p changes, and the malloc call isn't updated, you
could get memory corruption.  Instead do this:
        p = OPENSSL_malloc(sizeof(*p));
Also fixed a few memset() calls that I noticed while doing this.

Reviewed-by: Richard Levitte <levitte@openssl.org>
This commit is contained in:
Rich Salz
2015-05-01 23:10:31 -04:00
committed by Rich Salz
parent 8920a7cd04
commit b4faea50c3
142 changed files with 278 additions and 283 deletions

View File

@@ -455,8 +455,8 @@ static void build_SYS_str_reasons(void)
char (*dest)[LEN_SYS_STR_REASON] = &(strerror_tab[i - 1]);
char *src = strerror(i);
if (src != NULL) {
strncpy(*dest, src, sizeof *dest);
(*dest)[sizeof *dest - 1] = '\0';
strncpy(*dest, src, sizeof(*dest));
(*dest)[sizeof(*dest) - 1] = '\0';
str->string = *dest;
}
}
@@ -882,7 +882,7 @@ ERR_STATE *ERR_get_state(void)
/* ret == the error state, if NULL, make a new one */
if (ret == NULL) {
ret = OPENSSL_malloc(sizeof(ERR_STATE));
ret = OPENSSL_malloc(sizeof(*ret));
if (ret == NULL)
return (&fallback);
CRYPTO_THREADID_cpy(&ret->tid, &tid);