From b03a1252f3eb01a37f8b7b5d040f2b8cdd11b4ed Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 29 Feb 2012 23:55:35 -0600 Subject: [PATCH] Update: improve FORCING memory allocation strategy macro handling Do not alter the function disabling macros. Allow for forcing secure or insecure on individual blocks of code by removing the function disabling macros. The following should not be possible: int main(){ normal_allocations(); #define _f_memory_FORCE_secure_memory_ secure_allocations(); #undef _f_memory_FORCE_secure_memory_ do_stuff(); normal_deallocations(); #define _f_memory_FORCE_secure_memory_ secure_deallocations(); #undef _f_memory_FORCE_secure_memory_ return 0; } --- level_0/f_memory/c/memory.c | 16 ++++++++-------- level_0/f_memory/c/memory.h | 20 +++++++------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/level_0/f_memory/c/memory.c b/level_0/f_memory/c/memory.c index 45bf50a..cadd490 100644 --- a/level_0/f_memory/c/memory.c +++ b/level_0/f_memory/c/memory.c @@ -36,7 +36,7 @@ extern "C"{ } #endif // _di_f_new_ -#ifndef _di_f_delete_ +#if ! ( defined (_di_f_delete_) || defined (_f_memory_FORCE_secure_memory_) ) f_return_status f_delete(void **pointer, const f_memory_size_t type, const f_memory_length length){ #ifndef _di_level_0_parameter_checking_ if (pointer == 0) return f_invalid_parameter; @@ -52,9 +52,9 @@ extern "C"{ return f_none; } -#endif // _di_f_delete_ +#endif // ! ( defined (_di_f_delete_) || defined (_f_memory_FORCE_secure_memory_) ) -#ifndef _di_f_destroy_ +#if ! ( defined (_di_f_destroy_) || defined (_f_memory_FORCE_fast_memory_) ) f_return_status f_destroy(void **pointer, const f_memory_size_t type, const f_memory_length length){ #ifndef _di_level_0_parameter_checking_ if (length < 0) return f_invalid_parameter; @@ -76,9 +76,9 @@ extern "C"{ return f_none; } -#endif // _di_f_destroy_ +#endif // ! ( defined (_di_f_destroy_) || defined (_f_memory_FORCE_fast_memory_) ) -#ifndef _di_f_resize_ +#if ! ( defined (_di_f_resize_) || defined (_f_memory_FORCE_secure_memory_) ) f_return_status f_resize(void **pointer, const f_memory_size_t type, const f_memory_length old_length, const f_memory_length new_length){ #ifndef _di_level_0_parameter_checking_ if (type <= 0) return f_invalid_parameter; @@ -130,9 +130,9 @@ extern "C"{ return f_reallocation_error; } -#endif // _di_f_resize_ +#endif // ! ( defined (_di_f_resize_) || defined (_f_memory_FORCE_secure_memory_) ) -#ifndef _di_f_adjust_ +#if ! ( defined (_di_f_adjust_) || defined (_f_memory_FORCE_fast_memory_) ) f_return_status f_adjust(void **pointer, const f_memory_size_t type, const f_memory_length old_length, const f_memory_length new_length){ #ifndef _di_level_0_parameter_checking_ if (type <= 0) return f_invalid_parameter; @@ -192,7 +192,7 @@ extern "C"{ return f_reallocation_error; } -#endif // _di_f_adjust_ +#endif // ! ( defined (_di_f_adjust_) || defined (_f_memory_FORCE_fast_memory_) ) #ifdef __cplusplus } // extern "C" diff --git a/level_0/f_memory/c/memory.h b/level_0/f_memory/c/memory.h index 44cbebe..3fd2b24 100644 --- a/level_0/f_memory/c/memory.h +++ b/level_0/f_memory/c/memory.h @@ -38,10 +38,7 @@ extern "C"{ #endif #ifdef _f_memory_FORCE_secure_memory_ - #define _di_f_delete_ #define f_delete(the_pointer, the_type, the_length) f_destroy(the_pointer, the_type, the_length) - - #define _di_f_resize_ #define f_resize(the_pointer, the_type, the_old_length, the_new_length) f_adjust(the_pointer, the_type, the_old_length, the_new_length) #ifdef _f_memory_FORCE_fast_memory_ @@ -50,10 +47,7 @@ extern "C"{ #endif // _f_memory_FORCE_secure_memory_ #ifdef _f_memory_FORCE_fast_memory_ - #define _di_f_destroy_ #define f_destroy(the_pointer, the_type, the_length) f_delete(the_pointer, the_type, the_length) - - #define _di_f_adjust_ #define f_adjust(the_pointer, the_type, the_old_length, the_new_length) f_resize(the_pointer, the_type, the_old_length, the_new_length) #endif // _f_memory_FORCE_fast_memory_ @@ -77,26 +71,26 @@ extern "C"{ extern f_return_status f_new_array(void **pointer, const f_memory_size_t type, const f_memory_length length); #endif // _di_f_new_ -#ifndef _di_f_delete_ +#if ! ( defined (_di_f_delete_) || defined (_f_memory_FORCE_secure_memory_) ) // deletes some dynamically allocated data // f_delete, will not change any of the data to 0 prior to deallocation // type and length are not used by this function normally but must be provided for the cases when f_delete is swapped with f_destroy (or vice-versa) extern f_return_status f_delete(void **pointer, const f_memory_size_t type, const f_memory_length length); -#endif // _di_f_delete_ +#endif // ! ( defined (_di_f_delete_) || defined (_f_memory_FORCE_secure_memory_) ) -#ifndef _di_f_destroy_ +#if ! ( defined (_di_f_destroy_) || defined (_f_memory_FORCE_fast_memory_) ) // securely deletes some dynamically allocated data // f_destroy, will change all data to 0 prior to deallocation extern f_return_status f_destroy(void **pointer, const f_memory_size_t type, const f_memory_length length); -#endif // _di_f_destroy_ +#endif // ! ( defined (_di_f_destroy_) || defined (_f_memory_FORCE_fast_memory_) ) -#ifndef _di_f_resize_ +#if ! ( defined (_di_f_resize_) || defined (_f_memory_FORCE_secure_memory_) ) // resizes some dynamically allocated data // f_resize, will not change any of the data prior to deallocation extern f_return_status f_resize(void **pointer, const f_memory_size_t type, const f_memory_length old_length, const f_memory_length new_length); -#endif // _di_f_resize_ +#endif // ! ( defined (_di_f_resize_) || defined (_f_memory_FORCE_secure_memory_) ) -#ifndef _di_f_adjust_ +#if ! ( defined (_di_f_adjust_) || defined (_f_memory_FORCE_fast_memory_) ) // securely resizes some dynamically allocated data // f_adjust, will change all data to 0 prior to deallocation extern f_return_status f_adjust(void **pointer, const f_memory_size_t type, const f_memory_length old_length, const f_memory_length new_length); -- 1.8.3.1