bug-gnulib
[Top][All Lists]
Advanced

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

Re: %.1s format with vasnprintf reads more than one byte from argument


From: Eric Blake
Subject: Re: %.1s format with vasnprintf reads more than one byte from argument
Date: Fri, 27 Feb 2009 06:12:07 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.19) Gecko/20081209 Thunderbird/2.0.0.19 Mnenhy/0.7.6.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Eric Blake on 2/26/2009 7:49 PM:
> According to Bruno Haible on 2/26/2009 6:13 PM:
>> I removed the part of the test that should normally always fail:
>>     if (sprintf (buf, "%ls", wstring) != -1)
>>       return 2;
>> because it does not fail on any known platform.
> 
> Actually, it DOES fail, on cygwin 1.7.0.  But that is due to a bug in
> cygwin wctomb[1], which will hopefully be fixed soon, and not to sprintf
> accessing too far in the wchar_t array, as the %.2ls test passes.

Even worse.  Cygwin 1.5.x appears to do just fine on 2-character strings,
but mishandles 1-character strings, always returning -1, and without
setting errno!  I guess this failure can be chalked up to the fact that
cygwin 1.5.x made no attempt at multibyte support.

$ cat foo.c
#include <stdio.h>
#include <wchar.h>
#include <errno.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
  char buf[100];
  errno = buf[0] = 0;
  printf ("%d", sprintf (buf, "%ls", L"a"));
  printf (":%d:%s\n", errno, buf);
  errno = buf[0] = 0;
  printf ("%d", sprintf (buf, "%.1ls", L"a"));
  printf (":%d:%s\n", errno, buf);
  errno = buf[0] = 0;
  printf ("%d", sprintf (buf, "%.1ls", L"ab"));
  printf (":%d:%s\n", errno, buf);
  errno = buf[0] = 0;
  printf ("%d", sprintf (buf, "%.2ls", L"a"));
  printf (":%d:%s\n", errno, buf);
  errno = buf[0] = 0;
  printf ("%d", sprintf (buf, "%.2ls", L"ab"));
  printf (":%d:%s\n", errno, buf);
  errno = buf[0] = 0;
  printf ("%d", sprintf (buf, "%ls", L"ab"));
  printf (":%d:%s\n", errno, buf);
  return 0;
}
$ ./foo
- -1:0:
- -1:0:
- -1:0:
- -1:0:
2:0:ab
2:0:ab

Cygwin 1.7 is still an unreleased beta snapshot, so I am working with the
cygwin developers on getting that in shape, with the goal that it will not
need any gnulib assistance (now that we have identified the issues).
Meanwhile, I'm applying this patch to filter out the brain-dead cygwin
1.5.x implementation, since no further development on cygwin 1.5.x is
planned.  With the patch in place, both cygwin 1.5.x and cygwin 1.7 pass
'gnulib-tool --with-tests --test snprintf-posix'.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkmn5qcACgkQ84KuGfSFAYAeyACeP8CTWpCZsFfeU5sKGz9weO2o
K1oAoIxhMSOl8oZN1X63RzwMVVfOGwP0
=hzzl
-----END PGP SIGNATURE-----
>From 599e3ff6dc2aa17ee80ad76f893cf82c2ddeb8d6 Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Fri, 27 Feb 2009 06:06:47 -0700
Subject: [PATCH] Detect bug in cygwin 1.5.x *printf on 1-character %ls.

* m4/printf.m4 (gl_PRINTF_DIRECTIVE_LS): Enhance filter.
* doc/posix-functions/fprintf.texi: Update.
* doc/posix-functions/printf.texi: Update.
* doc/posix-functions/snprintf.texi: Update.
* doc/posix-functions/sprintf.texi: Update.
* doc/posix-functions/vfprintf.texi: Update.
* doc/posix-functions/vprintf.texi: Update.
* doc/posix-functions/vsnprintf.texi: Update.
* doc/posix-functions/vsprintf.texi: Update.
* doc/glibc-functions/obstack_printf.texi: Update.
* doc/glibc-functions/obstack_vprintf.texi: Update.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog                                |   15 +++++++++++++++
 doc/glibc-functions/obstack_printf.texi  |    2 +-
 doc/glibc-functions/obstack_vprintf.texi |    2 +-
 doc/posix-functions/fprintf.texi         |    2 +-
 doc/posix-functions/printf.texi          |    2 +-
 doc/posix-functions/snprintf.texi        |    2 +-
 doc/posix-functions/sprintf.texi         |    2 +-
 doc/posix-functions/vfprintf.texi        |    2 +-
 doc/posix-functions/vprintf.texi         |    2 +-
 doc/posix-functions/vsnprintf.texi       |    2 +-
 doc/posix-functions/vsprintf.texi        |    2 +-
 m4/printf.m4                             |   18 +++++++++++++-----
 12 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 90f91f6..c0428c0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2009-02-27  Eric Blake  <address@hidden>
+
+       Detect bug in cygwin 1.5.x *printf on 1-character %ls.
+       * m4/printf.m4 (gl_PRINTF_DIRECTIVE_LS): Enhance filter.
+       * doc/posix-functions/fprintf.texi: Update.
+       * doc/posix-functions/printf.texi: Update.
+       * doc/posix-functions/snprintf.texi: Update.
+       * doc/posix-functions/sprintf.texi: Update.
+       * doc/posix-functions/vfprintf.texi: Update.
+       * doc/posix-functions/vprintf.texi: Update.
+       * doc/posix-functions/vsnprintf.texi: Update.
+       * doc/posix-functions/vsprintf.texi: Update.
+       * doc/glibc-functions/obstack_printf.texi: Update.
+       * doc/glibc-functions/obstack_vprintf.texi: Update.
+
 2009-02-26  Eric Blake  <address@hidden>

        avoid gcc 3.4.3 bug on long double NaN on Irix 6.5
diff --git a/doc/glibc-functions/obstack_printf.texi 
b/doc/glibc-functions/obstack_printf.texi
index 7a57264..3f09487 100644
--- a/doc/glibc-functions/obstack_printf.texi
+++ b/doc/glibc-functions/obstack_printf.texi
@@ -36,7 +36,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/doc/glibc-functions/obstack_vprintf.texi 
b/doc/glibc-functions/obstack_vprintf.texi
index 8556f74..477b40a 100644
--- a/doc/glibc-functions/obstack_vprintf.texi
+++ b/doc/glibc-functions/obstack_vprintf.texi
@@ -36,7 +36,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/doc/posix-functions/fprintf.texi b/doc/posix-functions/fprintf.texi
index 0434e44..70230b9 100644
--- a/doc/posix-functions/fprintf.texi
+++ b/doc/posix-functions/fprintf.texi
@@ -30,7 +30,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/doc/posix-functions/printf.texi b/doc/posix-functions/printf.texi
index 1783ad2..4475e90 100644
--- a/doc/posix-functions/printf.texi
+++ b/doc/posix-functions/printf.texi
@@ -30,7 +30,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/doc/posix-functions/snprintf.texi 
b/doc/posix-functions/snprintf.texi
index d8ecbcc..e4cfee1 100644
--- a/doc/posix-functions/snprintf.texi
+++ b/doc/posix-functions/snprintf.texi
@@ -41,7 +41,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/doc/posix-functions/sprintf.texi b/doc/posix-functions/sprintf.texi
index 003fb44..0ef53b8 100644
--- a/doc/posix-functions/sprintf.texi
+++ b/doc/posix-functions/sprintf.texi
@@ -30,7 +30,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/doc/posix-functions/vfprintf.texi 
b/doc/posix-functions/vfprintf.texi
index 37c2d11..9e54bf9 100644
--- a/doc/posix-functions/vfprintf.texi
+++ b/doc/posix-functions/vfprintf.texi
@@ -30,7 +30,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/doc/posix-functions/vprintf.texi b/doc/posix-functions/vprintf.texi
index 4531172..d69bd73 100644
--- a/doc/posix-functions/vprintf.texi
+++ b/doc/posix-functions/vprintf.texi
@@ -30,7 +30,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/doc/posix-functions/vsnprintf.texi 
b/doc/posix-functions/vsnprintf.texi
index f09e054..0b41da5 100644
--- a/doc/posix-functions/vsnprintf.texi
+++ b/doc/posix-functions/vsnprintf.texi
@@ -41,7 +41,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/doc/posix-functions/vsprintf.texi 
b/doc/posix-functions/vsprintf.texi
index d477b4b..6d7b111 100644
--- a/doc/posix-functions/vsprintf.texi
+++ b/doc/posix-functions/vsprintf.texi
@@ -30,7 +30,7 @@ NetBSD 3.0, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, 
Solaris 9,
 Cygwin 1.5.x, mingw, BeOS.
 @item
 This function does not support the @samp{ls} directive on some platforms:
-OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.
+OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku, Cygwin 1.5.x.
 @item
 This function does not support precisions in the @samp{ls} directive correctly
 on some platforms:
diff --git a/m4/printf.m4 b/m4/printf.m4
index 4207ace..6da62df 100644
--- a/m4/printf.m4
+++ b/m4/printf.m4
@@ -1,4 +1,4 @@
-# printf.m4 serial 31
+# printf.m4 serial 32
 dnl Copyright (C) 2003, 2007-2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -558,7 +558,7 @@ int main ()
   if (sprintf (buf, "%F", 1.0 / 0.0) < 0
       || (strcmp (buf, "INF") != 0 && strcmp (buf, "INFINITY") != 0))
     return 1;
-  /* This catches a Cygwin 2007 bug.  */
+  /* This catches a Cygwin 1.5.x bug.  */
   if (sprintf (buf, "%.F", 1234.0) < 0
       || strcmp (buf, "1234") != 0)
     return 1;
@@ -653,13 +653,19 @@ int main ()
 {
   char buf[100];
   /* Test whether %ls works at all.
-     This test fails on OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku.  */
+     This test fails on OpenBSD 4.0, IRIX 6.5, Solaris 2.6, Haiku,
+     Cygwin 1.5.  */
   {
     static const wchar_t wstring[] = { 'a', 'b', 'c', 0 };
     buf[0] = '\0';
     if (sprintf (buf, "%ls", wstring) < 0
         || strcmp (buf, "abc") != 0)
       return 1;
+    buf[0] = '\0';
+    wstring[1] = 0;
+    if (sprintf (buf, "%ls", wstring) < 0
+        || strcmp (buf, "a") != 0)
+      return 1;
   }
   /* Test whether precisions in %ls are supported as specified in ISO C 99
      section 7.19.6.1:
@@ -685,6 +691,7 @@ changequote(,)dnl
          solaris*)        gl_cv_func_printf_directive_ls="guessing no";;
          irix*)           gl_cv_func_printf_directive_ls="guessing no";;
          beos* | haiku*)  gl_cv_func_printf_directive_ls="guessing no";;
+         cygwin*)         gl_cv_func_printf_directive_ls="guessing no";;
          *)               gl_cv_func_printf_directive_ls="guessing yes";;
        esac
 changequote([,])dnl
@@ -1384,8 +1391,9 @@ dnl   glibc 2.3.6                    .  .  .  .  #  .  .  
.  .  .  .  .  .  .  .
 dnl   FreeBSD 5.4, 6.1               .  .  .  .  #  .  .  .  .  .  .  #  .  #  
.  .  .  .  .  .
 dnl   MacOS X 10.3.9                 .  .  .  .  #  .  .  .  .  .  .  #  .  #  
.  .  .  .  .  .
 dnl   OpenBSD 3.9, 4.0               .  .  #  #  #  #  .  #  .  #  .  #  .  #  
.  .  .  .  .  .
-dnl   Cygwin 2007 (= Cygwin 1.5.24)  .  .  .  .  #  #  .  .  .  .  ?  #  ?  ?  
.  .  .  .  .  .
-dnl   Cygwin 2006 (= Cygwin 1.5.19)  #  .  .  .  #  #  .  ?  .  #  ?  #  ?  ?  
.  .  .  .  .  .
+dnl   Cygwin 1.7.0 (2009)            .  .  .  #  .  .  .  ?  .  .  .  .  .  ?  
.  .  .  .  .  .
+dnl   Cygwin 1.5.25 (2008)           .  .  .  #  #  .  .  #  .  .  .  .  .  #  
.  .  .  .  .  .
+dnl   Cygwin 1.5.19 (2006)           #  .  .  #  #  #  .  #  .  #  .  #  #  #  
.  .  .  .  .  .
 dnl   Solaris 10                     .  .  #  #  #  .  .  #  .  .  .  #  .  .  
.  .  .  .  .  .
 dnl   Solaris 2.6 ... 9              #  .  #  #  #  #  .  #  .  .  .  #  .  .  
.  .  .  .  .  .
 dnl   Solaris 2.5.1                  #  .  #  #  #  #  .  #  .  .  .  #  .  .  
#  #  #  #  #  #
-- 
1.6.1.2


reply via email to

[Prev in Thread] Current Thread [Next in Thread]