bug-gnulib
[Top][All Lists]
Advanced

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

fix bug in glibc 2.7 macro implementation of strtok_r.


From: Ben Pfaff
Subject: fix bug in glibc 2.7 macro implementation of strtok_r.
Date: Mon, 31 Aug 2009 05:56:38 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

This works around a bug in the glibc 2.7 macro implementation of
strtok_r described here:
http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.

Usually my gnulib submissions are intended primarily to help GNU
PSPP.  In this case, however, GNU PSPP doesn't actually trigger
the bug that this fixes.  The reason that I'm submitting it is
that I discovered this glibc bug myself, and it's been bothering
me ever since that gnulib doesn't detect and work around it,
because it seems very easy to trigger.

Comments?


commit 4df03d81af30b08367781281167a12fafce4502b
Author: Ben Pfaff <address@hidden>
Date:   Sun Aug 30 21:00:06 2009 -0700

    Fix bug in glibc 2.7 macro implementation of strtok_r.

diff --git a/ChangeLog b/ChangeLog
index 7dee308..a2009c9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2009-08-30  Ben Pfaff  <address@hidden>
+
+       Fix glibc 2.7 bug in macro implementation of strtok_r, seen at
+       least on Debian lenny systems.
+       * lib/string.in.h: Undefine strtok_r macro when bug is present (by
+       using out-of-line implementation instead).
+       * m4/string_h.m4: Add HAVE_BUGGY_STRTOK_R_MACRO substitution and
+       default value.
+       * m4/strtok_r.m4: New macro. gl_CHECK_BUGGY_STRTOK_R_MACRO.  Call
+       it from gl_FUNC_STRTOK_R.
+       * modules/string: Substitute HAVE_BUGGY_STRTOK_R_MACRO in
+       replacement string.h.
+       * doc/posix-functions/strtok_r.texi: Document.
+
 2009-08-30  Bruno Haible  <address@hidden>
 
        Fix an unnecessary error on Solaris 10 on NFSv3 file systems.
diff --git a/doc/posix-functions/strtok_r.texi 
b/doc/posix-functions/strtok_r.texi
index 7c0901e..1d958f9 100644
--- a/doc/posix-functions/strtok_r.texi
+++ b/doc/posix-functions/strtok_r.texi
@@ -9,6 +9,9 @@ Gnulib module: strtok_r
 Portability problems fixed by Gnulib:
 @itemize
 @item
+The macro version of this function segfaults on some inputs on some
+versions of glibc.
address@hidden
 This function is missing on some platforms:
 mingw.
 @end itemize
diff --git a/lib/string.in.h b/lib/string.in.h
index 8eaeeb8..96ba0d0 100644
--- a/lib/string.in.h
+++ b/lib/string.in.h
@@ -402,6 +402,11 @@ extern char *strcasestr (const char *haystack, const char 
*needle)
 # if ! @HAVE_DECL_STRTOK_R@
 extern char *strtok_r (char *restrict s, char const *restrict delim,
                       char **restrict save_ptr);
+# elif @HAVE_BUGGY_STRTOK_R_MACRO@
+/* Some of the forms of the strtok_r() macro implementation are
+   buggy, but the out-of-line implementation is fine, so force
+   the out-of-line implementation to be used. */
+#  undef strtok_r
 # endif
 # if defined GNULIB_POSIXCHECK
 #  undef strtok_r
diff --git a/m4/string_h.m4 b/m4/string_h.m4
index 11f09c8..128fb88 100644
--- a/m4/string_h.m4
+++ b/m4/string_h.m4
@@ -5,7 +5,7 @@
 # gives unlimited permission to copy and/or distribute it,
 # with or without modifications, as long as this notice is preserved.
 
-# serial 7
+# serial 8
 
 # Written by Paul Eggert.
 
@@ -81,6 +81,7 @@ AC_DEFUN([gl_HEADER_STRING_H_DEFAULTS],
   HAVE_STRSEP=1;               AC_SUBST([HAVE_STRSEP])
   HAVE_STRCASESTR=1;           AC_SUBST([HAVE_STRCASESTR])
   HAVE_DECL_STRTOK_R=1;                AC_SUBST([HAVE_DECL_STRTOK_R])
+  HAVE_BUGGY_STRTOK_R_MACRO=0;  AC_SUBST([HAVE_BUGGY_STRTOK_R_MACRO])
   HAVE_DECL_STRERROR=1;                AC_SUBST([HAVE_DECL_STRERROR])
   HAVE_DECL_STRSIGNAL=1;       AC_SUBST([HAVE_DECL_STRSIGNAL])
   HAVE_STRVERSCMP=1;           AC_SUBST([HAVE_STRVERSCMP])
diff --git a/m4/strtok_r.m4 b/m4/strtok_r.m4
index b79c2ee..492e4ae 100644
--- a/m4/strtok_r.m4
+++ b/m4/strtok_r.m4
@@ -1,4 +1,4 @@
-# strtok_r.m4 serial 8
+# strtok_r.m4 serial 9
 dnl Copyright (C) 2002, 2003, 2004, 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,
@@ -14,10 +14,41 @@ AC_DEFUN([gl_FUNC_STRTOK_R],
   AC_CHECK_DECLS_ONCE([strtok_r])
   if test $ac_cv_have_decl_strtok_r = no; then
     HAVE_DECL_STRTOK_R=0
+  else
+    gl_CHECK_BUGGY_STRTOK_R_MACRO
   fi
   gl_PREREQ_STRTOK_R
 ])
 
+dnl glibc 2.7 has a bug in strtok_r that causes a segmentation fault
+dnl when the second argument to strtok_r is a constant string that has
+dnl exactly one byte and compiling with optimization.  This bug is, for
+dnl example, present in the glibc 2.7-18 package in Debian "lenny".
+dnl
+dnl See also http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
+AC_DEFUN([gl_CHECK_BUGGY_STRTOK_R_MACRO],
+[
+  AC_CACHE_CHECK(
+     [for buggy strtok_r macro],
+     [gl_cv_buggy_strtok_r_macro],
+     [AC_RUN_IFELSE(
+        [AC_LANG_PROGRAM([#include <stdio.h>
+                          #include <string.h>
+                         ],
+                         [[char delimiters[] = "xxxxxxxx";
+                           char *save_ptr = (char *) 0xd0d0;
+                           strtok_r (delimiters, "x", &save_ptr);
+                           strtok_r (NULL, "x", &save_ptr);
+                           return 0;
+                          ]])],
+        [gl_cv_buggy_strtok_r_macro=no],
+        [gl_cv_buggy_strtok_r_macro=yes],
+        [gl_cv_buggy_strtok_r_macro=yes])])
+   if test $gl_cv_buggy_strtok_r_macro = yes; then
+     HAVE_BUGGY_STRTOK_R_MACRO=1
+   fi
+])
+
 # Prerequisites of lib/strtok_r.c.
 AC_DEFUN([gl_PREREQ_STRTOK_R], [
   :
diff --git a/modules/string b/modules/string
index 4073146..5e8c93b 100644
--- a/modules/string
+++ b/modules/string
@@ -73,6 +73,7 @@ string.h: string.in.h
              -e 's|@''HAVE_STRSEP''@|$(HAVE_STRSEP)|g' \
              -e 's|@''HAVE_STRCASESTR''@|$(HAVE_STRCASESTR)|g' \
              -e 's|@''HAVE_DECL_STRTOK_R''@|$(HAVE_DECL_STRTOK_R)|g' \
+             -e 
's|@''HAVE_BUGGY_STRTOK_R_MACRO''@|$(HAVE_BUGGY_STRTOK_R_MACRO)|g' \
              -e 's|@''HAVE_DECL_STRERROR''@|$(HAVE_DECL_STRERROR)|g' \
              -e 's|@''HAVE_DECL_STRSIGNAL''@|$(HAVE_DECL_STRSIGNAL)|g' \
              -e 's|@''HAVE_STRVERSCMP''@|$(HAVE_STRVERSCMP)|g' \

-- 
Ben Pfaff 
http://benpfaff.org




reply via email to

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