Commit b4faea50 authored by Rich Salz's avatar Rich Salz Committed by Rich Salz
Browse files

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: default avatarRichard Levitte <levitte@openssl.org>
parent 8920a7cd
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -180,7 +180,7 @@ int chopup_args(ARGS *arg, char *buf)
    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 @@ int chopup_args(ARGS *arg, char *buf)
        /* 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 @@ CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
        }
    }

    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 @@ static int WIN32_rename(const char *from, const char *to)
    } 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;
+2 −2
Original line number Diff line number Diff line
@@ -1970,7 +1970,7 @@ static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
    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 @@ static int do_revoke(X509 *x509, CA_DB *db, int type, char *value)
        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;
+1 −1
Original line number Diff line number Diff line
@@ -570,7 +570,7 @@ int cms_main(int argc, char **argv)
            }
            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;
+1 −1
Original line number Diff line number Diff line
@@ -232,7 +232,7 @@ int ecparam_main(int argc, char **argv)
        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;
+1 −1
Original line number Diff line number Diff line
@@ -804,7 +804,7 @@ static LHASH_OF(FUNCTION) *prog_init(void)

    /* 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);
Loading