ERR: refactor useful inner macros to err_locl.h. Add function name field
authorRichard Levitte <levitte@openssl.org>
Wed, 24 Jul 2019 10:56:58 +0000 (12:56 +0200)
committerRichard Levitte <levitte@openssl.org>
Wed, 31 Jul 2019 04:42:37 +0000 (06:42 +0200)
The useful inner macros are now static inline functions.  That will
make them easier to debug in the future.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9452)

crypto/err/err.c
crypto/err/err_locl.h [new file with mode: 0644]
include/openssl/err.h

index 7a35512f879802ea08b18a1f3258f94856b32085..60f945c845916985fa7a529e67b310de8f2a557e 100644 (file)
@@ -22,6 +22,7 @@
 #include "internal/ctype.h"
 #include "internal/constant_time_locl.h"
 #include "e_os.h"
+#include "err_locl.h"
 
 static int err_load_strings(const ERR_STRING_DATA *str);
 
@@ -235,40 +236,6 @@ static void build_SYS_str_reasons(void)
 }
 #endif
 
-#define err_get_slot(p) \
-    do { \
-        (p)->top = ((p)->top + 1) % ERR_NUM_ERRORS; \
-        if ((p)->top == (p)->bottom) \
-            (p)->bottom = ((p)->bottom + 1) % ERR_NUM_ERRORS; \
-    } while (0)
-
-#define err_clear_data(p, i, deall)                             \
-        do {                                                    \
-            if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) {    \
-                if (deall) {                                    \
-                    OPENSSL_free((p)->err_data[i]);             \
-                    (p)->err_data[i] = NULL;                    \
-                    (p)->err_data_size[i] = 0;                  \
-                    (p)->err_data_flags[i] = 0;                 \
-                } else if ((p)->err_data[i] != NULL) {          \
-                    (p)->err_data[i][0] = '\0';                 \
-                }                                               \
-            } else {                                            \
-                (p)->err_data[i] = NULL;                        \
-                (p)->err_data_size[i] = 0;                      \
-                (p)->err_data_flags[i] = 0;                     \
-            }                                                   \
-        } while (0)
-
-#define err_clear(p, i, deall) \
-        do { \
-            err_clear_data((p), (i), (deall)); \
-            (p)->err_flags[i] = 0; \
-            (p)->err_buffer[i] = 0; \
-            (p)->err_file[i] = NULL; \
-            (p)->err_line[i] = -1; \
-        } while (0)
-
 static void ERR_STATE_free(ERR_STATE *s)
 {
     int i;
@@ -424,9 +391,6 @@ void ERR_put_error(int lib, int func, int reason, const char *file, int line)
 
     err_get_slot(es);
     err_clear(es, es->top, 0);
-    es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
-    es->err_file[es->top] = file;
-    es->err_line[es->top] = line;
 }
 
 void ERR_clear_error(void)
@@ -789,18 +753,13 @@ static int err_set_error_data_int(char *data, size_t size, int flags,
                                   int deallocate)
 {
     ERR_STATE *es;
-    int i;
 
     es = ERR_get_state();
     if (es == NULL)
         return 0;
 
-    i = es->top;
-
     err_clear_data(es, es->top, deallocate);
-    es->err_data[i] = data;
-    es->err_data_size[i] = size;
-    es->err_data_flags[i] = flags;
+    err_set_data(es, es->top, data, size, flags);
 
     return 1;
 }
diff --git a/crypto/err/err_locl.h b/crypto/err/err_locl.h
new file mode 100644 (file)
index 0000000..d45a00e
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * 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
+ */
+
+#include <openssl/err.h>
+#include <openssl/e_os2.h>
+
+static ossl_inline void err_get_slot(ERR_STATE *es)
+{
+    es->top = (es->top + 1) % ERR_NUM_ERRORS;
+    if (es->top == es->bottom)
+        es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
+}
+
+static ossl_inline void err_clear_data(ERR_STATE *es, size_t i, int deall)
+{
+    if (es->err_data_flags[i] & ERR_TXT_MALLOCED) {
+        if (deall) {
+            OPENSSL_free(es->err_data[i]);
+            es->err_data[i] = NULL;
+            es->err_data_size[i] = 0;
+            es->err_data_flags[i] = 0;
+        } else if (es->err_data[i] != NULL) {
+            es->err_data[i][0] = '\0';
+        }
+    } else {
+        es->err_data[i] = NULL;
+        es->err_data_size[i] = 0;
+        es->err_data_flags[i] = 0;
+    }
+}
+
+static ossl_inline void err_set_error(ERR_STATE *es, size_t i,
+                                      int lib, int reason)
+{
+    es->err_buffer[i] = ERR_PACK(lib, 0, reason);
+}
+
+static ossl_inline void err_set_debug(ERR_STATE *es, size_t i,
+                                      const char *file, int line,
+                                      const char *fn)
+{
+    es->err_file[i] = file;
+    es->err_line[i] = line;
+    es->err_func[i] = fn;
+}
+
+static ossl_inline void err_set_data(ERR_STATE *es, size_t i,
+                                     void *data, size_t datasz, int flags)
+{
+    es->err_data[i] = data;
+    es->err_data_size[i] = datasz;
+    es->err_data_flags[i] = flags;
+}
+
+static ossl_inline void err_clear(ERR_STATE *es, size_t i, int deall)
+{
+    err_clear_data(es, i, (deall));
+    es->err_flags[i] = 0;
+    es->err_buffer[i] = 0;
+    es->err_file[i] = NULL;
+    es->err_line[i] = -1;
+}
index e84bc68a4ea542da67f76fbff209360377f8a362..acecca462bb35b3d2af02df5e0b2a658966d4fda 100644 (file)
@@ -50,6 +50,7 @@ typedef struct err_state_st {
     int err_data_flags[ERR_NUM_ERRORS];
     const char *err_file[ERR_NUM_ERRORS];
     int err_line[ERR_NUM_ERRORS];
+    const char *err_func[ERR_NUM_ERRORS];
     int top, bottom;
 } ERR_STATE;