summaryrefslogtreecommitdiffstats
path: root/crypto/o_str.c
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2016-05-23 13:52:29 +0100
committerMatt Caswell <matt@openssl.org>2016-05-23 23:26:10 +0100
commit7d37818dacc87c21dfc9d2def5014657344875e3 (patch)
tree459cf7a75959c26b09b1e91be893b5f4fe27b3fe /crypto/o_str.c
parenta93e0e78db78e03bdcd29acf9bbc8a812ee50cb6 (diff)
Use strerror_r()/strerror_s() instead of strerror() where possible
The function strerror() is not thread safe. We should use strerror_r() where possible, or strerror_s() on Windows. RT#2267 Reviewed-by: Richard Levitte <levitte@openssl.org>
Diffstat (limited to 'crypto/o_str.c')
-rw-r--r--crypto/o_str.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/crypto/o_str.c b/crypto/o_str.c
index 0ee2c86d8b..98eb1631cb 100644
--- a/crypto/o_str.c
+++ b/crypto/o_str.c
@@ -258,3 +258,31 @@ char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)
return tmp;
}
+
+int openssl_strerror_r(int errnum, char *buf, size_t buflen)
+{
+#if defined(OPENSSL_SYS_WINDOWS)
+ if (strerror_s(buf, buflen, errnum) == EINVAL)
+ return 0;
+ return 1;
+#elif (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE
+ /*
+ * We can use "real" strerror_r. The OpenSSL version differs in that it
+ * gives 1 on success and 0 on failure for consistency with other OpenSSL
+ * functions. Real strerror_r does it the other way around
+ */
+ return !strerror_r(errnum, buf, buflen);
+#else
+ char *err;
+ /* Fall back to non-thread safe strerror()...its all we can do */
+ if (buflen < 2)
+ return 0;
+ err = strerror(errnum);
+ /* Can this ever happen? */
+ if (err == NULL)
+ return 0;
+ strncpy(buf, err, buflen - 1);
+ buf[buflen - 1] = '\0';
+ return 1;
+#endif
+}