summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/asprintf.c
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2017-03-17 14:53:09 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2017-03-17 14:53:09 +0000
commit25883161ff1e1bdbdd3128e1b52e2ed218f0b714 (patch)
tree8cd54e3496f566a14c72b225a447dbba31cfc883 /lib/libc/stdio/asprintf.c
parentc284141464f73cf0430c26f1b3f81112b65fc135 (diff)
Use recallocarray() to avoid leaving detritus in memory when resizing
buffers. We don't bother doing this for objects containing pointers, but focus on controllable data. ok millert
Diffstat (limited to 'lib/libc/stdio/asprintf.c')
-rw-r--r--lib/libc/stdio/asprintf.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/lib/libc/stdio/asprintf.c b/lib/libc/stdio/asprintf.c
index 62b50a4a8b8..cea64692369 100644
--- a/lib/libc/stdio/asprintf.c
+++ b/lib/libc/stdio/asprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: asprintf.c,v 1.24 2017/03/16 14:32:02 millert Exp $ */
+/* $OpenBSD: asprintf.c,v 1.25 2017/03/17 14:53:08 deraadt Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -50,7 +50,9 @@ asprintf(char **str, const char *fmt, ...)
*f._p = '\0';
if (ret + 1 > INITIAL_SIZE && ret + 1 < pgsz / 2) {
/* midsize allocations can try to conserve memory */
- unsigned char *_base = realloc(f._bf._base, ret + 1);
+ unsigned char *_base = recallocarray(f._bf._base,
+ f._bf._size + 1, ret + 1, 1);
+
if (_base == NULL)
goto err;
*str = (char *)_base;