Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Use a proper char */size_t pair in __sfileext to keep track ...



details:   https://anonhg.NetBSD.org/src/rev/956d269e2444
branches:  trunk
changeset: 750713:956d269e2444
user:      joerg <joerg%NetBSD.org@localhost>
date:      Mon Jan 11 20:39:29 2010 +0000

description:
Use a proper char */size_t pair in __sfileext to keep track of the line
buffer for fgetln and fgetwln. Simplifies code by dropping the INT_MAX
related logic. Drop conditionals around FREELB, free(NULL) is valid.

diffstat:

 include/stdio.h           |   6 +++---
 lib/libc/stdio/fclose.c   |   7 +++----
 lib/libc/stdio/fgetstr.c  |  20 ++++----------------
 lib/libc/stdio/fgetwln.c  |  25 ++++++++++---------------
 lib/libc/stdio/fileext.h  |  14 +++++++++++---
 lib/libc/stdio/findfp.c   |   6 ++----
 lib/libc/stdio/freopen.c  |   8 +++-----
 lib/libc/stdio/local.h    |   8 ++++----
 lib/libc/stdio/sscanf.c   |   5 ++---
 lib/libc/stdio/vsscanf.c  |   5 ++---
 lib/libc/stdio/vswscanf.c |   5 ++---
 11 files changed, 46 insertions(+), 63 deletions(-)

diffs (truncated from 357 to 300 lines):

diff -r 84f26e84c19c -r 956d269e2444 include/stdio.h
--- a/include/stdio.h   Mon Jan 11 19:40:01 2010 +0000
+++ b/include/stdio.h   Mon Jan 11 20:39:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: stdio.h,v 1.74 2009/07/13 22:19:24 roy Exp $   */
+/*     $NetBSD: stdio.h,v 1.75 2010/01/11 20:39:29 joerg Exp $ */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -133,8 +133,8 @@
        unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
        unsigned char _nbuf[1]; /* guarantee a getc() buffer */
 
-       /* separate buffer for fgetln() when line crosses buffer boundary */
-       struct  __sbuf _lb;     /* buffer for fgetln() */
+       /* Formerly used by fgetln/fgetwln; kept for binary compatibility */
+       struct  __sbuf _lb__unused;
 
        /* Unix stdio files get aligned to block boundaries on fseek() */
        int     _blksize;       /* stat.st_blksize (may be != _bf._size) */
diff -r 84f26e84c19c -r 956d269e2444 lib/libc/stdio/fclose.c
--- a/lib/libc/stdio/fclose.c   Mon Jan 11 19:40:01 2010 +0000
+++ b/lib/libc/stdio/fclose.c   Mon Jan 11 20:39:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fclose.c,v 1.16 2003/08/07 16:43:22 agc Exp $  */
+/*     $NetBSD: fclose.c,v 1.17 2010/01/11 20:39:29 joerg Exp $        */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)fclose.c   8.1 (Berkeley) 6/4/93";
 #endif
-__RCSID("$NetBSD: fclose.c,v 1.16 2003/08/07 16:43:22 agc Exp $");
+__RCSID("$NetBSD: fclose.c,v 1.17 2010/01/11 20:39:29 joerg Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include <assert.h>
@@ -69,8 +69,7 @@
                free((char *)fp->_bf._base);
        if (HASUB(fp))
                FREEUB(fp);
-       if (HASLB(fp))
-               FREELB(fp);
+       FREELB(fp);
        FUNLOCKFILE(fp);
        fp->_file = -1;
        fp->_flags = 0;         /* Release this FILE for reuse. */
diff -r 84f26e84c19c -r 956d269e2444 lib/libc/stdio/fgetstr.c
--- a/lib/libc/stdio/fgetstr.c  Mon Jan 11 19:40:01 2010 +0000
+++ b/lib/libc/stdio/fgetstr.c  Mon Jan 11 20:39:29 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fgetstr.c,v 1.10 2009/12/02 09:03:13 roy Exp $     */
+/* $NetBSD: fgetstr.c,v 1.11 2010/01/11 20:39:29 joerg Exp $   */
 
 /*
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: fgetstr.c,v 1.10 2009/12/02 09:03:13 roy Exp $");
+__RCSID("$NetBSD: fgetstr.c,v 1.11 2010/01/11 20:39:29 joerg Exp $");
 
 #include "namespace.h"
 
@@ -49,24 +49,12 @@
 char *
 __fgetstr(FILE *__restrict fp, size_t *__restrict lenp, int sep)
 {
-       char *p;
-       size_t size;
        ssize_t n;
 
        _DIAGASSERT(fp != NULL);
        _DIAGASSERT(lenp != NULL);
 
-       p = (char *)fp->_lb._base;
-       size = fp->_lb._size;
-       n = __getdelim(&p, &size, sep, fp);
-       fp->_lb._base = (unsigned char *)p;
-       /* The struct size variable is only an int .....
-        * This still works when exceeded, but the buffer could be
-        * realloced needlessly. */
-       if (size > INT_MAX)
-               fp->_lb._size = INT_MAX;
-       else
-               fp->_lb._size = (int)size;
+       n = __getdelim(&_EXT(fp)->_fgetstr_buf, &_EXT(fp)->_fgetstr_len, sep, fp);
        if (n == -1) {
                *lenp = 0;
                if (__sferror(fp) && errno == EOVERFLOW) /* fixup errno */
@@ -74,5 +62,5 @@
                return NULL;
        }
        *lenp = n;
-       return p;
+       return _EXT(fp)->_fgetstr_buf;
 }
diff -r 84f26e84c19c -r 956d269e2444 lib/libc/stdio/fgetwln.c
--- a/lib/libc/stdio/fgetwln.c  Mon Jan 11 19:40:01 2010 +0000
+++ b/lib/libc/stdio/fgetwln.c  Mon Jan 11 20:39:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fgetwln.c,v 1.3 2009/09/24 20:38:53 roy Exp $  */
+/*     $NetBSD: fgetwln.c,v 1.4 2010/01/11 20:39:29 joerg Exp $        */
 
 /*-
  * Copyright (c) 2002-2004 Tim J. Robbins.
@@ -31,7 +31,7 @@
 #if 0
 __FBSDID("$FreeBSD: src/lib/libc/stdio/fgetwln.c,v 1.2 2004/08/06 17:00:09 tjr Exp $");
 #else
-__RCSID("$NetBSD: fgetwln.c,v 1.3 2009/09/24 20:38:53 roy Exp $");
+__RCSID("$NetBSD: fgetwln.c,v 1.4 2010/01/11 20:39:29 joerg Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -66,17 +66,12 @@
 #endif
        _DIAGASSERT(fp != NULL);
 
-       /* fp->_lb._size is an int ..... */
-       if (newsize > INT_MAX) {
-               errno = EOVERFLOW;
+       if (_EXT(fp)->_fgetstr_len >= newsize)
+               return (0);
+       if ((p = realloc(_EXT(fp)->_fgetstr_buf, newsize)) == NULL)
                return (-1);
-       }
-       if ((size_t)fp->_lb._size >= newsize)
-               return (0);
-       if ((p = realloc(fp->_lb._base, newsize)) == NULL)
-               return (-1);
-       fp->_lb._base = p;
-       fp->_lb._size = newsize;
+       _EXT(fp)->_fgetstr_buf = p;
+       _EXT(fp)->_fgetstr_len = newsize;
        return (0);
 }
 
@@ -92,10 +87,10 @@
        len = 0;
        while ((wc = __fgetwc_unlock(fp)) != WEOF) {
 #define        GROW    512
-               if (len * sizeof(wchar_t) >= (size_t)fp->_lb._size &&
+               if (len * sizeof(wchar_t) >= _EXT(fp)->_fgetstr_len &&
                    __slbexpand(fp, (len + GROW) * sizeof(wchar_t)))
                        goto error;
-               *((wchar_t *)(void *)fp->_lb._base + len++) = wc;
+               *((wchar_t *)(void *)_EXT(fp)->_fgetstr_buf + len++) = wc;
                if (wc == L'\n')
                        break;
        }
@@ -104,7 +99,7 @@
 
        FUNLOCKFILE(fp);
        *lenp = len;
-       return ((wchar_t *)(void *)fp->_lb._base);
+       return ((wchar_t *)(void *)_EXT(fp)->_fgetstr_buf);
 
 error:
        FUNLOCKFILE(fp);
diff -r 84f26e84c19c -r 956d269e2444 lib/libc/stdio/fileext.h
--- a/lib/libc/stdio/fileext.h  Mon Jan 11 19:40:01 2010 +0000
+++ b/lib/libc/stdio/fileext.h  Mon Jan 11 20:39:29 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fileext.h,v 1.5 2003/07/18 21:46:41 nathanw Exp $ */
+/* $NetBSD: fileext.h,v 1.6 2010/01/11 20:39:29 joerg Exp $ */
 
 /*-
  * Copyright (c)2001 Citrus Project,
@@ -34,6 +34,8 @@
 struct __sfileext {
        struct  __sbuf _ub; /* ungetc buffer */
        struct wchar_io_data _wcio;     /* wide char i/o status */
+       size_t _fgetstr_len;
+       char *_fgetstr_buf;
 #ifdef _REENTRANT
        mutex_t _lock;  /* Lock for FLOCKFILE/FUNLOCKFILE */
        cond_t _lockcond; /* Condition variable for signalling lock releases */
@@ -55,12 +57,18 @@
 #define _LOCKCANCELSTATE(fp) (_EXT(fp)->_lockcancelstate)
 #define _FILEEXT_SETUP(f, fext) do { \
        /* LINTED */(f)->_ext._base = (unsigned char *)(fext); \
+       (fext)->_fgetstr_len = 0; \
+       (fext)->_fgetstr_buf = NULL; \
        mutex_init(&_LOCK(f), NULL); \
        cond_init(&_LOCKCOND(f), 0, NULL); \
        _LOCKOWNER(f) = NULL; \
        _LOCKCOUNT(f) = 0; \
        _LOCKINTERNAL(f) = 0; \
-       } while (/* LINTED */ 0)
+       } while (/* CONSTCOND */ 0)
 #else
-#define _FILEEXT_SETUP(f, fext) /* LINTED */(f)->_ext._base = (unsigned char *)(fext)
+#define _FILEEXT_SETUP(f, fext) do { \
+       /* LINTED */(f)->_ext._base = (unsigned char *)(fext); \
+       (fext)->_fgetstr_len = 0; \
+       (fext)->_fgetstr_buf = NULL; \
+       } while (/* CONSTCOND */ 0)
 #endif
diff -r 84f26e84c19c -r 956d269e2444 lib/libc/stdio/findfp.c
--- a/lib/libc/stdio/findfp.c   Mon Jan 11 19:40:01 2010 +0000
+++ b/lib/libc/stdio/findfp.c   Mon Jan 11 20:39:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: findfp.c,v 1.23 2006/10/07 21:40:46 thorpej Exp $      */
+/*     $NetBSD: findfp.c,v 1.24 2010/01/11 20:39:29 joerg Exp $        */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)findfp.c   8.2 (Berkeley) 1/4/94";
 #else
-__RCSID("$NetBSD: findfp.c,v 1.23 2006/10/07 21:40:46 thorpej Exp $");
+__RCSID("$NetBSD: findfp.c,v 1.24 2010/01/11 20:39:29 joerg Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -154,8 +154,6 @@
 /*     fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
        _UB(fp)._base = NULL;   /* no ungetc buffer */
        _UB(fp)._size = 0;
-       fp->_lb._base = NULL;   /* no line buffer */
-       fp->_lb._size = 0;
        memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data));
        rwlock_unlock(&__sfp_lock);
        return (fp);
diff -r 84f26e84c19c -r 956d269e2444 lib/libc/stdio/freopen.c
--- a/lib/libc/stdio/freopen.c  Mon Jan 11 19:40:01 2010 +0000
+++ b/lib/libc/stdio/freopen.c  Mon Jan 11 20:39:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: freopen.c,v 1.15 2008/03/13 15:40:00 christos Exp $    */
+/*     $NetBSD: freopen.c,v 1.16 2010/01/11 20:39:29 joerg Exp $       */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)freopen.c  8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: freopen.c,v 1.15 2008/03/13 15:40:00 christos Exp $");
+__RCSID("$NetBSD: freopen.c,v 1.16 2010/01/11 20:39:29 joerg Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -135,9 +135,7 @@
                FREEUB(fp);
        WCIO_FREE(fp);
        _UB(fp)._size = 0;
-       if (HASLB(fp))
-               FREELB(fp);
-       fp->_lb._size = 0;
+       FREELB(fp);
 
        if (f < 0) {                    /* did not get it after all */
                fp->_flags = 0;         /* set it free */
diff -r 84f26e84c19c -r 956d269e2444 lib/libc/stdio/local.h
--- a/lib/libc/stdio/local.h    Mon Jan 11 19:40:01 2010 +0000
+++ b/lib/libc/stdio/local.h    Mon Jan 11 20:39:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: local.h,v 1.23 2009/12/02 09:03:13 roy Exp $   */
+/*     $NetBSD: local.h,v 1.24 2010/01/11 20:39:29 joerg Exp $ */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -104,10 +104,10 @@
 /*
  * test for an fgetln() buffer.
  */
-#define        HASLB(fp) ((fp)->_lb._base != NULL)
 #define        FREELB(fp) { \
-       free((char *)(fp)->_lb._base); \
-       (fp)->_lb._base = NULL; \
+       free(_EXT(fp)->_fgetstr_buf); \
+       _EXT(fp)->_fgetstr_buf = NULL; \
+       _EXT(fp)->_fgetstr_len = 0; \
 }
 
 extern void __flockfile_internal __P((FILE *, int));
diff -r 84f26e84c19c -r 956d269e2444 lib/libc/stdio/sscanf.c
--- a/lib/libc/stdio/sscanf.c   Mon Jan 11 19:40:01 2010 +0000
+++ b/lib/libc/stdio/sscanf.c   Mon Jan 11 20:39:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sscanf.c,v 1.16 2005/11/29 03:12:00 christos Exp $     */
+/*     $NetBSD: sscanf.c,v 1.17 2010/01/11 20:39:29 joerg Exp $        */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)sscanf.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: sscanf.c,v 1.16 2005/11/29 03:12:00 christos Exp $");
+__RCSID("$NetBSD: sscanf.c,v 1.17 2010/01/11 20:39:29 joerg Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -80,7 +80,6 @@
        f._bf._size = f._r = strlen(str);



Home | Main Index | Thread Index | Old Index