From b4faea50c35d92a67d1369355b49cc3efba78406 Mon Sep 17 00:00:00 2001 From: Rich Salz Date: Fri, 1 May 2015 23:10:31 -0400 Subject: 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 --- crypto/store/str_lib.c | 13 +++++++------ crypto/store/str_mem.c | 2 +- crypto/store/str_meth.c | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) (limited to 'crypto/store') diff --git a/crypto/store/str_lib.c b/crypto/store/str_lib.c index 1a4f237f06..fef7111f87 100644 --- a/crypto/store/str_lib.c +++ b/crypto/store/str_lib.c @@ -109,7 +109,7 @@ STORE *STORE_new_method(const STORE_METHOD *method) return NULL; } - ret = OPENSSL_malloc(sizeof(STORE)); + ret = OPENSSL_malloc(sizeof(*ret)); if (ret == NULL) { STOREerr(STORE_F_STORE_NEW_METHOD, ERR_R_MALLOC_FAILURE); return NULL; @@ -1156,9 +1156,9 @@ int STORE_delete_arbitrary(STORE *s, OPENSSL_ITEM attributes[], STORE_OBJECT *STORE_OBJECT_new(void) { - STORE_OBJECT *object = OPENSSL_malloc(sizeof(STORE_OBJECT)); + STORE_OBJECT *object = OPENSSL_malloc(sizeof(*object)); if (object) - memset(object, 0, sizeof(STORE_OBJECT)); + memset(object, 0, sizeof(*object)); return object; } @@ -1206,7 +1206,9 @@ struct STORE_attr_info_st { STORE_ATTR_INFO *STORE_ATTR_INFO_new(void) { - return OPENSSL_malloc(sizeof(STORE_ATTR_INFO)); + STORE_ATTR_INFO *p = OPENSSL_malloc(sizeof(*p)); + + return p; } static void STORE_ATTR_INFO_attr_free(STORE_ATTR_INFO *attrs, @@ -1450,8 +1452,7 @@ struct attr_list_ctx_st { void *STORE_parse_attrs_start(OPENSSL_ITEM *attributes) { if (attributes) { - struct attr_list_ctx_st *context = (struct attr_list_ctx_st *) - OPENSSL_malloc(sizeof(struct attr_list_ctx_st)); + struct attr_list_ctx_st *context = OPENSSL_malloc(sizeof(*context)); if (context) context->attributes = attributes; else diff --git a/crypto/store/str_mem.c b/crypto/store/str_mem.c index f1cca6a4e0..632ada89ba 100644 --- a/crypto/store/str_mem.c +++ b/crypto/store/str_mem.c @@ -244,7 +244,7 @@ static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type, OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]) { - struct mem_ctx_st *context = OPENSSL_malloc(sizeof(struct mem_ctx_st)); + struct mem_ctx_st *context = OPENSSL_malloc(sizeof(*context)); void *attribute_context = NULL; STORE_ATTR_INFO *attrs = NULL; diff --git a/crypto/store/str_meth.c b/crypto/store/str_meth.c index 781b1603e3..74878197f9 100644 --- a/crypto/store/str_meth.c +++ b/crypto/store/str_meth.c @@ -63,7 +63,7 @@ STORE_METHOD *STORE_create_method(char *name) { - STORE_METHOD *store_method = OPENSSL_malloc(sizeof(STORE_METHOD)); + STORE_METHOD *store_method = OPENSSL_malloc(sizeof(*store_method)); if (store_method) { memset(store_method, 0, sizeof(*store_method)); -- cgit v1.2.3