summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdio/fvwrite.c
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>2017-03-16 14:26:18 +0000
committermillert <millert@openbsd.org>2017-03-16 14:26:18 +0000
commit6c3871687032a8f27380507f678d3084c373c6c4 (patch)
tree6631d850676966a926bb3beaf7298cdb8194415e /lib/libc/stdio/fvwrite.c
parentConvert BUF_MEM_grow() and BUF_MEM_grow_clean() to recallocarray(), (diff)
downloadwireguard-openbsd-6c3871687032a8f27380507f678d3084c373c6c4.tar.xz
wireguard-openbsd-6c3871687032a8f27380507f678d3084c373c6c4.zip
When reallocating the buffer for asprintf(), just round up to the
nearest page instead of doubling the old size until it is large enough. OK deraadt@
Diffstat (limited to 'lib/libc/stdio/fvwrite.c')
-rw-r--r--lib/libc/stdio/fvwrite.c11
1 files changed, 5 insertions, 6 deletions
diff --git a/lib/libc/stdio/fvwrite.c b/lib/libc/stdio/fvwrite.c
index 7517b5f00da..d7b2ec69bdc 100644
--- a/lib/libc/stdio/fvwrite.c
+++ b/lib/libc/stdio/fvwrite.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fvwrite.c,v 1.18 2016/09/21 04:38:56 guenther Exp $ */
+/* $OpenBSD: fvwrite.c,v 1.19 2017/03/16 14:26:18 millert Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -35,6 +35,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <unistd.h>
#include "local.h"
#include "fvwrite.h"
@@ -105,14 +106,12 @@ __sfvwrite(FILE *fp, struct __suio *uio)
if ((fp->_flags & (__SALC | __SSTR)) ==
(__SALC | __SSTR) && fp->_w < len) {
size_t blen = fp->_p - fp->_bf._base;
+ int pgmsk = getpagesize() - 1;
unsigned char *_base;
int _size;
- /* Allocate space exponentially. */
- _size = fp->_bf._size;
- do {
- _size = (_size << 1) + 1;
- } while (_size < blen + len);
+ /* Round up to nearest page. */
+ _size = ((blen + len + 1 + pgmsk) & ~pgmsk) - 1;
_base = realloc(fp->_bf._base, _size + 1);
if (_base == NULL)
goto err;