summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdio/asprintf.c
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>2017-03-16 14:32:02 +0000
committermillert <millert@openbsd.org>2017-03-16 14:32:02 +0000
commit2abfd297431aae8f6c9b952fbefe1d953c3ffdaa (patch)
treed0c92502e20735959d7b04f03b595c3140e04119 /lib/libc/stdio/asprintf.c
parentbit more consistent; (diff)
downloadwireguard-openbsd-2abfd297431aae8f6c9b952fbefe1d953c3ffdaa.tar.xz
wireguard-openbsd-2abfd297431aae8f6c9b952fbefe1d953c3ffdaa.zip
Only reallocate the buffer to fit for medium-size allocations where
we expanded the buffer to a single page. The final realloc() can be expensive for large buffers and is not realled needed. OK deraadt@
Diffstat (limited to 'lib/libc/stdio/asprintf.c')
-rw-r--r--lib/libc/stdio/asprintf.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/lib/libc/stdio/asprintf.c b/lib/libc/stdio/asprintf.c
index 175765b196d..62b50a4a8b8 100644
--- a/lib/libc/stdio/asprintf.c
+++ b/lib/libc/stdio/asprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: asprintf.c,v 1.23 2017/03/14 16:46:05 millert Exp $ */
+/* $OpenBSD: asprintf.c,v 1.24 2017/03/16 14:32:02 millert Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -21,9 +21,10 @@
#include <string.h>
#include <errno.h>
#include <stdarg.h>
+#include <unistd.h>
#include "local.h"
-#define INITIAL_LEN 127 /* plus one for the NUL */
+#define INITIAL_SIZE 128
int
asprintf(char **str, const char *fmt, ...)
@@ -32,25 +33,29 @@ asprintf(char **str, const char *fmt, ...)
va_list ap;
FILE f;
struct __sfileext fext;
- unsigned char *_base;
+ const int pgsz = getpagesize();
_FILEEXT_SETUP(&f, &fext);
f._file = -1;
f._flags = __SWR | __SSTR | __SALC;
- f._bf._base = f._p = malloc(INITIAL_LEN + 1);
+ f._bf._base = f._p = malloc(INITIAL_SIZE);
if (f._bf._base == NULL)
goto err;
- f._bf._size = f._w = INITIAL_LEN;
+ f._bf._size = f._w = INITIAL_SIZE - 1; /* leave room for the NUL */
va_start(ap, fmt);
ret = __vfprintf(&f, fmt, ap);
va_end(ap);
if (ret == -1)
goto err;
*f._p = '\0';
- _base = realloc(f._bf._base, ret + 1);
- if (_base == NULL)
- goto err;
- *str = (char *)_base;
+ 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);
+ if (_base == NULL)
+ goto err;
+ *str = (char *)_base;
+ } else
+ *str = (char *)f._bf._base;
return (ret);
err: