X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=miscutil.c;h=10ff9ead4f58c2cd7d519964c46bf16b1ba9a2e4;hp=22e7ec460712931ec9daa07990ea3e2d3ba16d4c;hb=6c92f003d85e32dfcfed47d26263e9db40806d46;hpb=292da21cea2a42b0896d667cff7201ef0ea2894e diff --git a/miscutil.c b/miscutil.c index 22e7ec46..10ff9ead 100644 --- a/miscutil.c +++ b/miscutil.c @@ -1,4 +1,4 @@ -const char miscutil_rcs[] = "$Id: miscutil.c,v 1.73 2012/03/04 11:52:45 fabiankeil Exp $"; +const char miscutil_rcs[] = "$Id: miscutil.c,v 1.81 2016/02/26 12:29:17 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $ @@ -8,7 +8,7 @@ const char miscutil_rcs[] = "$Id: miscutil.c,v 1.73 2012/03/04 11:52:45 fabianke * to deserve their own file but don't really fit in * any other file. * - * Copyright : Written by and Copyright (C) 2001-2011 the + * Copyright : Written by and Copyright (C) 2001-2016 the * Privoxy team. http://www.privoxy.org/ * * Based on the Internet Junkbuster originally written @@ -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 @@ -129,6 +163,50 @@ char *strdup_or_die(const char *str) } +/********************************************************************* + * + * Function : malloc_or_die + * + * Description : malloc wrapper that either succeeds or causes + * program termination. + * + * Useful in situations were the buffer size is "small" + * and malloc() failures couldn't be handled better + * anyway. In case of debug builds, failures trigger + * an assert(). + * + * Parameters : + * 1 : buffer_size = Size of the space to allocate + * + * Returns : Pointer to newly malloc'd memory + * + *********************************************************************/ +void *malloc_or_die(size_t buffer_size) +{ + char *new_buf; + + if (buffer_size == 0) + { + log_error(LOG_LEVEL_ERROR, + "malloc_or_die() called with buffer size 0"); + assert(buffer_size != 0); + buffer_size = 4096; + } + + new_buf = malloc(buffer_size); + + if (new_buf == NULL) + { + assert(new_buf != NULL); + log_error(LOG_LEVEL_FATAL, "Out of memory in malloc_or_die()."); + exit(1); + } + + return(new_buf); + +} + + #if defined(unix) /********************************************************************* * @@ -213,7 +291,7 @@ int strcmpic(const char *s1, const char *s2) while (*s1 && *s2) { - if (( *s1 != *s2) && ( privoxy_tolower(*s1) != privoxy_tolower(*s2) ) ) + if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2))) { break; } @@ -246,7 +324,7 @@ int strncmpic(const char *s1, const char *s2, size_t n) while (*s1 && *s2) { - if (( *s1 != *s2) && ( privoxy_tolower(*s1) != privoxy_tolower(*s2) ) ) + if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2))) { break; } @@ -488,6 +566,31 @@ char *string_toupper(const char *string) } +/********************************************************************* + * + * Function : string_move + * + * Description : memmove wrapper to move the last part of a string + * towards the beginning, overwriting the part in + * the middle. strlcpy() can't be used here as the + * strings overlap. + * + * Parameters : + * 1 : dst = Destination to overwrite + * 2 : src = Source to move. + * + * Returns : N/A + * + *********************************************************************/ +void string_move(char *dst, char *src) +{ + assert(dst < src); + + /* +1 to copy the terminating nul as well. */ + memmove(dst, src, strlen(src)+1); +} + + /********************************************************************* * * Function : bindup @@ -542,11 +645,11 @@ char * make_path(const char * dir, const char * file) #ifdef AMIGA char path[512]; - if(dir) + if (dir) { - if(dir[0] == '.') + if (dir[0] == '.') { - if(dir[1] == '/') + if (dir[1] == '/') { strncpy(path,dir+2,512); } @@ -565,7 +668,7 @@ char * make_path(const char * dir, const char * file) { path[0]=0; } - if(AddPart(path,file,512)) + if (AddPart(path,file,512)) { return strdup(path); } @@ -586,7 +689,7 @@ char * make_path(const char * dir, const char * file) #else /* ifndef _WIN32 || __OS2__ */ || (*file == '/') /* Absolute path (U*ix) */ #endif /* ifndef _WIN32 || __OS2__ */ - ) + ) { return strdup(file); } @@ -618,12 +721,12 @@ char * make_path(const char * dir, const char * file) assert(NULL != path); #if defined(_WIN32) || defined(__OS2__) - if(path[strlen(path)-1] != '\\') + if (path[strlen(path)-1] != '\\') { strlcat(path, "\\", path_size); } #else /* ifndef _WIN32 || __OS2__ */ - if(path[strlen(path)-1] != '/') + if (path[strlen(path)-1] != '/') { strlcat(path, "/", path_size); } @@ -790,6 +893,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 */ } @@ -1374,7 +1487,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { tmp[str_arg_l++] = space_for_positive ? ' ' : '+'; #endif } else if (alternate_form) { - if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X')) + if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X') ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = fmt_spec; } /* alternate form should have no effect for p conversion, but ... */ #ifdef HPUX_COMPATIBLE @@ -1385,7 +1498,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { /* Actually it uses 0x prefix even for a zero value. */ && arg_sign != 0 #endif - ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; } + ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; } #endif } zero_padding_insertion_ind = str_arg_l; @@ -1397,7 +1510,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { * converting a zero value with a precision of zero is a null string. * Actually HP returns all zeroes, and Linux returns "(nil)". */ #endif - ) { + ) { /* converted to null string */ /* When zero value is formatted with an explicit precision 0, the resulting formatted string is empty (d, i, u, o, x, X, p). */ @@ -1437,7 +1550,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { if (zero_padding_insertion_ind+1 < str_arg_l && tmp[zero_padding_insertion_ind] == '0' && (tmp[zero_padding_insertion_ind+1] == 'x' || - tmp[zero_padding_insertion_ind+1] == 'X')) { + tmp[zero_padding_insertion_ind+1] == 'X') ) { zero_padding_insertion_ind += 2; } } @@ -1452,7 +1565,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { && !(zero_padding_insertion_ind < str_arg_l && tmp[zero_padding_insertion_ind] == '0') #endif - ) { /* assure leading zero for alternate-form octal numbers */ + ) { /* assure leading zero for alternate-form octal numbers */ if (!precision_specified || precision < num_of_digits+1) { /* precision is increased to force the first character to be zero, except if a zero value is formatted with an explicit precision