@ -941,7 +941,7 @@ static inline int cli_getpagesize(void)
/**
* @ brief Wrapper around malloc that limits how much may be allocated to CLI_MAX_ALLOCATION .
*
* Please use CLI_MAX_MALLOC ( ) with ` goto done ; ` error handling instead .
* Please use CLI_MAX_MALLOC_OR_GOTO_DONE ( ) with ` goto done ; ` error handling instead .
*
* @ param ptr
* @ param size
@ -950,9 +950,9 @@ static inline int cli_getpagesize(void)
void * cli_max_malloc ( size_t nmemb ) ;
/**
* @ brief Wrapper around C alloc that limits how much may be allocated to CLI_MAX_ALLOCATION .
* @ brief Wrapper around c alloc that limits how much may be allocated to CLI_MAX_ALLOCATION .
*
* Please use CLI_MAX_CALLOC ( ) with ` goto done ; ` error handling instead .
* Please use CLI_MAX_CALLOC_OR_GOTO_DONE ( ) with ` goto done ; ` error handling instead .
*
* @ param ptr
* @ param size
@ -963,7 +963,9 @@ void *cli_max_calloc(size_t nmemb, size_t size);
/**
* @ brief Wrapper around realloc that limits how much may be allocated to CLI_MAX_ALLOCATION .
*
* Please use CLI_MAX_REALLOC ( ) with ` goto done ; ` error handling instead .
* Please use CLI_MAX_REALLOC_OR_GOTO_DONE ( ) with ` goto done ; ` error handling instead .
*
* NOTE : cli_max_realloc ( ) will NOT free ptr if size = = 0. It is safe to free ptr after ` done : ` .
*
* IMPORTANT : This differs from realloc ( ) in that if size = = 0 , it will NOT free the ptr .
*
@ -976,7 +978,7 @@ void *cli_max_realloc(void *ptr, size_t size);
/**
* @ brief Wrapper around realloc that limits how much may be allocated to CLI_MAX_ALLOCATION .
*
* Please use CLI_MAX_REALLOC ( ) with ` goto done ; ` error handling instead .
* Please use CLI_MAX_REALLOC_OR_GOTO_DONE ( ) with ` goto done ; ` error handling instead .
*
* IMPORTANT : This differs from realloc ( ) in that if size = = 0 , it will NOT free the ptr .
*
@ -987,12 +989,12 @@ void *cli_max_realloc(void *ptr, size_t size);
* @ param size
* @ return void *
*/
void * cli_max_realloc2 ( void * ptr , size_t size ) ;
void * cli_max_realloc_or_free ( void * ptr , size_t size ) ;
/**
* @ brief Wrapper around realloc that , unlike some variants of realloc , will not free the ptr if size = = 0.
*
* Please use CLI_MAX_REALLOC ( ) with ` goto done ; ` error handling instead .
* Please use CLI_MAX_REALLOC_OR_GOTO_DONE ( ) with ` goto done ; ` error handling instead .
*
* IMPORTANT : This differs from realloc ( ) in that if size = = 0 , it will NOT free the ptr .
*
@ -1005,7 +1007,7 @@ void *cli_safer_realloc(void *ptr, size_t size);
/**
* @ brief Wrapper around realloc that , unlike some variants of realloc , will not free the ptr if size = = 0.
*
* Please use CLI_SAFER_REALLOC ( ) with ` goto done ; ` error handling instead .
* Please use CLI_SAFER_REALLOC_OR_GOTO_DONE ( ) with ` goto done ; ` error handling instead .
*
* IMPORTANT : This differs from realloc ( ) in that if size = = 0 , it will NOT free the ptr .
*
@ -1016,15 +1018,17 @@ void *cli_safer_realloc(void *ptr, size_t size);
* @ param size
* @ return void *
*/
void * cli_safer_realloc2 ( void * ptr , size_t size ) ;
void * cli_safer_realloc_or_free ( void * ptr , size_t size ) ;
/**
* @ brief Wrapper around strdup that does a NULL check .
*
* Please use CLI_STRDUP_OR_GOTO_DONE ( ) with ` goto done ; ` error handling instead .
*
* @ param s
* @ return char * Returns the allocated string or NULL if allocation failed . This includes if allocation fails because s = = NULL .
*/
char * cli_strdup ( const char * s ) ;
char * cli_safer_s trdup ( const char * s ) ;
int cli_rmdirs ( const char * dirname ) ;
char * cli_hashstream ( FILE * fs , unsigned char * digcpy , int type ) ;
@ -1298,103 +1302,150 @@ uint8_t cli_get_debug_flag(void);
*/
uint8_t cli_set_debug_flag ( uint8_t debug_flag ) ;
# ifndef STRDUP
# define STRDUP(buf, var, ...) \
do { \
var = strdup ( buf ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
} while ( 0 )
# endif
# ifndef CLI_STRDUP
# define CLI_STRDUP(buf, var, ...) \
do { \
var = cli_strdup ( buf ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
# ifndef CLI_SAFER_STRDUP_OR_GOTO_DONE
/**
* @ brief Wrapper around strdup that does a NULL check .
*
* This macro requires ` goto done ; ` error handling .
*
* @ param buf The string to duplicate .
* @ param var The variable to assign the allocated string to .
* @ param . . . The error handling code to execute if the allocation fails .
*/
# define CLI_SAFER_STRDUP_OR_GOTO_DONE(buf, var, ...) \
do { \
var = cli_safer_strdup ( buf ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
} while ( 0 )
# endif
# ifndef FREE
# define FREE(var) \
do { \
if ( NULL ! = var ) { \
free ( var ) ; \
var = NULL ; \
} \
# ifndef CLI_FREE_AND_SET_NULL
/**
* @ brief Wrapper around ` free ( ) ` to ensure you reset the variable to NULL so as to prevent a double - free .
*
* @ param var The variable to free and set to NULL .
*/
# define CLI_FREE_AND_SET_NULL(var) \
do { \
if ( NULL ! = var ) { \
free ( var ) ; \
var = NULL ; \
} \
} while ( 0 )
# endif
# ifndef MALLOC
# define MALLOC(var, size, ...) \
do { \
var = malloc ( size ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
# ifndef CLI_MALLOC_OR_GOTO_DONE
/**
* @ brief Wrapper around malloc that will ` goto done ; ` if the allocation fails .
*
* This macro requires ` goto done ; ` error handling .
*
* @ param ptr The variable to assign the allocated memory to .
* @ param size The size of the memory to allocate .
* @ param . . . The error handling code to execute if the allocation fails .
*/
# define CLI_MALLOC_OR_GOTO_DONE(var, size, ...) \
do { \
var = malloc ( size ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
} while ( 0 )
# endif
# ifndef CLI_MAX_MALLOC
# define CLI_MAX_MALLOC(var, size, ...) \
do { \
var = cli_max_malloc ( size ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
# ifndef CLI_MAX_MALLOC_OR_GOTO_DONE
/**
* @ brief Wrapper around malloc that limits how much may be allocated to CLI_MAX_ALLOCATION .
*
* This macro requires ` goto done ; ` error handling .
*
* @ param var The variable to assign the allocated memory to .
* @ param size The size of the memory to allocate .
* @ param . . . The error handling code to execute if the allocation fails .
*/
# define CLI_MAX_MALLOC_OR_GOTO_DONE(var, size, ...) \
do { \
var = cli_max_malloc ( size ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
} while ( 0 )
# endif
# ifndef CALLOC
# define CALLOC(var, nmemb, size, ...) \
do { \
( var ) = calloc ( nmemb , size ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
# ifndef CLI_CALLOC_OR_GOTO_DONE
/**
* @ brief Wrapper around calloc that will ` goto done ; ` if the allocation fails .
*
* This macro requires ` goto done ; ` error handling .
*
* @ param var The variable to assign the allocated memory to .
* @ param nmemb The number of elements to allocate .
* @ param size The size of each element .
* @ param . . . The error handling code to execute if the allocation fails .
*/
# define CLI_CALLOC_OR_GOTO_DONE(var, nmemb, size, ...) \
do { \
( var ) = calloc ( nmemb , size ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
} while ( 0 )
# endif
# ifndef CLI_MAX_CALLOC
# define CLI_MAX_CALLOC(var, nmemb, size, ...) \
do { \
( var ) = cli_max_calloc ( nmemb , size ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
# ifndef CLI_MAX_CALLOC_OR_GOTO_DONE
/**
* @ brief Wrapper around calloc that limits how much may be allocated to CLI_MAX_ALLOCATION .
*
* This macro requires ` goto done ; ` error handling .
*
* @ param var The variable to assign the allocated memory to .
* @ param nmemb The number of elements to allocate .
* @ param size The size of each element .
* @ param . . . The error handling code to execute if the allocation fails .
*/
# define CLI_MAX_CALLOC_OR_GOTO_DONE(var, nmemb, size, ...) \
do { \
( var ) = cli_max_calloc ( nmemb , size ) ; \
if ( NULL = = var ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
} while ( 0 )
# endif
# ifndef VERIFY_POINTER
# define VERIFY_POINTER(ptr, ...) \
do { \
if ( NULL = = ptr ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
# ifndef CLI_VERIFY_POINTER_OR_GOTO_DONE
/**
* @ brief Wrapper around a NULL - check that will ` goto done ; ` if the pointer is NULL .
*
* This macro requires ` goto done ; ` error handling .
*
* @ param ptr The pointer to verify .
* @ param . . . The error handling code to execute if the pointer is NULL .
*/
# define CLI_VERIFY_POINTER_OR_GOTO_DONE(ptr, ...) \
do { \
if ( NULL = = ptr ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
} while ( 0 )
# endif
@ -1405,21 +1456,21 @@ uint8_t cli_set_debug_flag(uint8_t debug_flag);
*
* NOTE : cli_max_realloc ( ) will NOT free ptr if size = = 0. It is safe to free ptr after ` done : ` .
*
* @ param ptr
* @ param size
* @ return void *
* @ param ptr The pointer to realloc .
* @ param size The size of the memory to allocate .
* @ param . . . The error handling code to execute if the allocation fails .
*/
# ifndef CLI_MAX_REALLOC
# define CLI_MAX_REALLOC(ptr, size, ...) \
do { \
void * vTmp = cli_max_realloc ( ptr , size ) ; \
if ( NULL = = vTmp ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
ptr = vTmp ; \
# ifndef CLI_MAX_REALLOC_OR_GOTO_DONE
# define CLI_MAX_REALLOC_OR_GOTO_DONE (ptr, size, ...) \
do { \
void * vTmp = cli_max_realloc ( ptr , size ) ; \
if ( NULL = = vTmp ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
ptr = vTmp ; \
} while ( 0 )
# endif
@ -1430,21 +1481,21 @@ uint8_t cli_set_debug_flag(uint8_t debug_flag);
*
* NOTE : cli_safer_realloc ( ) will NOT free ptr if size = = 0. It is safe to free ptr after ` done : ` .
*
* @ param ptr
* @ param size
* @ return void *
* @ param ptr The pointer to realloc .
* @ param size The size of the memory to allocate .
* @ param . . . The error handling code to execute if the allocation fails .
*/
# ifndef CLI_SAFER_REALLOC
# define CLI_SAFER_REALLOC(ptr, size, ...) \
do { \
void * vTmp = cli_safer_realloc ( ptr , size ) ; \
if ( NULL = = vTmp ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
ptr = vTmp ; \
# ifndef CLI_SAFER_REALLOC_OR_GOTO_DONE
# define CLI_SAFER_REALLOC_OR_GOTO_DONE (ptr, size, ...) \
do { \
void * vTmp = cli_safer_realloc ( ptr , size ) ; \
if ( NULL = = vTmp ) { \
do { \
__VA_ARGS__ ; \
} while ( 0 ) ; \
goto done ; \
} \
ptr = vTmp ; \
} while ( 0 )
# endif