Print thread IDs nicely.
[openssl.git] / crypto / err / err_prn.c
index a528d08d4ea347dde847c7df76b484c937329931..b43367f69439c66c7f25061e50e5aeeed27ff654 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
 void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
                          void *u)
 {
+    CRYPTO_THREAD_ID tid = CRYPTO_THREAD_get_current_id();
     unsigned long l;
     char buf[256];
-    char buf2[4096];
+    char buf2[4096], *hex;
     const char *file, *data;
     int line, flags;
-    /*
-     * We don't know what kind of thing CRYPTO_THREAD_ID is. Here is our best
-     * attempt to convert it into something we can print.
-     */
-    union {
-        CRYPTO_THREAD_ID tid;
-        unsigned long ltid;
-    } tid;
-
-    tid.ltid = 0;
-    tid.tid = CRYPTO_THREAD_get_current_id();
 
     while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
-        ERR_error_string_n(l, buf, sizeof buf);
-        BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", tid.ltid, buf,
-                     file, line, (flags & ERR_TXT_STRING) ? data : "");
+        ERR_error_string_n(l, buf, sizeof(buf));
+        hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
+        BIO_snprintf(buf2, sizeof(buf2), "%s:%s:%s:%d:%s\n", hex, buf, file,
+                     line, (flags & ERR_TXT_STRING) ? data : "");
+        OPENSSL_free(hex);
         if (cb(buf2, strlen(buf2), u) <= 0)
             break;              /* abort outputting the error report */
     }