From 01abeacded873dd835d2115d7c9a9b0b47e75254 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 13 Jan 2015 17:19:21 -0800 Subject: [PATCH] Sync with upstream OpenBSD stdio. Mainly to get the __atexit_register_cleanup removals we suggested. Change-Id: I58d40b8c5b8401bfb6bfffe8f3430ac0718af917 --- libc/upstream-openbsd/lib/libc/stdio/fgetln.c | 16 +++------------- libc/upstream-openbsd/lib/libc/stdio/getdelim.c | 7 +++---- libc/upstream-openbsd/lib/libc/stdio/makebuf.c | 3 +-- libc/upstream-openbsd/lib/libc/stdio/mktemp.c | 4 ++-- .../lib/libc/stdio/open_wmemstream.c | 4 ++-- libc/upstream-openbsd/lib/libc/stdio/setvbuf.c | 13 +++++++++---- libc/upstream-openbsd/lib/libc/stdio/ungetc.c | 6 +++--- 7 files changed, 23 insertions(+), 30 deletions(-) diff --git a/libc/upstream-openbsd/lib/libc/stdio/fgetln.c b/libc/upstream-openbsd/lib/libc/stdio/fgetln.c index d0c0809be..1109cf25c 100644 --- a/libc/upstream-openbsd/lib/libc/stdio/fgetln.c +++ b/libc/upstream-openbsd/lib/libc/stdio/fgetln.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fgetln.c,v 1.12 2013/11/12 07:04:06 deraadt Exp $ */ +/* $OpenBSD: fgetln.c,v 1.13 2015/01/05 21:58:52 millert Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -38,19 +38,12 @@ /* * Expand the line buffer. Return -1 on error. -#ifdef notdef - * The `new size' does not account for a terminating '\0', - * so we add 1 here. -#endif */ static int __slbexpand(FILE *fp, size_t newsize) { void *p; -#ifdef notdef - ++newsize; -#endif if (fp->_lb._size >= newsize) return (0); if ((p = realloc(fp->_lb._base, newsize)) == NULL) @@ -141,14 +134,11 @@ fgetln(FILE *fp, size_t *lenp) } *lenp = len; ret = (char *)fp->_lb._base; -#ifdef notdef - ret[len] = '\0'; -#endif FUNLOCKFILE(fp); return (ret); error: - *lenp = 0; /* ??? */ FUNLOCKFILE(fp); - return (NULL); /* ??? */ + *lenp = 0; + return (NULL); } diff --git a/libc/upstream-openbsd/lib/libc/stdio/getdelim.c b/libc/upstream-openbsd/lib/libc/stdio/getdelim.c index dcde0c34e..5e583cb55 100644 --- a/libc/upstream-openbsd/lib/libc/stdio/getdelim.c +++ b/libc/upstream-openbsd/lib/libc/stdio/getdelim.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getdelim.c,v 1.1 2012/03/21 23:44:35 fgsch Exp $ */ +/* $OpenBSD: getdelim.c,v 1.2 2014/10/16 17:31:51 millert Exp $ */ /* $NetBSD: getdelim.c,v 1.13 2011/07/22 23:12:30 joerg Exp $ */ /* @@ -78,13 +78,12 @@ getdelim(char **__restrict buf, size_t *__restrict buflen, else len = (p - fp->_p) + 1; - newlen = off + len; /* Ensure we can handle it */ - if (newlen < off || newlen > SSIZE_MAX) { + if (off > SSIZE_MAX || len + 1 > SSIZE_MAX - off) { errno = EOVERFLOW; goto error; } - newlen++; /* reserve space for the NULL terminator */ + newlen = off + len + 1; /* reserve space for NUL terminator */ if (newlen > *buflen) { if (newlen < MINBUF) newlen = MINBUF; diff --git a/libc/upstream-openbsd/lib/libc/stdio/makebuf.c b/libc/upstream-openbsd/lib/libc/stdio/makebuf.c index d47e27cf3..56e5f210c 100644 --- a/libc/upstream-openbsd/lib/libc/stdio/makebuf.c +++ b/libc/upstream-openbsd/lib/libc/stdio/makebuf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: makebuf.c,v 1.8 2005/12/28 18:50:22 millert Exp $ */ +/* $OpenBSD: makebuf.c,v 1.9 2015/01/13 07:18:21 guenther Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -65,7 +65,6 @@ __smakebuf(FILE *fp) fp->_bf._size = 1; return; } - __atexit_register_cleanup(_cleanup); flags |= __SMBF; fp->_bf._base = fp->_p = p; fp->_bf._size = size; diff --git a/libc/upstream-openbsd/lib/libc/stdio/mktemp.c b/libc/upstream-openbsd/lib/libc/stdio/mktemp.c index 2a17e522f..956608c15 100644 --- a/libc/upstream-openbsd/lib/libc/stdio/mktemp.c +++ b/libc/upstream-openbsd/lib/libc/stdio/mktemp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mktemp.c,v 1.34 2014/08/31 02:21:18 guenther Exp $ */ +/* $OpenBSD: mktemp.c,v 1.35 2014/10/31 15:54:14 millert Exp $ */ /* * Copyright (c) 1996-1998, 2008 Theo de Raadt * Copyright (c) 1997, 2008-2009 Todd C. Miller @@ -45,7 +45,7 @@ static int mktemp_internal(char *path, int slen, int mode, int flags) { char *start, *cp, *ep; - const char *tempchars = TEMPCHARS; + const char tempchars[] = TEMPCHARS; unsigned int tries; struct stat sb; size_t len; diff --git a/libc/upstream-openbsd/lib/libc/stdio/open_wmemstream.c b/libc/upstream-openbsd/lib/libc/stdio/open_wmemstream.c index 94141878f..391a94445 100644 --- a/libc/upstream-openbsd/lib/libc/stdio/open_wmemstream.c +++ b/libc/upstream-openbsd/lib/libc/stdio/open_wmemstream.c @@ -1,4 +1,4 @@ -/* $OpenBSD: open_wmemstream.c,v 1.3 2014/03/06 07:28:21 gerhard Exp $ */ +/* $OpenBSD: open_wmemstream.c,v 1.4 2014/10/08 05:28:19 deraadt Exp $ */ /* * Copyright (c) 2011 Martin Pieuchot @@ -52,7 +52,7 @@ wmemstream_write(void *v, const char *b, int l) if (sz < end + 1) sz = end + 1; - p = realloc(st->string, sz * sizeof(wchar_t)); + p = reallocarray(st->string, sz, sizeof(wchar_t)); if (!p) return (-1); bzero(p + st->size, (sz - st->size) * sizeof(wchar_t)); diff --git a/libc/upstream-openbsd/lib/libc/stdio/setvbuf.c b/libc/upstream-openbsd/lib/libc/stdio/setvbuf.c index 6c49f7a5d..9b2ab574f 100644 --- a/libc/upstream-openbsd/lib/libc/stdio/setvbuf.c +++ b/libc/upstream-openbsd/lib/libc/stdio/setvbuf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: setvbuf.c,v 1.11 2009/11/09 00:18:27 kurt Exp $ */ +/* $OpenBSD: setvbuf.c,v 1.12 2015/01/13 07:18:21 guenther Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -114,6 +114,13 @@ nbf: flags |= __SMBF; } + /* + * We're committed to buffering from here, so make sure we've + * registered to flush buffers on exit. + */ + if (!__sdidinit) + __sinit(); + /* * Kill any seek optimization if the buffer is not the * right size. @@ -124,8 +131,7 @@ nbf: flags |= __SNPT; /* - * Fix up the FILE fields, and set __cleanup for output flush on - * exit (since we are buffered in some way). + * Fix up the FILE fields. */ if (mode == _IOLBF) flags |= __SLBF; @@ -148,7 +154,6 @@ nbf: fp->_w = 0; } FUNLOCKFILE(fp); - __atexit_register_cleanup(_cleanup); return (ret); } diff --git a/libc/upstream-openbsd/lib/libc/stdio/ungetc.c b/libc/upstream-openbsd/lib/libc/stdio/ungetc.c index 675733aa6..ec98f26c2 100644 --- a/libc/upstream-openbsd/lib/libc/stdio/ungetc.c +++ b/libc/upstream-openbsd/lib/libc/stdio/ungetc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ungetc.c,v 1.12 2009/11/09 00:18:27 kurt Exp $ */ +/* $OpenBSD: ungetc.c,v 1.13 2014/10/11 04:05:10 deraadt Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -64,14 +64,14 @@ __submore(FILE *fp) return (0); } i = _UB(fp)._size; - p = realloc(_UB(fp)._base, i << 1); + p = reallocarray(_UB(fp)._base, i, 2); if (p == NULL) return (EOF); /* no overlap (hence can use memcpy) because we doubled the size */ (void)memcpy((void *)(p + i), (void *)p, (size_t)i); fp->_p = p + i; _UB(fp)._base = p; - _UB(fp)._size = i << 1; + _UB(fp)._size = i * 2; return (0); }