diff options
author | Stef Walter <stef@thewalter.net> | 2013-07-17 18:08:11 +0200 |
---|---|---|
committer | Stef Walter <stef@thewalter.net> | 2013-07-18 13:04:37 +0200 |
commit | 9a1fe66f08149596567fedb4e2338ae786a19ab9 (patch) | |
tree | 562ee2eedc17f0dd83f62890d3b4d12f02ee5a72 /common | |
parent | e403f7b33ac35e961c72ed1b6335bbe3084e4642 (diff) |
Avoid using the non-thread-safe strerror() function
https://bugzilla.redhat.com/show_bug.cgi?id=985481
Diffstat (limited to 'common')
-rw-r--r-- | common/compat.c | 16 | ||||
-rw-r--r-- | common/compat.h | 8 | ||||
-rw-r--r-- | common/message.c | 26 | ||||
-rw-r--r-- | common/message.h | 4 | ||||
-rw-r--r-- | common/path.c | 3 |
5 files changed, 55 insertions, 2 deletions
diff --git a/common/compat.c b/common/compat.c index 3b1361c..e7bee3c 100644 --- a/common/compat.c +++ b/common/compat.c @@ -807,3 +807,19 @@ getauxval (unsigned long type) } #endif /* HAVE_GETAUXVAL */ + +#ifndef HAVE_STRERROR_R + +int +strerror_r (int errnum, + char *buf, + size_t buflen) +{ +#ifdef OS_WIN32 + return strerror_s (buf, buflen, errnum); +#else + #error no strerror_r implementation +#endif +} + +#endif /* HAVE_STRERROR_R */ diff --git a/common/compat.h b/common/compat.h index 1cedc35..5b76d00 100644 --- a/common/compat.h +++ b/common/compat.h @@ -310,4 +310,12 @@ unsigned long getauxval (unsigned long type); #endif /* !HAVE_GETAUXVAL */ +#ifndef HAVE_STRERROR_R + +int strerror_r (int errnum, + char *buf, + size_t buflen); + +#endif /* HAVE_STRERROR_R */ + #endif /* __COMPAT_H__ */ diff --git a/common/message.c b/common/message.c index 8b54ad1..e68dfac 100644 --- a/common/message.c +++ b/common/message.c @@ -86,6 +86,32 @@ p11_message_store (const char* msg, } void +p11_message_err (int errnum, + const char* msg, + ...) +{ + char buffer[P11_MESSAGE_MAX]; + char strerr[P11_MESSAGE_MAX]; + va_list va; + size_t length; + + va_start (va, msg); + length = vsnprintf (buffer, P11_MESSAGE_MAX - 1, msg, va); + va_end (va); + + /* Was it truncated? */ + if (length > P11_MESSAGE_MAX - 1) + length = P11_MESSAGE_MAX - 1; + buffer[length] = 0; + + strncpy (strerr, "Unknown error", sizeof (strerr)); + strerror_r (errnum, strerr, sizeof (strerr)); + strerr[P11_MESSAGE_MAX - 1] = 0; + + p11_message ("%s: %s", buffer, strerr); +} + +void p11_message (const char* msg, ...) { diff --git a/common/message.h b/common/message.h index 60a7f81..3fe86df 100644 --- a/common/message.h +++ b/common/message.h @@ -48,6 +48,10 @@ extern char * (* p11_message_storage) (void); void p11_message (const char* msg, ...) GNUC_PRINTF (1, 2); +void p11_message_err (int errnum, + const char* msg, + ...) GNUC_PRINTF (2, 3); + void p11_message_store (const char* msg, size_t length); diff --git a/common/path.c b/common/path.c index 2f976a8..f7bd2b9 100644 --- a/common/path.c +++ b/common/path.c @@ -137,8 +137,7 @@ expand_homedir (const char *remainder) pwd = getpwuid (getuid ()); if (!pwd) { error = errno; - p11_message ("couldn't lookup home directory for user %d: %s", - getuid (), strerror (errno)); + p11_message_err (errno, "couldn't lookup home directory for user %d", getuid ()); errno = error; return NULL; } |