From caf54d1d7f7dd596814c6069b0af4a69dee6cc78 Mon Sep 17 00:00:00 2001 From: dockes Date: Thu, 18 Dec 2008 11:12:37 +0000 Subject: [PATCH] added recoll memory allocation checks --- unac/unac.c | 75 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/unac/unac.c b/unac/unac.c index c21c566d..feec8bbe 100644 --- a/unac/unac.c +++ b/unac/unac.c @@ -38,6 +38,7 @@ #endif /* HAVE_VSNPRINTF */ #include "unac.h" +#include "unac_version.h" /* Generated by builder. Do not modify. Start tables */ /* @@ -13879,7 +13880,7 @@ unac_data367 * Debug level. See unac.h for a detailed discussion of the * values. */ -static int debug_level = UNAC_DEBUG_NONE; +static int debug_level = UNAC_DEBUG_LOW; #ifdef UNAC_DEBUG_AVAILABLE @@ -13947,14 +13948,14 @@ void unac_debug_callback(int level, unac_debug_print_t function, void* data) * compatibility hack introduced in some GNU/Linux distributions that * did not know UTF-16BE. */ -static const char* utf16be() +static const char* utf16be(void) { iconv_t cd; static char* name = 0; if(name == 0) { if((cd = iconv_open("UTF-16BE", "UTF-16BE")) == (iconv_t)-1) { - if(debug_level == UNAC_DEBUG_LOW) DEBUG("could not find UTF-16BE (see iconv -l), using UTF-16. If UTF-16 happens to be encoded in little endian, be prepared for an horrible mess."); + if(debug_level >= UNAC_DEBUG_LOW) DEBUG("could not find UTF-16BE (see iconv -l), using UTF-16. If UTF-16 happens to be encoded in little endian, be prepared for an horrible mess."); name = "UTF-16"; } else { iconv_close(cd); @@ -13974,14 +13975,24 @@ int unacmaybefold_string_utf16(const char* in, size_t in_length, int i; out_size = in_length > 0 ? in_length : 1024; - if(*outp) { + if (*outp) { out = *outp; /* +1 for null */ out = realloc(out, out_size + 1); + if(out == 0) { + if(debug_level >= UNAC_DEBUG_LOW) + DEBUG("realloc %d bytes failed\n", out_size+1); + /* *outp is still valid. Let the caller free it */ + return -1; + } } else { /* +1 for null */ out = malloc(out_size + 1); - if(out == 0) return -1; + if (out == 0) { + if(debug_level >= UNAC_DEBUG_LOW) + DEBUG("malloc %d bytes failed\n", out_size+1); + return -1; + } } out_length = 0; @@ -14016,15 +14027,22 @@ int unacmaybefold_string_utf16(const char* in, size_t in_length, DEBUG_APPEND("\n"); } } + /* * Make sure there is enough space to hold the decomposition + * Note: a previous realloc may have succeeded, which means that *outp + * is not valid any more. We have to do the freeing and zero out *outp */ if(out_length + ((l + 1) * 2) > out_size) { + char *saved; out_size += ((l + 1) * 2) + 1024; + saved = out; out = realloc(out, out_size); if(out == 0) { - if(debug_level == UNAC_DEBUG_LOW) + if(debug_level >= UNAC_DEBUG_LOW) DEBUG("realloc %d bytes failed\n", out_size); + free(saved); + *outp = 0; return -1; } } @@ -14095,10 +14113,20 @@ static int convert(const char* from, const char* to, out = *outp; /* +1 for null */ out = realloc(out, out_size + 1); + if(out == 0) { + /* *outp still valid, no freeing */ + if(debug_level >= UNAC_DEBUG_LOW) + DEBUG("realloc %d bytes failed\n", out_size+1); + return -1; + } } else { /* +1 for null */ out = malloc(out_size + 1); - if(out == 0) return -1; + if(out == 0) { + if(debug_level >= UNAC_DEBUG_LOW) + DEBUG("malloc %d bytes failed\n", out_size+1); + return -1; + } } out_remain = out_size; out_base = out; @@ -14150,13 +14178,24 @@ static int convert(const char* from, const char* to, case E2BIG: { /* - * The output does not fit in the current out buffer, enlarge it. - */ + * The output does not fit in the current out buffer, enlarge it. + */ int length = out - out_base; out_size *= 2; - /* +1 for null */ - out_base = realloc(out_base, out_size + 1); - if(out_base == 0) return -1; + { + char *saved = out_base; + /* +1 for null */ + out_base = realloc(out_base, out_size + 1); + if (out_base == 0) { + /* *outp potentially not valid any more. Free here, + * and zero out */ + if(debug_level >= UNAC_DEBUG_LOW) + DEBUG("realloc %d bytes failed\n", out_size+1); + free(saved); + *outp = 0; + return -1; + } + } out = out_base + length; out_remain = out_size - length; } @@ -14184,11 +14223,13 @@ int unacmaybefold_string(const char* charset, * When converting an empty string, skip everything but alloc the * buffer if NULL pointer. */ - if(in_length <= 0) { - if(!*outp) - *outp = malloc(32); - (*outp)[0] = '\0'; - *out_lengthp = 0; + if (in_length <= 0) { + if(!*outp) { + if ((*outp = malloc(32)) == 0) + return -1; + } + (*outp)[0] = '\0'; + *out_lengthp = 0; } else { char* utf16 = 0; size_t utf16_length = 0;