]>
Commit | Line | Data |
---|---|---|
8a4dc425 | 1 | /* |
da1c088f | 2 | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
8a4dc425 RL |
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 | ||
ed49476a | 10 | #include <string.h> |
8a4dc425 RL |
11 | #include <openssl/err.h> |
12 | #include <openssl/e_os2.h> | |
13 | ||
14 | static ossl_inline void err_get_slot(ERR_STATE *es) | |
15 | { | |
16 | es->top = (es->top + 1) % ERR_NUM_ERRORS; | |
17 | if (es->top == es->bottom) | |
18 | es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS; | |
19 | } | |
20 | ||
21 | static ossl_inline void err_clear_data(ERR_STATE *es, size_t i, int deall) | |
22 | { | |
23 | if (es->err_data_flags[i] & ERR_TXT_MALLOCED) { | |
24 | if (deall) { | |
25 | OPENSSL_free(es->err_data[i]); | |
26 | es->err_data[i] = NULL; | |
27 | es->err_data_size[i] = 0; | |
28 | es->err_data_flags[i] = 0; | |
29 | } else if (es->err_data[i] != NULL) { | |
30 | es->err_data[i][0] = '\0'; | |
5a9dbfc5 | 31 | es->err_data_flags[i] = ERR_TXT_MALLOCED; |
8a4dc425 RL |
32 | } |
33 | } else { | |
34 | es->err_data[i] = NULL; | |
35 | es->err_data_size[i] = 0; | |
36 | es->err_data_flags[i] = 0; | |
37 | } | |
38 | } | |
39 | ||
40 | static ossl_inline void err_set_error(ERR_STATE *es, size_t i, | |
41 | int lib, int reason) | |
42 | { | |
71f2994b RL |
43 | es->err_buffer[i] = |
44 | lib == ERR_LIB_SYS | |
45 | ? (unsigned int)(ERR_SYSTEM_FLAG | reason) | |
46 | : ERR_PACK(lib, 0, reason); | |
8a4dc425 RL |
47 | } |
48 | ||
49 | static ossl_inline void err_set_debug(ERR_STATE *es, size_t i, | |
50 | const char *file, int line, | |
51 | const char *fn) | |
52 | { | |
de4a88a9 MC |
53 | /* |
54 | * We dup the file and fn strings because they may be provider owned. If the | |
55 | * provider gets unloaded, they may not be valid anymore. | |
56 | */ | |
57 | OPENSSL_free(es->err_file[i]); | |
58 | if (file == NULL || file[0] == '\0') | |
59 | es->err_file[i] = NULL; | |
ed49476a TM |
60 | else if ((es->err_file[i] = CRYPTO_malloc(strlen(file) + 1, |
61 | NULL, 0)) != NULL) | |
62 | /* We cannot use OPENSSL_strdup due to possible recursion */ | |
63 | strcpy(es->err_file[i], file); | |
64 | ||
8a4dc425 | 65 | es->err_line[i] = line; |
de4a88a9 MC |
66 | OPENSSL_free(es->err_func[i]); |
67 | if (fn == NULL || fn[0] == '\0') | |
68 | es->err_func[i] = NULL; | |
9c3ea4e1 TM |
69 | else if ((es->err_func[i] = CRYPTO_malloc(strlen(fn) + 1, |
70 | NULL, 0)) != NULL) | |
71 | strcpy(es->err_func[i], fn); | |
8a4dc425 RL |
72 | } |
73 | ||
74 | static ossl_inline void err_set_data(ERR_STATE *es, size_t i, | |
75 | void *data, size_t datasz, int flags) | |
76 | { | |
5a9dbfc5 P |
77 | if ((es->err_data_flags[i] & ERR_TXT_MALLOCED) != 0) |
78 | OPENSSL_free(es->err_data[i]); | |
8a4dc425 RL |
79 | es->err_data[i] = data; |
80 | es->err_data_size[i] = datasz; | |
81 | es->err_data_flags[i] = flags; | |
82 | } | |
83 | ||
84 | static ossl_inline void err_clear(ERR_STATE *es, size_t i, int deall) | |
85 | { | |
86 | err_clear_data(es, i, (deall)); | |
4e08ea6f | 87 | es->err_marks[i] = 0; |
8a4dc425 RL |
88 | es->err_flags[i] = 0; |
89 | es->err_buffer[i] = 0; | |
8a4dc425 | 90 | es->err_line[i] = -1; |
de4a88a9 MC |
91 | OPENSSL_free(es->err_file[i]); |
92 | es->err_file[i] = NULL; | |
93 | OPENSSL_free(es->err_func[i]); | |
94 | es->err_func[i] = NULL; | |
8a4dc425 | 95 | } |
e5d4233f | 96 | |
78715dcc | 97 | ERR_STATE *ossl_err_get_state_int(void); |
63132c53 RL |
98 | void ossl_err_string_int(unsigned long e, const char *func, |
99 | char *buf, size_t len); |