From 66b29488a7ed5909564ed03b3e89cd0d008df09e Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Mon, 7 Sep 2009 21:10:22 +0200 Subject: Moved all malloc() operations over to a calloc wrapper, malloc_nullsafe() This also improves debugging as well, if debug logging is enabled and log level is >= 40. --- common/CMakeLists.txt | 1 + common/certinfo.c | 3 +- common/eurephia_nullsafe.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ common/eurephia_nullsafe.h | 19 ++++++++++++- common/eurephia_values.c | 4 +-- common/passwd.c | 12 +++----- 6 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 common/eurephia_nullsafe.c (limited to 'common') diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index f2e0223..3232e7e 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -43,6 +43,7 @@ ADD_LIBRARY( eurephiacommon STATIC certinfo.c eurephia_getsym.c eurephia_log.c + eurephia_nullsafe.c eurephia_values.c passwd.c randstr.c diff --git a/common/certinfo.c b/common/certinfo.c index c2601de..1a7d532 100644 --- a/common/certinfo.c +++ b/common/certinfo.c @@ -60,8 +60,7 @@ certinfo *parse_tlsid(const char *input) { if( (input == NULL) || strlen(input) < 5) return NULL; - ret = (certinfo *) malloc(sizeof(certinfo)+2); - bzero(ret, sizeof(certinfo)+2); + ret = (certinfo *) malloc_nullsafe(NULL, sizeof(certinfo)+2); bzero(&tmp, 130); mainp = strdup(input); diff --git a/common/eurephia_nullsafe.c b/common/eurephia_nullsafe.c new file mode 100644 index 0000000..92747e5 --- /dev/null +++ b/common/eurephia_nullsafe.c @@ -0,0 +1,70 @@ +/* eurephia_nullsafe.c + * + * standard C string functions, which is made NULL safe by checking + * if input value is NULL before performing the action. + * + * GPLv2 only - Copyright (C) 2009 + * David Sommerseth + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/** + * @file eurephia_nullsafe.c + * @author David Sommerseth + * @date 2009-09-07 + * + * @brief standard C string functions, which is made NULL safe by checking + * if input value is NULL before performing the action. + * + */ + +#include +#include + +#include +#include + +/** + * Internal function, called via the malloc_nullsafe() macro. + * + * @param ctx eurephiaCTX, used for logging + * @param sz size of the memory region being allocated + * @param file debug info, which file is doing this call + * @param line debug info, which line in the file + * + * @return Returns a void pointer to the memory region on success, otherwise NULL + */ +void *__malloc_nullsafe(eurephiaCTX *ctx, size_t sz, const char *file, int line) { + void *buf = NULL; + + buf = calloc(1, sz); /* Using calloc, also gives a zero'd memory region */ + if( !buf ) { + if( ctx ) { + eurephia_log(ctx, LOG_FATAL, 40, + "Could not allocate memory region for %ld bytes (File %s, line %i)", + sz, file, line); + } else { + fprintf(stderr, "** FATAL ERROR ** " + "Could not allocate memory region for %ld bytes (File %s, line %i)", + sz, file, line); + } + } else { + DEBUG(ctx, 40, "Allocated %ld bytes of memory on address %p (File %s, line %i)", + sz, buf, file, line); + } + return buf; +} diff --git a/common/eurephia_nullsafe.h b/common/eurephia_nullsafe.h index 55a9148..fde8c2f 100644 --- a/common/eurephia_nullsafe.h +++ b/common/eurephia_nullsafe.h @@ -3,7 +3,7 @@ * standard C string functions, which is made NULL safe by checking * if input value is NULL before performing the action. * - * GPLv2 only - Copyright (C) 2008 + * GPLv2 only - Copyright (C) 2008, 2009 * David Sommerseth * * This program is free software; you can redistribute it and/or @@ -32,6 +32,8 @@ * */ +#include + #ifndef EUREPHIA_NULLSAFE_H_ #define EUREPHIA_NULLSAFE_H_ @@ -66,6 +68,21 @@ #define strlen_nullsafe(str) (str != NULL ? strlen(str) : 0) + +void *__malloc_nullsafe(eurephiaCTX *, size_t, const char *, int); + +/** + * calloc() wrapper. Should replace the use of malloc() and memset(). This + * function uses calloc internally, which results in the memory region being zero'd + * by the kernel on allocation. + * + * @param ctx eurephiaCTX (used for debugg logging) + * @param size Size of the memory region wanted + * + * @return Returns a pointer to the memory region on success, otherwise NULL. + */ +#define malloc_nullsafe(ctx, sz) __malloc_nullsafe(ctx, sz, __FILE__, __LINE__) + /** * free() wrapper. Frees memory allocated by malloc() or calloc(). It also sets the * input pointer to NULL on success. diff --git a/common/eurephia_values.c b/common/eurephia_values.c index 3c7ac91..501b6ba 100644 --- a/common/eurephia_values.c +++ b/common/eurephia_values.c @@ -128,12 +128,10 @@ eurephiaVALUES *eCreate_value_space(eurephiaCTX *ctx, int evgid) DEBUG(ctx, 32, "Function call: eCreate_value_space(ctx, %i)", evgid); - ptr = (eurephiaVALUES *) malloc(sizeof(eurephiaVALUES) + 2); + ptr = (eurephiaVALUES *) malloc_nullsafe(ctx, sizeof(eurephiaVALUES) + 2); if( ptr == NULL ) { - eurephia_log(ctx, LOG_PANIC, 0, "Could not allocate memory for a new eurephiaVALUES struct"); return NULL; } - memset(ptr, 0, sizeof(eurephiaVALUES) + 2); ptr->evgid = evgid; return ptr; } diff --git a/common/passwd.c b/common/passwd.c index 3a887c7..9820988 100644 --- a/common/passwd.c +++ b/common/passwd.c @@ -157,9 +157,8 @@ int gen_randsaltstr(eurephiaCTX *ctx, char *saltstr, int len) { char *ptr = NULL; int i = 0; - rand = (unsigned char *) malloc(len+2); + rand = (unsigned char *) malloc_nullsafe(ctx, len+2); assert(rand != NULL ); - memset(rand, 0, len+2); if( !eurephia_randstring(ctx, rand, len) ) { return 0; } @@ -472,9 +471,8 @@ char *eurephia_pwd_crypt(eurephiaCTX *ctx, const char *key, const char *salt) { srand( (unsigned int) time(NULL) ); } - buffer = (char *) malloc(buflen); + buffer = (char *) malloc_nullsafe(ctx, buflen); assert(buffer != NULL); - memset(buffer, 0, buflen); // Get default max rounds for hashing if( maxrounds == 0 ) { @@ -556,8 +554,7 @@ char *eurephia_quick_hash(const char *salt, const char *pwd) { } if( salt != NULL ) { - tmp = (char *) malloc(strlen_nullsafe(salt) + len + 2); - memset(tmp, 0, strlen_nullsafe(salt) + len + 2); + tmp = (char *) malloc_nullsafe(NULL, strlen_nullsafe(salt) + len + 2); sprintf(tmp, "%s%s", pwd, salt); } else { tmp = strdup_nullsafe(pwd); @@ -570,8 +567,7 @@ char *eurephia_quick_hash(const char *salt, const char *pwd) { SHA512Final(&sha, sha_res); // Allocate memory for the return buffer - ret = (char *) malloc((SHA512_HASH_SIZE*2)+3); - memset(ret, 0,(SHA512_HASH_SIZE*2)+3); + ret = (char *) malloc_nullsafe(NULL, (SHA512_HASH_SIZE*2)+3); ptr = ret; // Generate a readable string of the hash -- cgit