]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
ossl_err_get_state_int(): Avoid saving the last sys error if not needed
authorTomas Mraz <tomas@openssl.org>
Wed, 25 Feb 2026 17:15:47 +0000 (18:15 +0100)
committerNeil Horman <nhorman@openssl.org>
Thu, 16 Apr 2026 18:12:41 +0000 (14:12 -0400)
In calls like ERR_set_mark(), ERR_clear_last_mark() and
others, there is no point in saving the last sys error.

It can be potentially expensive (on Windows).

Reviewed-by: Simo Sorce <simo@redhat.com>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
MergeDate: Thu Apr 16 18:12:49 2026
(Merged from https://github.com/openssl/openssl/pull/30179)

crypto/err/err.c
crypto/err/err_blocks.c
crypto/err/err_local.h
crypto/err/err_mark.c
crypto/err/err_save.c

index b3e15196f67c8ecfc2072703dd922165e42176e5..bf637e1d45e7a673a2c84a7c3c57c79df6f98fe6 100644 (file)
@@ -25,9 +25,6 @@
 #include "internal/e_os.h"
 #include "err_local.h"
 
-/* Forward declaration in case it's not published because of configuration */
-ERR_STATE *ERR_get_state(void);
-
 #ifndef OPENSSL_NO_ERR
 static int err_load_strings(const ERR_STRING_DATA *str);
 #endif
@@ -331,7 +328,7 @@ void ERR_clear_error(void)
     int i;
     ERR_STATE *es;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(0);
     if (es == NULL)
         return;
 
@@ -445,7 +442,7 @@ static unsigned long get_error_values(ERR_GET_ACTION g,
     ERR_STATE *es;
     unsigned long ret;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(1);
     if (es == NULL)
         return 0;
 
@@ -648,10 +645,13 @@ static void err_delete_thread_state(void *unused)
     OSSL_ERR_STATE_free(state);
 }
 
-ERR_STATE *ossl_err_get_state_int(void)
+ERR_STATE *ossl_err_get_state_int(int save_sys_error)
 {
     ERR_STATE *state;
-    int saveerrno = get_last_sys_error();
+    int saveerrno = 0;
+
+    if (save_sys_error)
+        saveerrno = get_last_sys_error();
 
     if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
         return NULL;
@@ -686,7 +686,8 @@ ERR_STATE *ossl_err_get_state_int(void)
         OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
     }
 
-    set_sys_error(saveerrno);
+    if (save_sys_error)
+        set_sys_error(saveerrno);
     return state;
 }
 
@@ -753,7 +754,7 @@ static int err_set_error_data_int(char *data, size_t size, int flags,
 {
     ERR_STATE *es;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(1);
     if (es == NULL)
         return 0;
 
@@ -799,7 +800,7 @@ void ERR_add_error_vdata(int num, va_list args)
     ERR_STATE *es;
 
     /* Get the current error data; if an allocated string get it. */
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(1);
     if (es == NULL)
         return;
     i = es->top;
@@ -856,7 +857,7 @@ void err_clear_last_constant_time(int clear)
     ERR_STATE *es;
     int top;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(0);
     if (es == NULL)
         return;
 
index cc0fce041967f0245486902de77aa99d6e717cff..90451ea880a0c340f5ab363b42c0e3cf029e00c9 100644 (file)
@@ -15,7 +15,7 @@ void ERR_new(void)
 {
     ERR_STATE *es;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(1);
     if (es == NULL)
         return;
 
@@ -28,7 +28,7 @@ void ERR_set_debug(const char *file, int line, const char *func)
 {
     ERR_STATE *es;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(1);
     if (es == NULL)
         return;
 
@@ -52,7 +52,7 @@ void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
     unsigned long flags = 0;
     size_t i;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(1);
     if (es == NULL)
         return;
     i = es->top;
index 775d8e6bdffc2dd19f2dca1b5cfe2a97f4344e2f..4ddf7f7664c644a5c1b73430dcf6bcd2cfbca1d6 100644 (file)
@@ -112,6 +112,6 @@ static ossl_inline void err_clear(ERR_STATE *es, size_t i, int deall)
     es->err_func[i] = NULL;
 }
 
-ERR_STATE *ossl_err_get_state_int(void);
+ERR_STATE *ossl_err_get_state_int(int save_sys_error);
 void ossl_err_string_int(unsigned long e, const char *func,
     char *buf, size_t len);
index 2ebe86b9de5c519a97fe1718ed605e0171236e50..29aef24a129fd7c82d09c69e3c434fa26c96e86f 100644 (file)
@@ -14,7 +14,7 @@ int ERR_set_mark(void)
 {
     ERR_STATE *es;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(0);
     if (es == NULL)
         return 0;
 
@@ -28,7 +28,7 @@ int ERR_pop(void)
 {
     ERR_STATE *es;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(0);
     if (es == NULL || es->bottom == es->top)
         return 0;
 
@@ -41,7 +41,7 @@ int ERR_pop_to_mark(void)
 {
     ERR_STATE *es;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(0);
     if (es == NULL)
         return 0;
 
@@ -62,7 +62,7 @@ int ERR_count_to_mark(void)
     ERR_STATE *es;
     int count = 0, top;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(1);
     if (es == NULL)
         return 0;
 
@@ -81,7 +81,7 @@ int ERR_clear_last_mark(void)
     ERR_STATE *es;
     int top;
 
-    es = ossl_err_get_state_int();
+    es = ossl_err_get_state_int(0);
     if (es == NULL)
         return 0;
 
index 37980c884550488e174c45d220c53041f09b8122..62f5d752dc775406c34e65b2a430a83eb893ff69 100644 (file)
@@ -32,7 +32,7 @@ void OSSL_ERR_STATE_save(ERR_STATE *es)
     for (i = 0; i < ERR_NUM_ERRORS; i++)
         err_clear(es, i, 1);
 
-    thread_es = ossl_err_get_state_int();
+    thread_es = ossl_err_get_state_int(1);
     if (thread_es == NULL)
         return;
 
@@ -50,7 +50,7 @@ void OSSL_ERR_STATE_save_to_mark(ERR_STATE *es)
     if (es == NULL)
         return;
 
-    thread_es = ossl_err_get_state_int();
+    thread_es = ossl_err_get_state_int(1);
     if (thread_es == NULL) {
         for (i = 0; i < ERR_NUM_ERRORS; ++i)
             err_clear(es, i, 1);
@@ -116,7 +116,7 @@ void OSSL_ERR_STATE_restore(const ERR_STATE *es)
     if (es == NULL || es->bottom == es->top)
         return;
 
-    thread_es = ossl_err_get_state_int();
+    thread_es = ossl_err_get_state_int(0);
     if (thread_es == NULL)
         return;