diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2017-03-17 14:53:09 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2017-03-17 14:53:09 +0000 |
commit | 25883161ff1e1bdbdd3128e1b52e2ed218f0b714 (patch) | |
tree | 8cd54e3496f566a14c72b225a447dbba31cfc883 | |
parent | c284141464f73cf0430c26f1b3f81112b65fc135 (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
-rw-r--r-- | lib/libc/stdio/asprintf.c | 6 | ||||
-rw-r--r-- | lib/libc/stdio/fgetln.c | 4 | ||||
-rw-r--r-- | lib/libc/stdio/open_memstream.c | 4 | ||||
-rw-r--r-- | lib/libc/stdio/vasprintf.c | 6 |
4 files changed, 12 insertions, 8 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; diff --git a/lib/libc/stdio/fgetln.c b/lib/libc/stdio/fgetln.c index bdb0c2a5b37..903dbd60661 100644 --- a/lib/libc/stdio/fgetln.c +++ b/lib/libc/stdio/fgetln.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fgetln.c,v 1.16 2016/09/21 04:38:56 guenther Exp $ */ +/* $OpenBSD: fgetln.c,v 1.17 2017/03/17 14:53:08 deraadt Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -46,7 +46,7 @@ __slbexpand(FILE *fp, size_t newsize) if (fp->_lb._size >= newsize) return (0); - if ((p = realloc(fp->_lb._base, newsize)) == NULL) + if ((p = recallocarray(fp->_lb._base, fp->_lb._size, newsize, 1)) == NULL) return (-1); fp->_lb._base = p; fp->_lb._size = newsize; diff --git a/lib/libc/stdio/open_memstream.c b/lib/libc/stdio/open_memstream.c index f708acc5032..131d4e08e13 100644 --- a/lib/libc/stdio/open_memstream.c +++ b/lib/libc/stdio/open_memstream.c @@ -1,4 +1,4 @@ -/* $OpenBSD: open_memstream.c,v 1.6 2015/08/31 02:53:57 guenther Exp $ */ +/* $OpenBSD: open_memstream.c,v 1.7 2017/03/17 14:53:08 deraadt Exp $ */ /* * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org> @@ -50,7 +50,7 @@ memstream_write(void *v, const char *b, int l) if (sz < end + 1) sz = end + 1; - p = realloc(st->string, sz); + p = recallocarray(st->string, st->size, sz, 1); if (!p) return (-1); bzero(p + st->size, sz - st->size); diff --git a/lib/libc/stdio/vasprintf.c b/lib/libc/stdio/vasprintf.c index 5aaaadf677b..174d8f4f4c1 100644 --- a/lib/libc/stdio/vasprintf.c +++ b/lib/libc/stdio/vasprintf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vasprintf.c,v 1.21 2017/03/16 14:32:02 millert Exp $ */ +/* $OpenBSD: vasprintf.c,v 1.22 2017/03/17 14:53:08 deraadt Exp $ */ /* * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> @@ -46,7 +46,9 @@ vasprintf(char **str, const char *fmt, __va_list ap) *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; |