krb5 commit: Define k5-platform.h wrapper for strerror_r

Greg Hudson ghudson at MIT.EDU
Tue Jul 8 20:17:04 EDT 2014


https://github.com/krb5/krb5/commit/6351586a771e9a99e1e946cc9a0b6a87bbb14094
commit 6351586a771e9a99e1e946cc9a0b6a87bbb14094
Author: Greg Hudson <ghudson at mit.edu>
Date:   Sat Jul 5 10:14:34 2014 -0400

    Define k5-platform.h wrapper for strerror_r
    
    On systems where strerror_r is not the POSIX version, define it to
    k5_strerror_r.  Implement k5_strerror_r in libkrb5support using
    strerror_s, strerror, or the GNU strerror_r as appropriate.
    
    ticket: 7961

 src/aclocal.m4                                |    2 +-
 src/configure.in                              |    3 +-
 src/include/k5-platform.h                     |    6 ++
 src/util/support/Makefile.in                  |    5 +-
 src/util/support/libkrb5support-fixed.exports |    1 +
 src/util/support/strerror_r.c                 |   96 +++++++++++++++++++++++++
 6 files changed, 110 insertions(+), 3 deletions(-)

diff --git a/src/aclocal.m4 b/src/aclocal.m4
index 7be77c2..a0d1bf1 100644
--- a/src/aclocal.m4
+++ b/src/aclocal.m4
@@ -1,4 +1,4 @@
-AC_PREREQ(2.52)
+AC_PREREQ(2.53)
 AC_COPYRIGHT([Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
 Massachusetts Institute of Technology.
 ])
diff --git a/src/configure.in b/src/configure.in
index 1339379..8aa513e 100644
--- a/src/configure.in
+++ b/src/configure.in
@@ -369,7 +369,8 @@ AC_CONFIG_HEADERS(include/autoconf.h, [echo timestamp > include/autoconf.stamp])
 AC_PROG_LEX
 AC_C_CONST
 AC_HEADER_DIRENT
-AC_CHECK_FUNCS(strdup setvbuf seteuid setresuid setreuid setegid setresgid setregid setsid flock fchmod chmod strftime strptime geteuid setenv unsetenv getenv gmtime_r localtime_r bswap16 bswap64 mkstemp getusershell access getcwd srand48 srand srandom stat strchr strerror strerror_r timegm)
+AC_FUNC_STRERROR_R
+AC_CHECK_FUNCS(strdup setvbuf seteuid setresuid setreuid setegid setresgid setregid setsid flock fchmod chmod strftime strptime geteuid setenv unsetenv getenv gmtime_r localtime_r bswap16 bswap64 mkstemp getusershell access getcwd srand48 srand srandom stat strchr strerror timegm)
 
 AC_CHECK_FUNC(mkstemp,
 [MKSTEMP_ST_OBJ=
diff --git a/src/include/k5-platform.h b/src/include/k5-platform.h
index e5fd004..cff7dc3 100644
--- a/src/include/k5-platform.h
+++ b/src/include/k5-platform.h
@@ -38,6 +38,7 @@
  * + strlcpy/strlcat
  * + fnmatch
  * + [v]asprintf
+ * + strerror_r
  * + mkstemp
  * + zap (support function; macro is in k5-int.h)
  * + constant time memory comparison
@@ -998,6 +999,11 @@ extern int asprintf(char **, const char *, ...)
 #define SNPRINTF_OVERFLOW(result, size) \
     ((unsigned int)(result) >= (size_t)(size))
 
+#if defined(_WIN32) || !defined(HAVE_STRERROR_R) || defined(STRERROR_R_CHAR_P)
+#define strerror_r k5_strerror_r
+#endif
+extern int k5_strerror_r(int errnum, char *buf, size_t buflen);
+
 #ifndef HAVE_MKSTEMP
 extern int krb5int_mkstemp(char *);
 #define mkstemp krb5int_mkstemp
diff --git a/src/util/support/Makefile.in b/src/util/support/Makefile.in
index 979b4cd..5181762 100644
--- a/src/util/support/Makefile.in
+++ b/src/util/support/Makefile.in
@@ -72,6 +72,7 @@ STLIBOBJS= \
 	base64.o \
 	json.o \
 	bcmp.o \
+	strerror_r.o \
 	$(GETTIMEOFDAY_ST_OBJ) \
 	$(IPC_ST_OBJ) \
 	$(STRLCPY_ST_OBJ) \
@@ -94,6 +95,7 @@ LIBOBJS= \
 	$(OUTPRE)base64.$(OBJEXT) \
 	$(OUTPRE)json.$(OBJEXT) \
 	$(OUTPRE)bcmp.$(OBJEXT) \
+	$(OUTPRE)strerror_r.$(OBJEXT) \
 	$(GETTIMEOFDAY_OBJ) \
 	$(IPC_OBJ) \
 	$(STRLCPY_OBJ) \
@@ -124,7 +126,8 @@ SRCS=\
 	$(srcdir)/path.c \
 	$(srcdir)/base64.c \
 	$(srcdir)/json.c \
-	$(srcdir)/bcmp.c
+	$(srcdir)/bcmp.c \
+	$(srcdir)/strerror_r.c
 
 SHLIB_EXPDEPS =
 # Add -lm if dumping thread stats, for sqrt.
diff --git a/src/util/support/libkrb5support-fixed.exports b/src/util/support/libkrb5support-fixed.exports
index 4a76eef..b5ca0b2 100644
--- a/src/util/support/libkrb5support-fixed.exports
+++ b/src/util/support/libkrb5support-fixed.exports
@@ -47,6 +47,7 @@ k5_json_string_utf8
 k5_path_isabs
 k5_path_join
 k5_path_split
+k5_strerror_r
 krb5int_key_register
 krb5int_key_delete
 krb5int_getspecific
diff --git a/src/util/support/strerror_r.c b/src/util/support/strerror_r.c
new file mode 100644
index 0000000..e1ca565
--- /dev/null
+++ b/src/util/support/strerror_r.c
@@ -0,0 +1,96 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/* util/support/strerror_r.c - strerror_r compatibility shim */
+/*
+ * Copyright (C) 2014 by the Massachusetts Institute of Technology.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in
+ *   the documentation and/or other materials provided with the
+ *   distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <k5-platform.h>
+#undef strerror_r
+
+#if defined(_WIN32)
+
+/* Implement strerror_r in terms of strerror_s. */
+int
+k5_strerror_r(int errnum, char *buf, size_t buflen)
+{
+    int st;
+
+    st = strerror_s(buf, buflen, errnum);
+    if (st != 0) {
+        errno = st;
+        return -1;
+    }
+}
+
+#elif !defined(HAVE_STRERROR_R)
+
+/* Implement strerror_r in terms of strerror (not thread-safe). */
+int
+k5_strerror_r(int errnum, char *buf, size_t buflen)
+{
+    if (strlcpy(buf, strerror(errnum), buflen) >= buflen) {
+        errno = ERANGE;
+        return -1;
+    }
+    return 0;
+}
+
+#elif defined(STRERROR_R_CHAR_P)
+
+/*
+ * Implement the POSIX strerror_r API in terms of the GNU strerror_r, which
+ * returns a pointer to either the caller buffer or a constant string.  This is
+ * the default version on glibc systems when _GNU_SOURCE is defined.
+ */
+int
+k5_strerror_r(int errnum, char *buf, size_t buflen)
+{
+    const char *str;
+
+    str = strerror_r(errnum, buf, buflen);
+    if (str != buf) {
+        if (strlcpy(buf, str, buflen) >= buflen) {
+            errno = ERANGE;
+            return -1;
+        }
+    }
+    return 0;
+}
+
+#else
+
+/* Define a stub in terms of the real strerror_r, just to simplify the library
+ * export list.  This shouldn't get used. */
+int
+k5_strerror_r(int errnum, char *buf, size_t buflen)
+{
+    return strerror_r(errnum, buf, buflen);
+}
+
+#endif


More information about the cvs-krb5 mailing list