From a4dda309ae93a2d233f705328babc911a286242c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 23 Nov 2013 19:24:26 +0100 Subject: define xrealloc as a secure realloc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/passphrase.c | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/passphrase.c b/src/passphrase.c index ad9062d..8329d25 100644 --- a/src/passphrase.c +++ b/src/passphrase.c @@ -35,6 +35,28 @@ static struct termios saved_stty; #endif +#ifndef PASSPHRASE_REALLOC +static inline char* xrealloc(char* array, size_t size) +{ + char* rc = malloc(size); + int i; + if (rc) + for (i = 0; *(array + i); i++) + { + *(rc + i) = *(array + i); + *(array + i) = 0; + } + else + for (i = 0; *(array + i); i++) + *(array + i) = 0; + free(array); + return rc; +} +#else +#define xrealloc realloc +#endif + + /** * Reads the passphrase from stdin * @@ -74,27 +96,8 @@ char* passphrase_read(void) #endif *(rc + len++) = c; if (len == size) - { -#ifndef PASSPHRASE_REALLOC - char* rc_2 = malloc((size <<= 1L) * sizeof(char)); - int i; - if (rc_2) - { - for (i = 0; i < len; i++) - *(rc_2 + i) = *(rc + i); - } - for (i = 0; i < len; i++) - *(rc + i) = 0; - free(rc); - if (rc_2 == NULL) - return rc_2; - rc = rc_2; -#else - rc = realloc(rc, (size <<= 1L) * sizeof(char)); - if (rc == NULL) - return NULL; -#endif - } + if ((rc = xrealloc(rc, (size <<= 1L) * sizeof(char))) == NULL) + return rc; } } -- cgit v1.2.3-70-g09d2