X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=miscutil.c;h=4a9b88f384913af5c70dd7f216e17491eec05ee6;hp=aebd55f3973b5b4ee5b33c40a868707a2242097d;hb=361348bc7e87795de686e7d40996513d52fa606c;hpb=da9616d7948038f2e5f5b50ef6ce7d1a12835413 diff --git a/miscutil.c b/miscutil.c index aebd55f3..4a9b88f3 100644 --- a/miscutil.c +++ b/miscutil.c @@ -1,4 +1,4 @@ -const char miscutil_rcs[] = "$Id: miscutil.c,v 1.79 2015/08/12 10:34:38 fabiankeil Exp $"; +const char miscutil_rcs[] = "$Id: miscutil.c,v 1.82 2016/07/23 23:05:15 ler762 Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $ @@ -93,6 +93,40 @@ void *zalloc(size_t size) } +/********************************************************************* + * + * Function : zalloc_or_die + * + * Description : zalloc wrapper that either succeeds or causes + * program termination. + * + * Useful in situations were the string length is + * "small" and zalloc() failures couldn't be handled + * better anyway. In case of debug builds, failures + * trigger an assert(). + * + * Parameters : + * 1 : size = Size of memory chunk to return. + * + * Returns : Pointer to newly malloc'd memory chunk. + * + *********************************************************************/ +void *zalloc_or_die(size_t size) +{ + void *buffer; + + buffer = zalloc(size); + if (buffer == NULL) + { + assert(buffer != NULL); + log_error(LOG_LEVEL_FATAL, "Out of memory in zalloc_or_die()."); + exit(1); + } + + return(buffer); + +} + /********************************************************************* * * Function : strdup_or_die @@ -731,7 +765,9 @@ long int pick_from_range(long int range) if (range <= 0) return 0; -#ifdef HAVE_RANDOM +#ifdef HAVE_ARC4RANDOM + number = arc4random() % range + 1; +#elif defined(HAVE_RANDOM) number = random() % range + 1; #elif defined(MUTEX_LOCKS_AVAILABLE) privoxy_mutex_lock(&rand_mutex); @@ -755,7 +791,7 @@ long int pick_from_range(long int range) "might cause crashes, predictable results or even combine these fine options."); number = rand() % (long int)(range + 1); -#endif /* (def HAVE_RANDOM) */ +#endif /* (def HAVE_ARC4RANDOM) */ return number; } @@ -859,6 +895,16 @@ time_t timegm(struct tm *tm) strcat(old_zone, zone); putenv(old_zone); #ifdef _WIN32 + /* http://man7.org/linux/man-pages/man3/putenv.3.html + * int putenv(char *string); + * The string pointed to by string becomes part of the environment, so altering the + * string changes the environment. + * In other words, the memory pointed to by *string is used until + * a) another call to putenv() with the same e-var name + * b) the program exits + * + * Windows e-vars don't work that way, so let's not leak memory. + */ free(old_zone); #endif /* def _WIN32 */ }