diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2004-10-04 04:09:41 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2004-10-04 04:09:41 +0000 |
commit | 55fbe6686af907ac025888e290c5b8ef879b5879 (patch) | |
tree | 32340229b3778f12901c6b9dd99401212a8d2f23 /lib | |
parent | 14f5062c301fcfb23fb265b9352cc2aa952036c9 (diff) | |
download | coreutils-55fbe6686af907ac025888e290c5b8ef879b5879.tar.xz |
Sync from gnulib.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/dirfd.h | 19 | ||||
-rw-r--r-- | lib/getpagesize.h | 18 | ||||
-rw-r--r-- | lib/vasnprintf.c | 13 | ||||
-rw-r--r-- | lib/vasnprintf.h | 21 | ||||
-rw-r--r-- | lib/vasprintf.c | 14 |
5 files changed, 71 insertions, 14 deletions
diff --git a/lib/dirfd.h b/lib/dirfd.h index 422f97663..714f69624 100644 --- a/lib/dirfd.h +++ b/lib/dirfd.h @@ -1,3 +1,22 @@ +/* Declare dirfd, if necessary. + Copyright (C) 2001, 2002 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Written by Jim Meyering. */ + #if HAVE_CONFIG_H # include <config.h> #endif diff --git a/lib/getpagesize.h b/lib/getpagesize.h index fb5b91c08..80d9eb3be 100644 --- a/lib/getpagesize.h +++ b/lib/getpagesize.h @@ -1,4 +1,20 @@ -/* Emulate getpagesize on systems that lack it. */ +/* Emulate getpagesize on systems that lack it. + Copyright (C) 1999, 2000 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ #ifndef HAVE_GETPAGESIZE diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index e24e4cd86..d49bc559e 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -40,7 +40,7 @@ #include <stdlib.h> /* abort(), malloc(), realloc(), free() */ #include <string.h> /* memcpy(), strlen() */ #include <errno.h> /* errno */ -#include <limits.h> /* CHAR_BIT */ +#include <limits.h> /* CHAR_BIT, INT_MAX */ #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */ #if WIDE_CHAR_VERSION # include "wprintf-parse.h" @@ -875,8 +875,19 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar free (buf_malloced); CLEANUP (); *lengthp = length; + if (length > INT_MAX) + goto length_overflow; return result; + length_overflow: + /* We could produce such a big string, but its length doesn't fit into + an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in + this case. */ + if (result != resultbuf) + free (result); + errno = EOVERFLOW; + return NULL; + out_of_memory: if (!(result == resultbuf || result == NULL)) free (result); diff --git a/lib/vasnprintf.h b/lib/vasnprintf.h index 4edb95d65..271210699 100644 --- a/lib/vasnprintf.h +++ b/lib/vasnprintf.h @@ -1,5 +1,5 @@ /* vsprintf with automatic memory allocation. - Copyright (C) 2002-2003 Free Software Foundation, Inc. + Copyright (C) 2002-2004 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -47,7 +47,24 @@ extern "C" { If successful, return the address of the string (this may be = RESULTBUF if no dynamic memory allocation was necessary) and set *LENGTHP to the number of resulting bytes, excluding the trailing NUL. Upon error, set - errno and return NULL. */ + errno and return NULL. + + When dynamic memory allocation occurs, the preallocated buffer is left + alone (with possibly modified contents). This makes it possible to use + a statically allocated or stack-allocated buffer, like this: + + char buf[100]; + size_t len = sizeof (buf); + char *output = vasnprintf (buf, &len, format, args); + if (output == NULL) + ... error handling ...; + else + { + ... use the output string ...; + if (output != buf) + free (output); + } + */ extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...) __attribute__ ((__format__ (__printf__, 3, 4))); extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args) diff --git a/lib/vasprintf.c b/lib/vasprintf.c index bda9aa1b0..ba94c64ba 100644 --- a/lib/vasprintf.c +++ b/lib/vasprintf.c @@ -1,5 +1,5 @@ /* Formatted output to strings. - Copyright (C) 1999, 2002-2003 Free Software Foundation, Inc. + Copyright (C) 1999, 2002-2004 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,7 +22,6 @@ /* Specification. */ #include "vasprintf.h" -#include <limits.h> #include <stdlib.h> #include "vasnprintf.h" @@ -34,15 +33,10 @@ vasprintf (char **resultp, const char *format, va_list args) char *result = vasnprintf (NULL, &length, format, args); if (result == NULL) return -1; - if (length > INT_MAX) - { - /* We could produce such a big string, but can't return its length - as an 'int'. */ - free (result); - return -1; - } *resultp = result; - /* Return the number of resulting bytes, excluding the trailing NUL. */ + /* Return the number of resulting bytes, excluding the trailing NUL. + If it wouldn't fit in an 'int', vasnprintf() would have returned NULL + and set errno to EOVERFLOW. */ return length; } |