diff options
author | Simon Josefsson <simon@josefsson.org> | 2009-05-20 08:31:45 +0200 |
---|---|---|
committer | Simon Josefsson <simon@josefsson.org> | 2009-05-20 08:31:45 +0200 |
commit | 5507d4d53a7c68e6f961656102de076940fe3ce1 (patch) | |
tree | d904d621467091bae953ac9b017afb80625e896a | |
parent | 869eae6a7cd5c8687485e525813d2283cb42f76c (diff) | |
download | gnutls-5507d4d53a7c68e6f961656102de076940fe3ce1.tar.gz |
Replace vsnprintf if needed.
-rw-r--r-- | lib/gl/Makefile.am | 11 | ||||
-rw-r--r-- | lib/gl/m4/gnulib-cache.m4 | 3 | ||||
-rw-r--r-- | lib/gl/m4/gnulib-comp.m4 | 5 | ||||
-rw-r--r-- | lib/gl/m4/vsnprintf.m4 | 40 | ||||
-rw-r--r-- | lib/gl/tests/Makefile.am | 9 | ||||
-rw-r--r-- | lib/gl/tests/test-vsnprintf.c | 85 | ||||
-rw-r--r-- | lib/gl/vsnprintf.c | 71 |
7 files changed, 222 insertions, 2 deletions
diff --git a/lib/gl/Makefile.am b/lib/gl/Makefile.am index 821976bbf0..b512e46474 100644 --- a/lib/gl/Makefile.am +++ b/lib/gl/Makefile.am @@ -9,7 +9,7 @@ # the same distribution terms as the rest of that program. # # Generated by gnulib-tool. -# Reproduce by: gnulib-tool --import --dir=. --local-dir=gl/override --lib=liblgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lseek-tests --lgpl=2 --libtool --macro-prefix=lgl --no-vc-files byteswap c-ctype fseeko func gettext lib-msvc-compat lib-symbol-versions memmem-simple memmove minmax netdb read-file snprintf sockets socklen stdint strcase strverscmp sys_socket sys_stat time_r unistd vasprintf +# Reproduce by: gnulib-tool --import --dir=. --local-dir=gl/override --lib=liblgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lseek-tests --lgpl=2 --libtool --macro-prefix=lgl --no-vc-files byteswap c-ctype fseeko func gettext lib-msvc-compat lib-symbol-versions memmem-simple memmove minmax netdb read-file snprintf sockets socklen stdint strcase strverscmp sys_socket sys_stat time_r unistd vasprintf vsnprintf AUTOMAKE_OPTIONS = 1.5 gnits @@ -815,6 +815,15 @@ EXTRA_liblgnu_la_SOURCES += asprintf.c vasprintf.c ## end gnulib module vasprintf +## begin gnulib module vsnprintf + + +EXTRA_DIST += vsnprintf.c + +EXTRA_liblgnu_la_SOURCES += vsnprintf.c + +## end gnulib module vsnprintf + ## begin gnulib module wchar BUILT_SOURCES += $(WCHAR_H) diff --git a/lib/gl/m4/gnulib-cache.m4 b/lib/gl/m4/gnulib-cache.m4 index 46ea924cf8..eb846ec907 100644 --- a/lib/gl/m4/gnulib-cache.m4 +++ b/lib/gl/m4/gnulib-cache.m4 @@ -15,7 +15,7 @@ # Specification in the form of a command-line invocation: -# gnulib-tool --import --dir=. --local-dir=gl/override --lib=liblgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lseek-tests --lgpl=2 --libtool --macro-prefix=lgl --no-vc-files byteswap c-ctype fseeko func gettext lib-msvc-compat lib-symbol-versions memmem-simple memmove minmax netdb read-file snprintf sockets socklen stdint strcase strverscmp sys_socket sys_stat time_r unistd vasprintf +# gnulib-tool --import --dir=. --local-dir=gl/override --lib=liblgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lseek-tests --lgpl=2 --libtool --macro-prefix=lgl --no-vc-files byteswap c-ctype fseeko func gettext lib-msvc-compat lib-symbol-versions memmem-simple memmove minmax netdb read-file snprintf sockets socklen stdint strcase strverscmp sys_socket sys_stat time_r unistd vasprintf vsnprintf # Specification in the form of a few gnulib-tool.m4 macro invocations: gl_LOCAL_DIR([gl/override]) @@ -43,6 +43,7 @@ gl_MODULES([ time_r unistd vasprintf + vsnprintf ]) gl_AVOID([alignof-tests lseek-tests]) gl_SOURCE_BASE([gl]) diff --git a/lib/gl/m4/gnulib-comp.m4 b/lib/gl/m4/gnulib-comp.m4 index 9f3f51f6df..b54047abb0 100644 --- a/lib/gl/m4/gnulib-comp.m4 +++ b/lib/gl/m4/gnulib-comp.m4 @@ -93,6 +93,8 @@ AC_DEFUN([lgl_INIT], m4_ifdef([AM_XGETTEXT_OPTION], [AM_XGETTEXT_OPTION([--flag=asprintf:2:c-format]) AM_XGETTEXT_OPTION([--flag=vasprintf:2:c-format])]) + gl_FUNC_VSNPRINTF + gl_STDIO_MODULE_INDICATOR([vsnprintf]) gl_WCHAR_H gl_XSIZE m4_ifval(lgl_LIBSOURCES_LIST, [ @@ -281,6 +283,7 @@ AC_DEFUN([lgl_FILE_LIST], [ lib/vasnprintf.c lib/vasnprintf.h lib/vasprintf.c + lib/vsnprintf.c lib/w32sock.h lib/wchar.in.h lib/xsize.h @@ -354,6 +357,7 @@ AC_DEFUN([lgl_FILE_LIST], [ m4/vasnprintf.m4 m4/vasprintf.m4 m4/visibility.m4 + m4/vsnprintf.m4 m4/wchar.m4 m4/wchar_t.m4 m4/wint_t.m4 @@ -383,6 +387,7 @@ AC_DEFUN([lgl_FILE_LIST], [ tests/test-unistd.c tests/test-vasnprintf.c tests/test-vasprintf.c + tests/test-vsnprintf.c tests/test-wchar.c tests=lib/dummy.c tests=lib/intprops.h diff --git a/lib/gl/m4/vsnprintf.m4 b/lib/gl/m4/vsnprintf.m4 new file mode 100644 index 0000000000..3b37d460b3 --- /dev/null +++ b/lib/gl/m4/vsnprintf.m4 @@ -0,0 +1,40 @@ +# vsnprintf.m4 serial 5 +dnl Copyright (C) 2002-2004, 2007-2008 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_VSNPRINTF], +[ + AC_REQUIRE([gl_STDIO_H_DEFAULTS]) + gl_cv_func_vsnprintf_usable=no + AC_CHECK_FUNCS([vsnprintf]) + if test $ac_cv_func_vsnprintf = yes; then + gl_SNPRINTF_SIZE1 + case "$gl_cv_func_snprintf_size1" in + *yes) + gl_cv_func_vsnprintf_usable=yes + ;; + esac + fi + if test $gl_cv_func_vsnprintf_usable = no; then + gl_REPLACE_VSNPRINTF + fi + AC_CHECK_DECLS_ONCE([vsnprintf]) + if test $ac_cv_have_decl_vsnprintf = no; then + HAVE_DECL_VSNPRINTF=0 + fi +]) + +AC_DEFUN([gl_REPLACE_VSNPRINTF], +[ + AC_REQUIRE([gl_STDIO_H_DEFAULTS]) + AC_LIBOBJ([vsnprintf]) + if test $ac_cv_func_vsnprintf = yes; then + REPLACE_VSNPRINTF=1 + fi + gl_PREREQ_VSNPRINTF +]) + +# Prerequisites of lib/vsnprintf.c. +AC_DEFUN([gl_PREREQ_VSNPRINTF], [:]) diff --git a/lib/gl/tests/Makefile.am b/lib/gl/tests/Makefile.am index c6791747fa..26ed81bd82 100644 --- a/lib/gl/tests/Makefile.am +++ b/lib/gl/tests/Makefile.am @@ -259,6 +259,15 @@ libtests_a_SOURCES += verify.h ## end gnulib module verify +## begin gnulib module vsnprintf-tests + +TESTS += test-vsnprintf +check_PROGRAMS += test-vsnprintf + +EXTRA_DIST += test-vsnprintf.c + +## end gnulib module vsnprintf-tests + ## begin gnulib module wchar-tests TESTS += test-wchar diff --git a/lib/gl/tests/test-vsnprintf.c b/lib/gl/tests/test-vsnprintf.c new file mode 100644 index 0000000000..9c1be8d115 --- /dev/null +++ b/lib/gl/tests/test-vsnprintf.c @@ -0,0 +1,85 @@ +/* Test of vsnprintf() function. + Copyright (C) 2007-2008 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include <stdio.h> + +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +static int +my_snprintf (char *buf, int size, const char *format, ...) +{ + va_list args; + int ret; + + va_start (args, format); + ret = vsnprintf (buf, size, format, args); + va_end (args); + return ret; +} + +int +main (int argc, char *argv[]) +{ + char buf[8]; + int size; + int retval; + + for (size = 0; size <= 8; size++) + { + memcpy (buf, "DEADBEEF", 8); + retval = my_snprintf (buf, size, "%d", 12345); + if (size < 6) + { +#if CHECK_VSNPRINTF_POSIX + ASSERT (retval < 0 || retval >= size); +#endif + if (size > 0) + { + ASSERT (memcmp (buf, "12345", size - 1) == 0); + ASSERT (buf[size - 1] == '\0' || buf[size - 1] == '0' + size); + } +#if !CHECK_VSNPRINTF_POSIX + if (size > 0) +#endif + ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0); + } + else + { + ASSERT (retval == 5); + ASSERT (memcmp (buf, "12345\0EF", 8) == 0); + } + } + + return 0; +} diff --git a/lib/gl/vsnprintf.c b/lib/gl/vsnprintf.c new file mode 100644 index 0000000000..d487be8996 --- /dev/null +++ b/lib/gl/vsnprintf.c @@ -0,0 +1,71 @@ +/* Formatted output to strings. + Copyright (C) 2004, 2006-2008 Free Software Foundation, Inc. + Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1, 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +/* Specification. */ +#include <stdio.h> + +#include <errno.h> +#include <limits.h> +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> + +#include "vasnprintf.h" + +/* Print formatted output to string STR. Similar to vsprintf, but + additional length SIZE limit how much is written into STR. Returns + string length of formatted string (which may be larger than SIZE). + STR may be NULL, in which case nothing will be written. On error, + return a negative value. */ +int +vsnprintf (char *str, size_t size, const char *format, va_list args) +{ + char *output; + size_t len; + size_t lenbuf = size; + + output = vasnprintf (str, &lenbuf, format, args); + len = lenbuf; + + if (!output) + return -1; + + if (output != str) + { + if (size) + { + size_t pruned_len = (len < size ? len : size - 1); + memcpy (str, output, pruned_len); + str[pruned_len] = '\0'; + } + + free (output); + } + + if (len > INT_MAX) + { + errno = EOVERFLOW; + return -1; + } + + return len; +} |