aboutsummaryrefslogtreecommitdiffstats
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
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 '')
-rw-r--r--Makefile2
-rw-r--r--src/passphrase.c45
2 files changed, 25 insertions, 22 deletions
diff --git a/Makefile b/Makefile
index 5259633..bd9b847 100644
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ OPTIONS =
OPTIMISE = -Os
CPPFLAGS = $(foreach D, $(OPTIONS), -D'$(D)=1')
-CFLAGS = -std=c90 -Wall -Wextra -fPIC
+CFLAGS = -std=c99 -Wall -Wextra -fPIC
LDFLAGS = -shared
CC_FLAGS = $(CPPFLAGS) $(CFLAGS) $(OPTIMISE)
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;
}
}