aboutsummaryrefslogtreecommitdiffstats
path: root/src/passphrase.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2013-11-23 19:24:26 +0100
committerMattias Andrée <maandree@operamail.com>2013-11-23 19:24:30 +0100
commita4dda309ae93a2d233f705328babc911a286242c (patch)
tree3f58da589e90478b63419ce34e9c573cff1c4e82 /src/passphrase.c
parentm (diff)
downloadlibpassphrase-a4dda309ae93a2d233f705328babc911a286242c.tar.gz
libpassphrase-a4dda309ae93a2d233f705328babc911a286242c.tar.bz2
libpassphrase-a4dda309ae93a2d233f705328babc911a286242c.tar.xz
define xrealloc as a secure realloc
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/passphrase.c')
-rw-r--r--src/passphrase.c45
1 files changed, 24 insertions, 21 deletions
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;
}
}