This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: vasprintf bugs
- From: Eric Blake <ebb9 at byu dot net>
- Cc: newlib at sources dot redhat dot com
- Date: Mon, 12 Mar 2007 07:09:37 -0600
- Subject: Re: vasprintf bugs
- References: <45F54F6D.7050902@byu.net>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
According to Eric Blake on 3/12/2007 7:02 AM:
> I noticed these while trying to implement vasnprintf.
>
> 2007-03-12 Eric Blake <ebb9@byu.net>
>
> * libc/stdio/fvwrite.c (__sfvwrite_r): Fix reentrancy.
> * libc/stdio/vasprintf.c (vasprintf, _vasprintf_r): Pass failed
> allocation to caller.
>
Resubmit - the integer-only and non-va_list variants were also affected.
2007-03-12 Eric Blake <ebb9@byu.net>
* libc/stdio/fvwrite.c (__sfvwrite_r): Fix reentrancy.
* libc/stdio/vasprintf.c (vasprintf, _vasprintf_r): Pass failed
allocation to caller.
* libc/stdio/asprintf.c (_asprintf_r, asprintf): Likewise.
* libc/stdio/asiprintf.c (_asiprintf_r, asiprintf): Likewise.
* libc/stdio/vasiprintf.c (vasiprintf, _vasiprintf_r): Likewise.
- --
Don't work too hard, make some time for fun as well!
Eric Blake ebb9@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFF9VEQ84KuGfSFAYARAsVHAJ0Qe/yK+zb8QmolCqgSeIoiRUMgnwCgzhFQ
r2V3RD8iyirwvaJSTWEACdc=
=0PoL
-----END PGP SIGNATURE-----
Index: libc/stdio/asiprintf.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/asiprintf.c,v
retrieving revision 1.1
diff -u -p -r1.1 asiprintf.c
--- libc/stdio/asiprintf.c 24 Nov 2004 00:45:41 -0000 1.1
+++ libc/stdio/asiprintf.c 12 Mar 2007 13:05:01 -0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1990 The Regents of the University of California.
+ * Copyright (c) 1990, 2007 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
@@ -57,8 +57,11 @@ _asiprintf_r(ptr, strp, fmt, va_alist)
#endif
ret = vfiprintf (&f, fmt, ap);
va_end (ap);
- *f._p = 0;
- *strp = f._bf._base;
+ if (ret >= 0)
+ {
+ *f._p = 0;
+ *strp = f._bf._base;
+ }
return (ret);
}
@@ -79,7 +82,7 @@ asiprintf(strp, fmt, va_alist)
int ret;
va_list ap;
FILE f;
-
+
/* mark a zero-length reallocatable buffer */
f._flags = __SWR | __SSTR | __SMBF;
f._bf._base = f._p = NULL;
@@ -92,8 +95,11 @@ asiprintf(strp, fmt, va_alist)
#endif
ret = vfiprintf (&f, fmt, ap);
va_end (ap);
- *f._p = 0;
- *strp = f._bf._base;
+ if (ret >= 0)
+ {
+ *f._p = 0;
+ *strp = f._bf._base;
+ }
return (ret);
}
Index: libc/stdio/asprintf.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/asprintf.c,v
retrieving revision 1.5
diff -u -p -r1.5 asprintf.c
--- libc/stdio/asprintf.c 23 Apr 2004 20:01:54 -0000 1.5
+++ libc/stdio/asprintf.c 12 Mar 2007 13:05:01 -0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1990 The Regents of the University of California.
+ * Copyright (c) 1990, 2007 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
@@ -57,8 +57,11 @@ _asprintf_r(ptr, strp, fmt, va_alist)
#endif
ret = vfprintf (&f, fmt, ap);
va_end (ap);
- *f._p = 0;
- *strp = f._bf._base;
+ if (ret >= 0)
+ {
+ *f._p = 0;
+ *strp = f._bf._base;
+ }
return (ret);
}
@@ -79,7 +82,7 @@ asprintf(strp, fmt, va_alist)
int ret;
va_list ap;
FILE f;
-
+
/* mark a zero-length reallocatable buffer */
f._flags = __SWR | __SSTR | __SMBF;
f._bf._base = f._p = NULL;
@@ -92,8 +95,11 @@ asprintf(strp, fmt, va_alist)
#endif
ret = vfprintf (&f, fmt, ap);
va_end (ap);
- *f._p = 0;
- *strp = f._bf._base;
+ if (ret >= 0)
+ {
+ *f._p = 0;
+ *strp = f._bf._base;
+ }
return (ret);
}
Index: libc/stdio/fvwrite.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fvwrite.c,v
retrieving revision 1.9
diff -u -p -r1.9 fvwrite.c
--- libc/stdio/fvwrite.c 29 Nov 2006 21:36:54 -0000 1.9
+++ libc/stdio/fvwrite.c 12 Mar 2007 13:05:01 -0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1990, 2006 The Regents of the University of California.
+ * Copyright (c) 1990, 2006, 2007 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
@@ -129,7 +129,7 @@ _DEFUN(__sfvwrite_r, (ptr, fp, uio),
{
if (len >= w && fp->_flags & __SMBF)
{ /* must be asprintf family */
- unsigned char *ptr;
+ unsigned char *str;
int curpos = (fp->_p - fp->_bf._base);
/* Choose a geometric growth factor to avoid
quadratic realloc behavior, but use a rate less
@@ -141,17 +141,16 @@ _DEFUN(__sfvwrite_r, (ptr, fp, uio),
int newsize = fp->_bf._size * 3 / 2;
if (newsize < curpos + len + 1)
newsize = curpos + len + 1;
- ptr = (unsigned char *)_realloc_r (_REENT,
- fp->_bf._base,
- newsize);
- if (!ptr)
+ str = (unsigned char *)_realloc_r (ptr, fp->_bf._base,
+ newsize);
+ if (!str)
{
/* Free buffer which is no longer used. */
- _free_r (_REENT, fp->_bf._base);
+ _free_r (ptr, fp->_bf._base);
goto err;
}
- fp->_bf._base = ptr;
- fp->_p = ptr + curpos;
+ fp->_bf._base = str;
+ fp->_p = str + curpos;
fp->_bf._size = newsize;
w = len;
fp->_w = newsize - curpos;
Index: libc/stdio/vasiprintf.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/vasiprintf.c,v
retrieving revision 1.1
diff -u -p -r1.1 vasiprintf.c
--- libc/stdio/vasiprintf.c 24 Nov 2004 00:45:41 -0000 1.1
+++ libc/stdio/vasiprintf.c 12 Mar 2007 13:05:01 -0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1990 The Regents of the University of California.
+ * Copyright (c) 1990, 2007 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
@@ -46,8 +46,11 @@ _DEFUN(vasiprintf, (strp, fmt, ap),
f._bf._size = f._w = 0;
f._file = -1; /* No file. */
ret = _vfiprintf_r (_REENT, &f, fmt, ap);
- *f._p = 0;
- *strp = f._bf._base;
+ if (ret >= 0)
+ {
+ *f._p = 0;
+ *strp = f._bf._base;
+ }
return ret;
}
@@ -68,8 +71,10 @@ _DEFUN(_vasiprintf_r, (ptr, strp, fmt, a
f._bf._size = f._w = 0;
f._file = -1; /* No file. */
ret = _vfiprintf_r (ptr, &f, fmt, ap);
- *f._p = 0;
- *strp = f._bf._base;
+ if (ret >= 0)
+ {
+ *f._p = 0;
+ *strp = f._bf._base;
+ }
return ret;
}
-
Index: libc/stdio/vasprintf.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/vasprintf.c,v
retrieving revision 1.4
diff -u -p -r1.4 vasprintf.c
--- libc/stdio/vasprintf.c 23 Apr 2004 20:01:55 -0000 1.4
+++ libc/stdio/vasprintf.c 12 Mar 2007 13:05:01 -0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1990 The Regents of the University of California.
+ * Copyright (c) 1990, 2007 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
@@ -46,8 +46,11 @@ _DEFUN(vasprintf, (strp, fmt, ap),
f._bf._size = f._w = 0;
f._file = -1; /* No file. */
ret = _vfprintf_r (_REENT, &f, fmt, ap);
- *f._p = 0;
- *strp = f._bf._base;
+ if (ret >= 0)
+ {
+ *f._p = 0;
+ *strp = f._bf._base;
+ }
return ret;
}
@@ -68,8 +71,10 @@ _DEFUN(_vasprintf_r, (ptr, strp, fmt, ap
f._bf._size = f._w = 0;
f._file = -1; /* No file. */
ret = _vfprintf_r (ptr, &f, fmt, ap);
- *f._p = 0;
- *strp = f._bf._base;
+ if (ret >= 0)
+ {
+ *f._p = 0;
+ *strp = f._bf._base;
+ }
return ret;
}
-