[rrd-developers] [PATCH] Fixed and improved error reporting using strerror_r().
Sebastian Harl
sh at tokkee.org
Fri Feb 13 09:33:42 CET 2009
Even though POSIX/XSI requires "strerror_r" to return an "int", some systems
(e.g. the GNU libc) return a "char *" _and_ ignore the second argument (user
provided buffer). The configure script now checks for that behavior using
AC_FUNC_STRERROR_R. rrd_strerror() in rrd_thread_safe.c has been updated to
(hopefully) handle all possible cases.
Previously, rrd_strerror() would have returned "strerror_r failed. sorry!" in
mostly any cases when using glibc, since "if (strerror_r())" had been used to
check for errors which evaluates to true if a (non-NULL) pointer was returned.
Now, we, at least, return the error number in case anything else fails.
Thanks to Alessandro Iurlano for reporting this issue after spotting it in
collectd <http://collectd.org>.
---
Damn ... I just spotted an error in the ! STRERROR_R_CHAR_P case
(assigning ctx->lib_errstr to ret) - please consider this patch instead.
---
program/configure.ac | 4 +++-
program/src/rrd_thread_safe.c | 35 +++++++++++++++++++++++++++++++----
2 files changed, 34 insertions(+), 5 deletions(-)
diff --git a/program/configure.ac b/program/configure.ac
index 6cc2767..843e5b2 100644
--- a/program/configure.ac
+++ b/program/configure.ac
@@ -203,7 +203,9 @@ AC_C_BIGENDIAN
dnl for each function found we get a definition in config.h
dnl of the form HAVE_FUNCTION
-AC_CHECK_FUNCS(tzset fsync mbstowcs opendir readdir chdir chroot getuid setlocale strerror strerror_r snprintf vsnprintf fpclass class fp_class isnan memmove strchr mktime getrusage gettimeofday)
+AC_CHECK_FUNCS(tzset fsync mbstowcs opendir readdir chdir chroot getuid setlocale strerror snprintf vsnprintf fpclass class fp_class isnan memmove strchr mktime getrusage gettimeofday)
+
+AC_FUNC_STRERROR_R
CONFIGURE_PART(Map/Fadvis/Madvise checking)
diff --git a/program/src/rrd_thread_safe.c b/program/src/rrd_thread_safe.c
index c910b98..6e30115 100644
--- a/program/src/rrd_thread_safe.c
+++ b/program/src/rrd_thread_safe.c
@@ -59,11 +59,38 @@ const char *rrd_strerror(
int err)
{
rrd_context_t *ctx = rrd_get_context();
+ char *ret = "unknown error";
- if (strerror_r(err, ctx->lib_errstr, sizeof(ctx->lib_errstr)))
- return "strerror_r failed. sorry!";
- else
- return ctx->lib_errstr;
+ *ctx->lib_errstr = '\0';
+
+ /* Even though POSIX/XSI requires "strerror_r" to return an "int", some
+ * systems (e.g. the GNU libc) return a "char *" _and_ ignore the second
+ * argument ... -tokkee */
+#if STRERROR_R_CHAR_P
+ ret = strerror_r(err, ctx->lib_errstr, sizeof(ctx->lib_errstr));
+ if ((! ret) || (*ret == '\0')) {
+ if (*ctx->lib_errstr != '\0')
+ ret = ctx->lib_errstr;
+ else {
+ /* according to the manpage this should not happen -
+ let's handle it somehow sanely anyway */
+ snprintf(ctx->lib_errstr, sizeof(ctx->lib_errstr),
+ "unknown error %i - strerror_r did not return anything",
+ err);
+ ctx->lib_errstr[sizeof(ctx->lib_errstr) - 1] = '\0';
+ ret = ctx->lib_errstr;
+ }
+ }
+#else /* ! STRERROR_R_CHAR_P */
+ if (strerror_r(err, ctx->lib_errstr, sizeof(ctx->lib_errstr))) {
+ snprintf(ctx->lib_errstr, sizeof(ctx->lib_errstr),
+ "unknown error %i - strerror_r returned with errno = %i",
+ err, errno);
+ ctx->lib_errstr[sizeof(ctx->lib_errstr) - 1] = '\0';
+ }
+ ret = ctx->lib_errstr;
+#endif
+ return ret;
}
#else
#undef strerror
--
1.6.1.rc3
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.oetiker.ch/pipermail/rrd-developers/attachments/20090213/9d43f5df/attachment.bin
More information about the rrd-developers
mailing list