ERR: Move ERR_set_mark(), ERR_pop_to_mark() and ERR_clear_last_mark()
[openssl.git] / crypto / err / err_mark.c
1 /*
2  * Copyright 2003-2022 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 #define OSSL_FORCE_ERR_STATE
11
12 #include <openssl/err.h>
13 #include "err_local.h"
14
15 int ERR_set_mark(void)
16 {
17     ERR_STATE *es;
18
19     es = ossl_err_get_state_int();
20     if (es == NULL)
21         return 0;
22
23     if (es->bottom == es->top)
24         return 0;
25     es->err_marks[es->top]++;
26     return 1;
27 }
28
29 int ERR_pop_to_mark(void)
30 {
31     ERR_STATE *es;
32
33     es = ossl_err_get_state_int();
34     if (es == NULL)
35         return 0;
36
37     while (es->bottom != es->top
38            && es->err_marks[es->top] == 0) {
39         err_clear(es, es->top, 0);
40         es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
41     }
42
43     if (es->bottom == es->top)
44         return 0;
45     es->err_marks[es->top]--;
46     return 1;
47 }
48
49 int ERR_clear_last_mark(void)
50 {
51     ERR_STATE *es;
52     int top;
53
54     es = ossl_err_get_state_int();
55     if (es == NULL)
56         return 0;
57
58     top = es->top;
59     while (es->bottom != top
60            && es->err_marks[top] == 0) {
61         top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
62     }
63
64     if (es->bottom == top)
65         return 0;
66     es->err_marks[top]--;
67     return 1;
68 }
69