From: Jaakko Heinonen Date: Wed, 27 Jul 2005 06:47:38 +0000 (+0000) Subject: - add xrealloc_inc X-Git-Tag: upstream/0.6.1~2^2~201 X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=384a832f989722381d0ad998a87d52f2ce05b714;p=pkg%2Fabook.git - add xrealloc_inc - xmalloc cleanups --- diff --git a/filter.c b/filter.c index e11a60e..f5998ba 100644 --- a/filter.c +++ b/filter.c @@ -646,16 +646,12 @@ mutt_read_line(FILE *in, char **alias, char **rest) while( ! ISSPACE(*ptr) ) ptr++; - /* includes also the trailing zero */ - alias_len = (size_t)(ptr - tmp + 1); + alias_len = (size_t)(ptr - tmp); - if( (*alias = xmalloc(alias_len)) == NULL) { - free(line); - return 1; - } + *alias = xmalloc_inc(alias_len, 1); - strncpy(*alias, tmp, alias_len - 1); - *(*alias + alias_len - 1) = 0; + strncpy(*alias, tmp, alias_len); + *(*alias + alias_len) = 0; while(ISSPACE(*ptr)) ptr++; diff --git a/misc.c b/misc.c index 6c96c5a..db2a584 100644 --- a/misc.c +++ b/misc.c @@ -103,7 +103,7 @@ mkstr (const char *format, ... ) else size *= 2; - buffer = xrealloc (buffer, size); + buffer = xrealloc(buffer, size); } } @@ -218,7 +218,7 @@ getaline(FILE *f) break; /* the whole line has been read */ for (inc = size, p = NULL; inc > mininc; inc /= 2) - if ((p = xrealloc(buf, size + inc)) != + if ((p = xrealloc_inc(buf, size, inc)) != NULL) break; @@ -235,7 +235,7 @@ getaline(FILE *f) buf[--len] = '\0'; if (size - len > mucho) { /* a plenitude of unused memory? */ - p = xrealloc(buf, len+1); + p = xrealloc_inc(buf, len, 1); if (p != NULL) { buf = p; size = len+1; diff --git a/xmalloc.c b/xmalloc.c index 086f3fb..bc22a97 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -78,22 +78,6 @@ xmalloc0(size_t size) return p; } -void * -xrealloc(void *ptr, size_t size) -{ - if((ptr = realloc(ptr, size)) == NULL) - (*xmalloc_handle_error)(errno); - - return ptr; -} - -void -xfree(void *ptr) -{ - free(ptr); - ptr = NULL; -} - static void * _xmalloc_inc(size_t size, size_t inc, int zero) { @@ -122,3 +106,38 @@ xmalloc0_inc(size_t size, size_t inc) return _xmalloc_inc(size, inc, 1); } +void * +xrealloc(void *ptr, size_t size) +{ + if((ptr = realloc(ptr, size)) == NULL) + (*xmalloc_handle_error)(errno); + + return ptr; +} + +void * +xrealloc_inc(void *ptr, size_t size, size_t inc) +{ + size_t total_size = size + inc; + + /* + * check if the calculation overflowed + */ + if(total_size < size) { + (*xmalloc_handle_error)(EINVAL); + return NULL; + } + + if((ptr = realloc(ptr, total_size)) == NULL) + (*xmalloc_handle_error)(errno); + + return ptr; +} + +void +xfree(void *ptr) +{ + free(ptr); + ptr = NULL; +} + diff --git a/xmalloc.h b/xmalloc.h index f8e0bc1..c6c5317 100644 --- a/xmalloc.h +++ b/xmalloc.h @@ -8,6 +8,7 @@ void * xmalloc(size_t); void * xmalloc0(size_t); void * xmalloc_inc(size_t, size_t); void * xrealloc(void *, size_t); +void * xrealloc_inc(void *, size_t, size_t); void xfree(void *); #endif