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>
diff --git a/apps/apps.c b/apps/apps.c
index 1e2970a..797e250 100644
--- a/apps/apps.c
+++ b/apps/apps.c
@@ -180,7 +180,7 @@
arg->argc = 0;
if (arg->size == 0) {
arg->size = 20;
- arg->argv = app_malloc(sizeof(char *) * arg->size, "argv space");
+ arg->argv = app_malloc(sizeof(*arg->argv) * arg->size, "argv space");
if (arg->argv == NULL)
return 0;
}
@@ -195,7 +195,8 @@
/* The start of something good :-) */
if (arg->argc >= arg->size) {
arg->size += 20;
- arg->argv = OPENSSL_realloc(arg->argv, sizeof(char *) * arg->size);
+ arg->argv = OPENSSL_realloc(arg->argv,
+ sizeof(*arg->argv) * arg->size);
if (arg->argv == NULL)
return 0;
}
@@ -1585,7 +1586,7 @@
}
}
- retdb = app_malloc(sizeof *retdb, "new DB");
+ retdb = app_malloc(sizeof(*retdb), "new DB");
retdb->db = tmpdb;
tmpdb = NULL;
if (db_attr)
@@ -2364,7 +2365,7 @@
} else { /* UNICODE path */
size_t i, flen = strlen(from) + 1, tlen = strlen(to) + 1;
- tfrom = (TCHAR *)malloc(sizeof(TCHAR) * (flen + tlen));
+ tfrom = malloc(*sizeof(*tfrom) * (flen + tlen));
if (tfrom == NULL)
goto err;
tto = tfrom + flen;
diff --git a/apps/ca.c b/apps/ca.c
index b6cf47f..abce534 100644
--- a/apps/ca.c
+++ b/apps/ca.c
@@ -1970,7 +1970,7 @@
row[DB_type][0] = 'V';
row[DB_type][1] = '\0';
- irow = app_malloc(sizeof(char *) * (DB_NUMBER + 1), "row space");
+ irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row space");
for (i = 0; i < DB_NUMBER; i++) {
irow[i] = row[i];
row[i] = NULL;
@@ -2207,7 +2207,7 @@
row[DB_type][0] = 'V';
row[DB_type][1] = '\0';
- irow = app_malloc(sizeof(char *) * (DB_NUMBER + 1), "row ptr");
+ irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row ptr");
for (i = 0; i < DB_NUMBER; i++) {
irow[i] = row[i];
row[i] = NULL;
diff --git a/apps/cms.c b/apps/cms.c
index 79d0b8f..5eb5d2d 100644
--- a/apps/cms.c
+++ b/apps/cms.c
@@ -570,7 +570,7 @@
}
if (key_param == NULL || key_param->idx != keyidx) {
cms_key_param *nparam;
- nparam = app_malloc(sizeof *nparam, "key param buffer");
+ nparam = app_malloc(sizeof(*nparam), "key param buffer");
nparam->idx = keyidx;
if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)
goto end;
diff --git a/apps/ecparam.c b/apps/ecparam.c
index dd0e8f5..5ceaec7 100644
--- a/apps/ecparam.c
+++ b/apps/ecparam.c
@@ -232,7 +232,7 @@
size_t crv_len = EC_get_builtin_curves(NULL, 0);
size_t n;
- curves = app_malloc((int)(sizeof *curves * crv_len), "list curves");
+ curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
if (!EC_get_builtin_curves(curves, crv_len)) {
OPENSSL_free(curves);
goto end;
diff --git a/apps/openssl.c b/apps/openssl.c
index b71f3d1..fa3b683 100644
--- a/apps/openssl.c
+++ b/apps/openssl.c
@@ -804,7 +804,7 @@
/* Sort alphabetically within category. For nicer help displays. */
for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
- qsort(functions, i, sizeof *functions, SortFnByName);
+ qsort(functions, i, sizeof(*functions), SortFnByName);
if ((ret = lh_FUNCTION_new()) == NULL)
return (NULL);
diff --git a/apps/s_cb.c b/apps/s_cb.c
index 13a3a25..d371cc9 100644
--- a/apps/s_cb.c
+++ b/apps/s_cb.c
@@ -1173,7 +1173,7 @@
static int ssl_excert_prepend(SSL_EXCERT **pexc)
{
- SSL_EXCERT *exc = app_malloc(sizeof *exc, "prepend cert");
+ SSL_EXCERT *exc = app_malloc(sizeof(*exc), "prepend cert");
exc->certfile = NULL;
exc->keyfile = NULL;
diff --git a/apps/s_server.c b/apps/s_server.c
index fead620..c1b799f 100644
--- a/apps/s_server.c
+++ b/apps/s_server.c
@@ -461,7 +461,7 @@
{
EBCDIC_OUTBUFF *wbuf;
- wbuf = app_malloc(sizeof(EBCDIC_OUTBUFF) + 1024, "ebcdef wbuf");
+ wbuf = app_malloc(sizeof(*wbuf) + 1024, "ebcdic wbuf");
wbuf->alloced = 1024;
wbuf->buff[0] = '\0';
@@ -515,7 +515,7 @@
num = num + num; /* double the size */
if (num < inl)
num = inl;
- wbuf = app_malloc(sizeof(EBCDIC_OUTBUFF) + num, "grow ebcdic wbuf");
+ wbuf = app_malloc(sizeof(*wbuf) + num, "grow ebcdic wbuf");
OPENSSL_free(b->ptr);
wbuf->alloced = num;
@@ -3127,7 +3127,7 @@
static int add_session(SSL *ssl, SSL_SESSION *session)
{
- simple_ssl_session *sess = app_malloc(sizeof *sess, "get session");
+ simple_ssl_session *sess = app_malloc(sizeof(*sess), "get session");
unsigned char *p;
SSL_SESSION_get_id(session, &sess->idlen);
diff --git a/apps/speed.c b/apps/speed.c
index 7a69485..00c7c41 100644
--- a/apps/speed.c
+++ b/apps/speed.c
@@ -2283,7 +2283,7 @@
int *fds;
static char sep[] = ":";
- fds = malloc(multi * sizeof *fds);
+ fds = malloc(sizeof(*fds) * multi);
for (n = 0; n < multi; ++n) {
if (pipe(fd) == -1) {
fprintf(stderr, "pipe failure\n");
diff --git a/apps/srp.c b/apps/srp.c
index 8b44780..c7a93cf 100644
--- a/apps/srp.c
+++ b/apps/srp.c
@@ -138,7 +138,7 @@
char **irow;
int i;
- irow = app_malloc(sizeof(char *) * (DB_NUMBER + 1), "row pointers");
+ irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");
for (i = 0; i < DB_NUMBER; i++) {
irow[i] = row[i];
row[i] = NULL;
diff --git a/apps/vms_decc_init.c b/apps/vms_decc_init.c
index 3ec7b54..2b273ac 100644
--- a/apps/vms_decc_init.c
+++ b/apps/vms_decc_init.c
@@ -130,7 +130,7 @@
*/
int i, count = *argc;
- char **newargv = app_malloc((count + 1) * sizeof *newargv, "argv copy");
+ char **newargv = app_malloc(sizeof(*newargv) * (count + 1), "argv copy");
for (i = 0; i < count; i++)
newargv[i] = argv[i];