X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=miscutil.c;h=f5e647caf377292e65e46bef4b38f14ee423b5c5;hp=a002abb7bd18a17673f2ef67d8f170e4929cb093;hb=138747bd3f178c448de25dce2ca1d90de6e0f6cb;hpb=50f87c9446b435d47c371f63615260636639f450 diff --git a/miscutil.c b/miscutil.c index a002abb7..f5e647ca 100644 --- a/miscutil.c +++ b/miscutil.c @@ -1,4 +1,4 @@ -const char miscutil_rcs[] = "$Id: miscutil.c,v 1.74 2012/03/09 16:23:50 fabiankeil Exp $"; +const char miscutil_rcs[] = "$Id: miscutil.c,v 1.80 2016/01/16 12:33:36 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $ @@ -8,7 +8,7 @@ const char miscutil_rcs[] = "$Id: miscutil.c,v 1.74 2012/03/09 16:23:50 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 @@ -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); } @@ -1374,7 +1477,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 +1488,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 +1500,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 +1540,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 +1555,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