]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Define k5-platform.h wrapper for strerror_r
authorGreg Hudson <ghudson@mit.edu>
Sat, 5 Jul 2014 14:14:34 +0000 (10:14 -0400)
committerGreg Hudson <ghudson@mit.edu>
Tue, 8 Jul 2014 23:19:24 +0000 (19:19 -0400)
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
src/configure.in
src/include/k5-platform.h
src/util/support/Makefile.in
src/util/support/libkrb5support-fixed.exports
src/util/support/strerror_r.c [new file with mode: 0644]

index 7be77c28d1e308a89002dbe9a8aa2e310fbc8c39..a0d1bf1738f052b38aa47082bd5d8962baa8bb4f 100644 (file)
@@ -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.
 ])
index 1339379260fcba888b81ccd9f529a381d5cfdac6..8aa513e9128ee4f313e21246d7fe08aa9d9ad8e9 100644 (file)
@@ -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=
index e5fd004595b654e8c6b6851db15b2412decaa610..cff7dc32b80da7bd5c4fd9baa2654e5fcc441910 100644 (file)
@@ -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
index 979b4cd3ebfcf68e944eb39b1337fa4f8f74ce6b..51817621be738c27ab689389cfefa093f33b6bd9 100644 (file)
@@ -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.
index 4a76eef8880fe860520f81c93de04b4b8a57a090..b5ca0b28c58b759b66cb42f2e9a99dd6bdba465e 100644 (file)
@@ -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 (file)
index 0000000..e1ca565
--- /dev/null
@@ -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