Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/err/err_save.c
Line
Count
Source
1
/*
2
 * Copyright 2023-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/err.h>
11
#include "err_local.h"
12
13
/*
14
 * Save and restore error state.
15
 * We are using CRYPTO_zalloc(.., NULL, 0) instead of OPENSSL_malloc() in
16
 * these functions to prevent mem alloc error loop.
17
 */
18
19
ERR_STATE *OSSL_ERR_STATE_new(void)
20
3
{
21
3
    return CRYPTO_zalloc(sizeof(ERR_STATE), NULL, 0);
22
3
}
23
24
void OSSL_ERR_STATE_save(ERR_STATE *es)
25
0
{
26
0
    int i;
27
0
    ERR_STATE *thread_es;
28
29
0
    if (es == NULL)
30
0
        return;
31
32
0
    for (i = 0; i < ERR_NUM_ERRORS; i++)
33
0
        err_clear(es, i, 1);
34
35
0
    thread_es = ossl_err_get_state_int();
36
0
    if (thread_es == NULL)
37
0
        return;
38
39
0
    memcpy(es, thread_es, sizeof(*es));
40
    /* Taking over the pointers, just clear the thread state. */
41
0
    memset(thread_es, 0, sizeof(*thread_es));
42
0
}
43
44
void OSSL_ERR_STATE_save_to_mark(ERR_STATE *es)
45
0
{
46
0
    int i, j, count;
47
0
    int top;
48
0
    ERR_STATE *thread_es;
49
50
0
    if (es == NULL)
51
0
        return;
52
53
0
    thread_es = ossl_err_get_state_int();
54
0
    if (thread_es == NULL) {
55
0
        for (i = 0; i < ERR_NUM_ERRORS; ++i)
56
0
            err_clear(es, i, 1);
57
58
0
        es->top = es->bottom = 0;
59
0
        return;
60
0
    }
61
62
    /* Determine number of errors we are going to move. */
63
0
    for (count = 0, top = thread_es->top;
64
0
        thread_es->bottom != top
65
0
        && thread_es->err_marks[top] == 0;
66
0
        ++count)
67
0
        top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
68
69
    /* Move the errors, preserving order. */
70
0
    for (i = 0, j = top; i < count; ++i) {
71
0
        j = (j + 1) % ERR_NUM_ERRORS;
72
73
0
        err_clear(es, i, 1);
74
75
        /* Move the error entry to the given ERR_STATE. */
76
0
        es->err_flags[i] = thread_es->err_flags[j];
77
0
        es->err_marks[i] = 0;
78
0
        es->err_buffer[i] = thread_es->err_buffer[j];
79
0
        es->err_data[i] = thread_es->err_data[j];
80
0
        es->err_data_size[i] = thread_es->err_data_size[j];
81
0
        es->err_data_flags[i] = thread_es->err_data_flags[j];
82
0
        es->err_file[i] = thread_es->err_file[j];
83
0
        es->err_line[i] = thread_es->err_line[j];
84
0
        es->err_func[i] = thread_es->err_func[j];
85
86
0
        thread_es->err_flags[j] = 0;
87
0
        thread_es->err_buffer[j] = 0;
88
0
        thread_es->err_data[j] = NULL;
89
0
        thread_es->err_data_size[j] = 0;
90
0
        thread_es->err_data_flags[j] = 0;
91
0
        thread_es->err_file[j] = NULL;
92
0
        thread_es->err_line[j] = 0;
93
0
        thread_es->err_func[j] = NULL;
94
0
    }
95
96
0
    if (i > 0) {
97
0
        thread_es->top = top;
98
        /* If we moved anything, es's stack always starts at [0]. */
99
0
        es->top = i - 1;
100
0
        es->bottom = ERR_NUM_ERRORS - 1;
101
0
    } else {
102
        /* Didn't move anything - empty stack */
103
0
        es->top = es->bottom = 0;
104
0
    }
105
106
    /* Erase extra space as a precaution. */
107
0
    for (; i < ERR_NUM_ERRORS; ++i)
108
0
        err_clear(es, i, 1);
109
0
}
110
111
void OSSL_ERR_STATE_restore(const ERR_STATE *es)
112
0
{
113
0
    int i;
114
0
    ERR_STATE *thread_es;
115
116
0
    if (es == NULL || es->bottom == es->top)
117
0
        return;
118
119
0
    thread_es = ossl_err_get_state_int();
120
0
    if (thread_es == NULL)
121
0
        return;
122
123
0
    for (i = es->bottom; i != es->top;) {
124
0
        int top;
125
126
0
        i = (i + 1) % ERR_NUM_ERRORS;
127
0
        if ((es->err_flags[i] & ERR_FLAG_CLEAR) != 0)
128
0
            continue;
129
130
0
        err_get_slot(thread_es);
131
0
        top = thread_es->top;
132
0
        err_clear(thread_es, top, 0);
133
134
0
        thread_es->err_flags[top] = es->err_flags[i];
135
0
        thread_es->err_buffer[top] = es->err_buffer[i];
136
137
0
        err_set_debug(thread_es, top, es->err_file[i], es->err_line[i],
138
0
            es->err_func[i]);
139
140
0
        if (es->err_data[i] != NULL && es->err_data_size[i] != 0) {
141
0
            void *data;
142
0
            size_t data_sz = es->err_data_size[i];
143
144
0
            data = CRYPTO_malloc(data_sz, NULL, 0);
145
0
            if (data != NULL) {
146
0
                memcpy(data, es->err_data[i], data_sz);
147
0
                err_set_data(thread_es, top, data, data_sz,
148
0
                    es->err_data_flags[i] | ERR_TXT_MALLOCED);
149
0
            }
150
0
        } else {
151
0
            err_clear_data(thread_es, top, 0);
152
0
        }
153
0
    }
154
0
}