QUIC TLS: Rethink error handling
[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_count_to_mark(void)
50 {
51     ERR_STATE *es;
52     int count = 0, 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         ++count;
62         top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
63     }
64
65     return count;
66 }
67
68 int ERR_clear_last_mark(void)
69 {
70     ERR_STATE *es;
71     int top;
72
73     es = ossl_err_get_state_int();
74     if (es == NULL)
75         return 0;
76
77     top = es->top;
78     while (es->bottom != top
79            && es->err_marks[top] == 0) {
80         top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
81     }
82
83     if (es->bottom == top)
84         return 0;
85     es->err_marks[top]--;
86     return 1;
87 }
88