blob: d74144fe6520d028bfbb4f8bb1519721668bb41a [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
Bob Beckfc524c12023-02-07 14:32:41 -0700109// Ensure we can't call OPENSSL_malloc circularly.
110#define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <openssl/err.h>
112
113#include <assert.h>
114#include <errno.h>
115#include <inttypes.h>
Bob Beckfc524c12023-02-07 14:32:41 -0700116#include <limits.h>
117#include <stdarg.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -0800118#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119
Yoshisato Yanagisawab7725cf2014-08-18 11:39:09 -0700120#if defined(OPENSSL_WINDOWS)
David Benjamina353cdb2016-06-09 16:48:33 -0400121OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langley3e719312015-03-20 16:32:23 -0700122#include <windows.h>
David Benjamina353cdb2016-06-09 16:48:33 -0400123OPENSSL_MSVC_PRAGMA(warning(pop))
Yoshisato Yanagisawab7725cf2014-08-18 11:39:09 -0700124#endif
125
Adam Langley95c29f32014-06-20 12:00:00 -0700126#include <openssl/mem.h>
127#include <openssl/thread.h>
128
Adam Langleyb9e77a02015-03-31 18:36:20 -0700129#include "../internal.h"
David Benjaminb25a8992017-10-01 21:47:07 -0400130#include "./internal.h"
Adam Langleyb9e77a02015-03-31 18:36:20 -0700131
Adam Langley95c29f32014-06-20 12:00:00 -0700132
David Benjaminb25a8992017-10-01 21:47:07 -0400133struct err_error_st {
134 // file contains the filename where the error occurred.
135 const char *file;
Bob Beckfc524c12023-02-07 14:32:41 -0700136 // data contains a NUL-terminated string with optional data. It is allocated
137 // with system |malloc| and must be freed with |free| (not |OPENSSL_free|)
David Benjaminb25a8992017-10-01 21:47:07 -0400138 char *data;
139 // packed contains the error library and reason, as packed by ERR_PACK.
140 uint32_t packed;
141 // line contains the line number where the error occurred.
142 uint16_t line;
143 // mark indicates a reversion point in the queue. See |ERR_pop_to_mark|.
144 unsigned mark : 1;
145};
146
147// ERR_STATE contains the per-thread, error queue.
148typedef struct err_state_st {
David Benjamin0f222e62023-07-03 12:24:51 -0400149 // errors contains up to ERR_NUM_ERRORS - 1 most recent errors, organised as a
150 // ring buffer.
David Benjaminb25a8992017-10-01 21:47:07 -0400151 struct err_error_st errors[ERR_NUM_ERRORS];
David Benjamin0f222e62023-07-03 12:24:51 -0400152 // top contains the index of the most recent error. If |top| equals |bottom|
153 // then the queue is empty.
David Benjaminb25a8992017-10-01 21:47:07 -0400154 unsigned top;
David Benjamin0f222e62023-07-03 12:24:51 -0400155 // bottom contains the index before the least recent error in the queue.
David Benjaminb25a8992017-10-01 21:47:07 -0400156 unsigned bottom;
157
158 // to_free, if not NULL, contains a pointer owned by this structure that was
159 // previously a |data| pointer of one of the elements of |errors|.
160 void *to_free;
161} ERR_STATE;
162
David Benjamind27eda02015-03-05 01:19:27 -0500163extern const uint32_t kOpenSSLReasonValues[];
164extern const size_t kOpenSSLReasonValuesLen;
165extern const char kOpenSSLReasonStringData[];
166
David Benjamin808f8322017-08-18 14:06:02 -0400167// err_clear clears the given queued error.
Adam Langley95c29f32014-06-20 12:00:00 -0700168static void err_clear(struct err_error_st *error) {
Bob Beckfc524c12023-02-07 14:32:41 -0700169 free(error->data);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500170 OPENSSL_memset(error, 0, sizeof(struct err_error_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700171}
172
David Benjaminb25a8992017-10-01 21:47:07 -0400173static void err_copy(struct err_error_st *dst, const struct err_error_st *src) {
174 err_clear(dst);
175 dst->file = src->file;
176 if (src->data != NULL) {
Bob Beckfc524c12023-02-07 14:32:41 -0700177 // Disable deprecated functions on msvc so it doesn't complain about strdup.
178 OPENSSL_MSVC_PRAGMA(warning(push))
179 OPENSSL_MSVC_PRAGMA(warning(disable : 4996))
180 // We can't use OPENSSL_strdup because we don't want to call OPENSSL_malloc,
181 // which can affect the error stack.
182 dst->data = strdup(src->data);
183 OPENSSL_MSVC_PRAGMA(warning(pop))
David Benjaminb25a8992017-10-01 21:47:07 -0400184 }
185 dst->packed = src->packed;
186 dst->line = src->line;
187}
188
Bob Beckfc524c12023-02-07 14:32:41 -0700189
David Benjamin808f8322017-08-18 14:06:02 -0400190// global_next_library contains the next custom library value to return.
Adam Langley2e0f0712015-03-31 16:04:09 -0700191static int global_next_library = ERR_NUM_LIBS;
192
David Benjamin808f8322017-08-18 14:06:02 -0400193// global_next_library_mutex protects |global_next_library| from concurrent
194// updates.
David Benjamin04c3d402023-06-03 01:26:29 -0400195static CRYPTO_MUTEX global_next_library_mutex = CRYPTO_MUTEX_INIT;
Adam Langley8bdd5422015-04-13 11:04:09 -0700196
Adam Langleyb9e77a02015-03-31 18:36:20 -0700197static void err_state_free(void *statep) {
198 ERR_STATE *state = statep;
Adam Langley2e0f0712015-03-31 16:04:09 -0700199
Adam Langleyb9e77a02015-03-31 18:36:20 -0700200 if (state == NULL) {
201 return;
202 }
203
David Benjaminb25a8992017-10-01 21:47:07 -0400204 for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) {
Adam Langleyb9e77a02015-03-31 18:36:20 -0700205 err_clear(&state->errors[i]);
206 }
Bob Beckfc524c12023-02-07 14:32:41 -0700207 free(state->to_free);
208 free(state);
Adam Langley2e0f0712015-03-31 16:04:09 -0700209}
210
David Benjamin808f8322017-08-18 14:06:02 -0400211// err_get_state gets the ERR_STATE object for the current thread.
Adam Langley95c29f32014-06-20 12:00:00 -0700212static ERR_STATE *err_get_state(void) {
Adam Langleyb9e77a02015-03-31 18:36:20 -0700213 ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR);
Adam Langley2e0f0712015-03-31 16:04:09 -0700214 if (state == NULL) {
Bob Beckfc524c12023-02-07 14:32:41 -0700215 state = malloc(sizeof(ERR_STATE));
Adam Langleyb9e77a02015-03-31 18:36:20 -0700216 if (state == NULL) {
217 return NULL;
218 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500219 OPENSSL_memset(state, 0, sizeof(ERR_STATE));
Adam Langley8bdd5422015-04-13 11:04:09 -0700220 if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state,
221 err_state_free)) {
Adam Langleyb9e77a02015-03-31 18:36:20 -0700222 return NULL;
223 }
Adam Langley2e0f0712015-03-31 16:04:09 -0700224 }
225
Adam Langley2e0f0712015-03-31 16:04:09 -0700226 return state;
Adam Langley95c29f32014-06-20 12:00:00 -0700227}
228
229static uint32_t get_error_values(int inc, int top, const char **file, int *line,
Adam Langley5f1374e2014-10-01 15:21:01 -0700230 const char **data, int *flags) {
Adam Langley95c29f32014-06-20 12:00:00 -0700231 unsigned i = 0;
232 ERR_STATE *state;
233 struct err_error_st *error;
234 uint32_t ret;
235
236 state = err_get_state();
Adam Langley69a01602014-11-17 17:26:55 -0800237 if (state == NULL || state->bottom == state->top) {
Adam Langley95c29f32014-06-20 12:00:00 -0700238 return 0;
239 }
240
241 if (top) {
David Benjamin0d824822014-11-08 12:51:36 -0500242 assert(!inc);
David Benjamin808f8322017-08-18 14:06:02 -0400243 // last error
Adam Langley95c29f32014-06-20 12:00:00 -0700244 i = state->top;
245 } else {
246 i = (state->bottom + 1) % ERR_NUM_ERRORS;
247 }
248
249 error = &state->errors[i];
250 ret = error->packed;
251
252 if (file != NULL && line != NULL) {
253 if (error->file == NULL) {
254 *file = "NA";
255 *line = 0;
256 } else {
257 *file = error->file;
258 *line = error->line;
259 }
260 }
261
262 if (data != NULL) {
263 if (error->data == NULL) {
264 *data = "";
265 if (flags != NULL) {
266 *flags = 0;
267 }
268 } else {
269 *data = error->data;
270 if (flags != NULL) {
Bob Beckfc524c12023-02-07 14:32:41 -0700271 // Without |ERR_FLAG_MALLOCED|, rust-openssl assumes the string has a
272 // static lifetime. In both cases, we retain ownership of the string,
273 // and the caller is not expected to free it.
274 *flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED;
Adam Langley95c29f32014-06-20 12:00:00 -0700275 }
David Benjamin808f8322017-08-18 14:06:02 -0400276 // If this error is being removed, take ownership of data from
277 // the error. The semantics are such that the caller doesn't
278 // take ownership either. Instead the error system takes
279 // ownership and retains it until the next call that affects the
280 // error queue.
David Benjamin0d824822014-11-08 12:51:36 -0500281 if (inc) {
David Benjamine1c3dad2017-09-28 16:33:09 -0400282 if (error->data != NULL) {
Bob Beckfc524c12023-02-07 14:32:41 -0700283 free(state->to_free);
David Benjamin0d824822014-11-08 12:51:36 -0500284 state->to_free = error->data;
Adam Langley5f1374e2014-10-01 15:21:01 -0700285 }
David Benjamin0d824822014-11-08 12:51:36 -0500286 error->data = NULL;
Adam Langley5f1374e2014-10-01 15:21:01 -0700287 }
Adam Langley95c29f32014-06-20 12:00:00 -0700288 }
289 }
290
291 if (inc) {
292 assert(!top);
293 err_clear(error);
294 state->bottom = i;
295 }
296
297 return ret;
298}
299
300uint32_t ERR_get_error(void) {
David Benjamin34248d42015-06-28 23:36:21 -0400301 return get_error_values(1 /* inc */, 0 /* bottom */, NULL, NULL, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700302}
303
304uint32_t ERR_get_error_line(const char **file, int *line) {
David Benjamin34248d42015-06-28 23:36:21 -0400305 return get_error_values(1 /* inc */, 0 /* bottom */, file, line, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700306}
307
308uint32_t ERR_get_error_line_data(const char **file, int *line,
Adam Langley5f1374e2014-10-01 15:21:01 -0700309 const char **data, int *flags) {
David Benjamin34248d42015-06-28 23:36:21 -0400310 return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700311}
312
313uint32_t ERR_peek_error(void) {
David Benjamin34248d42015-06-28 23:36:21 -0400314 return get_error_values(0 /* peek */, 0 /* bottom */, NULL, NULL, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700315}
316
317uint32_t ERR_peek_error_line(const char **file, int *line) {
David Benjamin34248d42015-06-28 23:36:21 -0400318 return get_error_values(0 /* peek */, 0 /* bottom */, file, line, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700319}
320
321uint32_t ERR_peek_error_line_data(const char **file, int *line,
322 const char **data, int *flags) {
David Benjamin34248d42015-06-28 23:36:21 -0400323 return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data,
324 flags);
325}
326
Adam Langley95c29f32014-06-20 12:00:00 -0700327uint32_t ERR_peek_last_error(void) {
David Benjamin34248d42015-06-28 23:36:21 -0400328 return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700329}
330
331uint32_t ERR_peek_last_error_line(const char **file, int *line) {
David Benjamin34248d42015-06-28 23:36:21 -0400332 return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700333}
334
335uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
336 const char **data, int *flags) {
David Benjamin34248d42015-06-28 23:36:21 -0400337 return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700338}
339
340void ERR_clear_error(void) {
341 ERR_STATE *const state = err_get_state();
342 unsigned i;
343
Adam Langley69a01602014-11-17 17:26:55 -0800344 if (state == NULL) {
345 return;
346 }
347
Adam Langley95c29f32014-06-20 12:00:00 -0700348 for (i = 0; i < ERR_NUM_ERRORS; i++) {
349 err_clear(&state->errors[i]);
350 }
Bob Beckfc524c12023-02-07 14:32:41 -0700351 free(state->to_free);
David Benjamincca4ba72015-04-22 15:49:27 -0400352 state->to_free = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
354 state->top = state->bottom = 0;
355}
356
Adam Langley03d31ed2014-08-05 16:54:47 -0700357void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
Adam Langleyb9e77a02015-03-31 18:36:20 -0700358 if (tid != NULL) {
359 assert(0);
Adam Langley2e0f0712015-03-31 16:04:09 -0700360 return;
361 }
362
Adam Langleyb9e77a02015-03-31 18:36:20 -0700363 ERR_clear_error();
Adam Langley03d31ed2014-08-05 16:54:47 -0700364}
365
David Benjaminc44d2f42014-08-20 16:24:00 -0400366int ERR_get_next_error_library(void) {
Adam Langley2e0f0712015-03-31 16:04:09 -0700367 int ret;
368
David Benjamin04c3d402023-06-03 01:26:29 -0400369 CRYPTO_MUTEX_lock_write(&global_next_library_mutex);
Adam Langley2e0f0712015-03-31 16:04:09 -0700370 ret = global_next_library++;
David Benjamin04c3d402023-06-03 01:26:29 -0400371 CRYPTO_MUTEX_unlock_write(&global_next_library_mutex);
Adam Langley2e0f0712015-03-31 16:04:09 -0700372
373 return ret;
Adam Langley40426672014-07-01 16:48:28 -0700374}
375
Matt Braithwaite9b68e722015-05-06 13:57:29 -0700376void ERR_remove_state(unsigned long pid) {
377 ERR_clear_error();
378}
379
Adam Langley95c29f32014-06-20 12:00:00 -0700380void ERR_clear_system_error(void) {
381 errno = 0;
382}
383
Adam Langley29b18672015-02-06 11:52:16 -0800384// err_string_cmp is a compare function for searching error values with
385// |bsearch| in |err_string_lookup|.
386static int err_string_cmp(const void *a, const void *b) {
387 const uint32_t a_key = *((const uint32_t*) a) >> 15;
388 const uint32_t b_key = *((const uint32_t*) b) >> 15;
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langley29b18672015-02-06 11:52:16 -0800390 if (a_key < b_key) {
391 return -1;
392 } else if (a_key > b_key) {
393 return 1;
394 } else {
395 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700396 }
Adam Langley95c29f32014-06-20 12:00:00 -0700397}
398
David Benjamin808f8322017-08-18 14:06:02 -0400399// err_string_lookup looks up the string associated with |lib| and |key| in
400// |values| and |string_data|. It returns the string or NULL if not found.
Adam Langley29b18672015-02-06 11:52:16 -0800401static const char *err_string_lookup(uint32_t lib, uint32_t key,
402 const uint32_t *values,
403 size_t num_values,
404 const char *string_data) {
David Benjamin808f8322017-08-18 14:06:02 -0400405 // |values| points to data in err_data.h, which is generated by
406 // err_data_generate.go. It's an array of uint32_t values. Each value has the
407 // following structure:
408 // | lib | key | offset |
409 // |6 bits| 11 bits | 15 bits |
410 //
411 // The |lib| value is a library identifier: one of the |ERR_LIB_*| values.
412 // The |key| is a reason code, depending on the context.
413 // The |offset| is the number of bytes from the start of |string_data| where
414 // the (NUL terminated) string for this value can be found.
415 //
416 // Values are sorted based on treating the |lib| and |key| part as an
417 // unsigned integer.
Adam Langley29b18672015-02-06 11:52:16 -0800418 if (lib >= (1 << 6) || key >= (1 << 11)) {
419 return NULL;
420 }
421 uint32_t search_key = lib << 26 | key << 15;
422 const uint32_t *result = bsearch(&search_key, values, num_values,
423 sizeof(uint32_t), err_string_cmp);
424 if (result == NULL) {
425 return NULL;
426 }
427
428 return &string_data[(*result) & 0x7fff];
429}
430
David Benjamindbad7452024-02-20 15:06:57 -0500431typedef struct library_name_st {
432 const char *str;
433 const char *symbol;
434 const char *reason_symbol;
435} LIBRARY_NAME;
436
437static const LIBRARY_NAME kLibraryNames[ERR_NUM_LIBS] = {
438 {"invalid library (0)", NULL, NULL},
439 {"unknown library", "NONE", "NONE_LIB"},
440 {"system library", "SYS", "SYS_LIB"},
441 {"bignum routines", "BN", "BN_LIB"},
442 {"RSA routines", "RSA", "RSA_LIB"},
443 {"Diffie-Hellman routines", "DH", "DH_LIB"},
444 {"public key routines", "EVP", "EVP_LIB"},
445 {"memory buffer routines", "BUF", "BUF_LIB"},
446 {"object identifier routines", "OBJ", "OBJ_LIB"},
447 {"PEM routines", "PEM", "PEM_LIB"},
448 {"DSA routines", "DSA", "DSA_LIB"},
449 {"X.509 certificate routines", "X509", "X509_LIB"},
450 {"ASN.1 encoding routines", "ASN1", "ASN1_LIB"},
451 {"configuration file routines", "CONF", "CONF_LIB"},
452 {"common libcrypto routines", "CRYPTO", "CRYPTO_LIB"},
453 {"elliptic curve routines", "EC", "EC_LIB"},
454 {"SSL routines", "SSL", "SSL_LIB"},
455 {"BIO routines", "BIO", "BIO_LIB"},
456 {"PKCS7 routines", "PKCS7", "PKCS7_LIB"},
457 {"PKCS8 routines", "PKCS8", "PKCS8_LIB"},
458 {"X509 V3 routines", "X509V3", "X509V3_LIB"},
459 {"random number generator", "RAND", "RAND_LIB"},
460 {"ENGINE routines", "ENGINE", "ENGINE_LIB"},
461 {"OCSP routines", "OCSP", "OCSP_LIB"},
462 {"UI routines", "UI", "UI_LIB"},
463 {"COMP routines", "COMP", "COMP_LIB"},
464 {"ECDSA routines", "ECDSA", "ECDSA_LIB"},
465 {"ECDH routines", "ECDH", "ECDH_LIB"},
466 {"HMAC routines", "HMAC", "HMAC_LIB"},
467 {"Digest functions", "DIGEST", "DIGEST_LIB"},
468 {"Cipher functions", "CIPHER", "CIPHER_LIB"},
469 {"HKDF functions", "HKDF", "HKDF_LIB"},
470 {"Trust Token functions", "TRUST_TOKEN", "TRUST_TOKEN_LIB"},
471 {"User defined functions", "USER", "USER_LIB"},
Adam Langley29b18672015-02-06 11:52:16 -0800472};
473
David Benjamineec7f322021-04-21 14:49:45 -0400474static const char *err_lib_error_string(uint32_t packed_error) {
Adam Langley29b18672015-02-06 11:52:16 -0800475 const uint32_t lib = ERR_GET_LIB(packed_error);
David Benjamindbad7452024-02-20 15:06:57 -0500476 return lib >= ERR_NUM_LIBS ? NULL : kLibraryNames[lib].str;
Adam Langley95c29f32014-06-20 12:00:00 -0700477}
478
David Benjamineec7f322021-04-21 14:49:45 -0400479const char *ERR_lib_error_string(uint32_t packed_error) {
480 const char *ret = err_lib_error_string(packed_error);
481 return ret == NULL ? "unknown library" : ret;
482}
483
David Benjamindbad7452024-02-20 15:06:57 -0500484const char *ERR_lib_symbol_name(uint32_t packed_error) {
485 const uint32_t lib = ERR_GET_LIB(packed_error);
486 return lib >= ERR_NUM_LIBS ? NULL : kLibraryNames[lib].symbol;
487}
488
Adam Langley95c29f32014-06-20 12:00:00 -0700489const char *ERR_func_error_string(uint32_t packed_error) {
David Benjamin34248d42015-06-28 23:36:21 -0400490 return "OPENSSL_internal";
Adam Langley95c29f32014-06-20 12:00:00 -0700491}
492
David Benjamindbad7452024-02-20 15:06:57 -0500493static const char *err_reason_error_string(uint32_t packed_error, int symbol) {
Adam Langley29b18672015-02-06 11:52:16 -0800494 const uint32_t lib = ERR_GET_LIB(packed_error);
495 const uint32_t reason = ERR_GET_REASON(packed_error);
Adam Langley95c29f32014-06-20 12:00:00 -0700496
Adam Langley29b18672015-02-06 11:52:16 -0800497 if (lib == ERR_LIB_SYS) {
David Benjamindbad7452024-02-20 15:06:57 -0500498 if (!symbol && reason < 127) {
Adam Langley29b18672015-02-06 11:52:16 -0800499 return strerror(reason);
500 }
501 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700502 }
503
Adam Langley29b18672015-02-06 11:52:16 -0800504 if (reason < ERR_NUM_LIBS) {
David Benjamindbad7452024-02-20 15:06:57 -0500505 return symbol ? kLibraryNames[reason].reason_symbol
506 : kLibraryNames[reason].str;
Adam Langley29b18672015-02-06 11:52:16 -0800507 }
508
509 if (reason < 100) {
David Benjamindbad7452024-02-20 15:06:57 -0500510 // TODO(davidben): All our other reason strings match the symbol name. Only
511 // the common ones differ. Should we just consistently return the symbol
512 // name?
Adam Langley29b18672015-02-06 11:52:16 -0800513 switch (reason) {
514 case ERR_R_MALLOC_FAILURE:
David Benjamindbad7452024-02-20 15:06:57 -0500515 return symbol ? "MALLOC_FAILURE" : "malloc failure";
Adam Langley29b18672015-02-06 11:52:16 -0800516 case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
David Benjamindbad7452024-02-20 15:06:57 -0500517 return symbol ? "SHOULD_NOT_HAVE_BEEN_CALLED"
518 : "function should not have been called";
Adam Langley29b18672015-02-06 11:52:16 -0800519 case ERR_R_PASSED_NULL_PARAMETER:
David Benjamindbad7452024-02-20 15:06:57 -0500520 return symbol ? "PASSED_NULL_PARAMETER" : "passed a null parameter";
Adam Langley29b18672015-02-06 11:52:16 -0800521 case ERR_R_INTERNAL_ERROR:
David Benjamindbad7452024-02-20 15:06:57 -0500522 return symbol ? "INTERNAL_ERROR" : "internal error";
Adam Langley29b18672015-02-06 11:52:16 -0800523 case ERR_R_OVERFLOW:
David Benjamindbad7452024-02-20 15:06:57 -0500524 return symbol ? "OVERFLOW" : "overflow";
Adam Langley29b18672015-02-06 11:52:16 -0800525 default:
526 return NULL;
527 }
528 }
529
David Benjamindbad7452024-02-20 15:06:57 -0500530 // Unlike OpenSSL, BoringSSL's reason strings already match symbol name, so we
531 // do not need to check |symbol|.
David Benjamind27eda02015-03-05 01:19:27 -0500532 return err_string_lookup(lib, reason, kOpenSSLReasonValues,
533 kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
Adam Langley95c29f32014-06-20 12:00:00 -0700534}
535
David Benjamineec7f322021-04-21 14:49:45 -0400536const char *ERR_reason_error_string(uint32_t packed_error) {
David Benjamindbad7452024-02-20 15:06:57 -0500537 const char *ret = err_reason_error_string(packed_error, /*symbol=*/0);
David Benjamineec7f322021-04-21 14:49:45 -0400538 return ret == NULL ? "unknown error" : ret;
539}
540
David Benjamindbad7452024-02-20 15:06:57 -0500541const char *ERR_reason_symbol_name(uint32_t packed_error) {
542 return err_reason_error_string(packed_error, /*symbol=*/1);
543}
544
David Benjamineec7f322021-04-21 14:49:45 -0400545char *ERR_error_string(uint32_t packed_error, char *ret) {
546 static char buf[ERR_ERROR_STRING_BUF_LEN];
547
548 if (ret == NULL) {
549 // TODO(fork): remove this.
550 ret = buf;
551 }
552
553#if !defined(NDEBUG)
554 // This is aimed to help catch callers who don't provide
555 // |ERR_ERROR_STRING_BUF_LEN| bytes of space.
556 OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
557#endif
558
559 return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
560}
561
562char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
563 if (len == 0) {
564 return NULL;
565 }
566
567 unsigned lib = ERR_GET_LIB(packed_error);
568 unsigned reason = ERR_GET_REASON(packed_error);
569
570 const char *lib_str = err_lib_error_string(packed_error);
David Benjamindbad7452024-02-20 15:06:57 -0500571 const char *reason_str = err_reason_error_string(packed_error, /*symbol=*/0);
David Benjamineec7f322021-04-21 14:49:45 -0400572
David Benjamin6bd1e152023-07-19 13:56:50 -0400573 char lib_buf[32], reason_buf[32];
David Benjamineec7f322021-04-21 14:49:45 -0400574 if (lib_str == NULL) {
David Benjamin23d6e4c2023-07-15 02:44:54 -0400575 snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
David Benjamineec7f322021-04-21 14:49:45 -0400576 lib_str = lib_buf;
577 }
578
David Benjamin6bd1e152023-07-19 13:56:50 -0400579 if (reason_str == NULL) {
580 snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
581 reason_str = reason_buf;
582 }
David Benjamineec7f322021-04-21 14:49:45 -0400583
David Benjamin6bd1e152023-07-19 13:56:50 -0400584 int ret = snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
585 packed_error, lib_str, reason_str);
586 if (ret >= 0 && (size_t)ret >= len) {
587 // The output was truncated; make sure we always have 5 colon-separated
David Benjamineec7f322021-04-21 14:49:45 -0400588 // fields, i.e. 4 colons.
589 static const unsigned num_colons = 4;
590 unsigned i;
591 char *s = buf;
592
593 if (len <= num_colons) {
594 // In this situation it's not possible to ensure that the correct number
595 // of colons are included in the output.
596 return buf;
597 }
598
599 for (i = 0; i < num_colons; i++) {
600 char *colon = strchr(s, ':');
601 char *last_pos = &buf[len - 1] - num_colons + i;
602
603 if (colon == NULL || colon > last_pos) {
604 // set colon |i| at last possible position (buf[len-1] is the
605 // terminating 0). If we're setting this colon, then all whole of the
606 // rest of the string must be colons in order to have the correct
607 // number.
608 OPENSSL_memset(last_pos, ':', num_colons - i);
609 break;
610 }
611
612 s = colon + 1;
613 }
614 }
615
616 return buf;
617}
618
Adam Langley95c29f32014-06-20 12:00:00 -0700619void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
Adam Langley95c29f32014-06-20 12:00:00 -0700620 char buf[ERR_ERROR_STRING_BUF_LEN];
621 char buf2[1024];
Adam Langley5f1374e2014-10-01 15:21:01 -0700622 const char *file, *data;
Adam Langley95c29f32014-06-20 12:00:00 -0700623 int line, flags;
624 uint32_t packed_error;
625
David Benjamin808f8322017-08-18 14:06:02 -0400626 // thread_hash is the least-significant bits of the |ERR_STATE| pointer value
627 // for this thread.
Adam Langley33672732015-03-31 18:55:53 -0700628 const unsigned long thread_hash = (uintptr_t) err_get_state();
Adam Langley95c29f32014-06-20 12:00:00 -0700629
630 for (;;) {
631 packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
632 if (packed_error == 0) {
633 break;
634 }
635
David Benjamin3fc138e2015-10-28 18:03:21 -0400636 ERR_error_string_n(packed_error, buf, sizeof(buf));
David Benjamin23d6e4c2023-07-15 02:44:54 -0400637 snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, file,
638 line, (flags & ERR_FLAG_STRING) ? data : "");
Adam Langley95c29f32014-06-20 12:00:00 -0700639 if (callback(buf2, strlen(buf2), ctx) <= 0) {
640 break;
641 }
Adam Langley95c29f32014-06-20 12:00:00 -0700642 }
643}
644
Brian Smith83a82982015-04-09 16:21:10 -1000645static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) {
646 assert(msg[msg_len] == '\0');
647 FILE* fp = ctx;
648 int res = fputs(msg, fp);
649 return res < 0 ? 0 : 1;
650}
651
652void ERR_print_errors_fp(FILE *file) {
653 ERR_print_errors_cb(print_errors_to_file, file);
654}
655
David Benjamine1c3dad2017-09-28 16:33:09 -0400656// err_set_error_data sets the data on the most recent error.
657static void err_set_error_data(char *data) {
Adam Langley95c29f32014-06-20 12:00:00 -0700658 ERR_STATE *const state = err_get_state();
659 struct err_error_st *error;
660
Adam Langley69a01602014-11-17 17:26:55 -0800661 if (state == NULL || state->top == state->bottom) {
Bob Beckfc524c12023-02-07 14:32:41 -0700662 free(data);
Adam Langley95c29f32014-06-20 12:00:00 -0700663 return;
664 }
665
666 error = &state->errors[state->top];
667
Bob Beckfc524c12023-02-07 14:32:41 -0700668 free(error->data);
Adam Langley95c29f32014-06-20 12:00:00 -0700669 error->data = data;
Adam Langley95c29f32014-06-20 12:00:00 -0700670}
671
David Benjamin3fc138e2015-10-28 18:03:21 -0400672void ERR_put_error(int library, int unused, int reason, const char *file,
673 unsigned line) {
Adam Langley95c29f32014-06-20 12:00:00 -0700674 ERR_STATE *const state = err_get_state();
675 struct err_error_st *error;
676
Adam Langley69a01602014-11-17 17:26:55 -0800677 if (state == NULL) {
678 return;
679 }
680
Adam Langley95c29f32014-06-20 12:00:00 -0700681 if (library == ERR_LIB_SYS && reason == 0) {
Brian Smith8a36e532015-07-31 16:11:31 -0400682#if defined(OPENSSL_WINDOWS)
Adam Langley95c29f32014-06-20 12:00:00 -0700683 reason = GetLastError();
684#else
685 reason = errno;
686#endif
687 }
688
689 state->top = (state->top + 1) % ERR_NUM_ERRORS;
690 if (state->top == state->bottom) {
691 state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
692 }
693
694 error = &state->errors[state->top];
695 err_clear(error);
696 error->file = file;
697 error->line = line;
David Benjamin34248d42015-06-28 23:36:21 -0400698 error->packed = ERR_PACK(library, reason);
Adam Langley95c29f32014-06-20 12:00:00 -0700699}
700
David Benjamin808f8322017-08-18 14:06:02 -0400701// ERR_add_error_data_vdata takes a variable number of const char* pointers,
702// concatenates them and sets the result as the data on the most recent
703// error.
Adam Langley95c29f32014-06-20 12:00:00 -0700704static void err_add_error_vdata(unsigned num, va_list args) {
Bob Beckfc524c12023-02-07 14:32:41 -0700705 size_t total_size = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700706 const char *substr;
Bob Beckfc524c12023-02-07 14:32:41 -0700707 char *buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Bob Beckfc524c12023-02-07 14:32:41 -0700709 va_list args_copy;
710 va_copy(args_copy, args);
711 for (size_t i = 0; i < num; i++) {
712 substr = va_arg(args_copy, const char *);
713 if (substr == NULL) {
714 continue;
715 }
716 size_t substr_len = strlen(substr);
717 if (SIZE_MAX - total_size < substr_len) {
718 return; // Would overflow.
719 }
720 total_size += substr_len;
721 }
722 va_end(args_copy);
723 if (total_size == SIZE_MAX) {
724 return; // Would overflow.
725 }
726 total_size += 1; // NUL terminator.
727 if ((buf = malloc(total_size)) == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700728 return;
729 }
Bob Beckfc524c12023-02-07 14:32:41 -0700730 buf[0] = '\0';
731 for (size_t i = 0; i < num; i++) {
Adam Langley95c29f32014-06-20 12:00:00 -0700732 substr = va_arg(args, const char *);
733 if (substr == NULL) {
734 continue;
735 }
Bob Beckfc524c12023-02-07 14:32:41 -0700736 if (OPENSSL_strlcat(buf, substr, total_size) >= total_size) {
737 assert(0); // should not be possible.
Adam Langley95c29f32014-06-20 12:00:00 -0700738 }
Adam Langley95c29f32014-06-20 12:00:00 -0700739 }
Bob Beckfc524c12023-02-07 14:32:41 -0700740 va_end(args);
David Benjamine1c3dad2017-09-28 16:33:09 -0400741 err_set_error_data(buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700742}
743
744void ERR_add_error_data(unsigned count, ...) {
745 va_list args;
746 va_start(args, count);
747 err_add_error_vdata(count, args);
748 va_end(args);
749}
750
751void ERR_add_error_dataf(const char *format, ...) {
Bob Beckfc524c12023-02-07 14:32:41 -0700752 char *buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700753 va_list ap;
Adam Langley95c29f32014-06-20 12:00:00 -0700754
755 va_start(ap, format);
Bob Beckfc524c12023-02-07 14:32:41 -0700756 if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) {
757 return;
758 }
Adam Langley95c29f32014-06-20 12:00:00 -0700759 va_end(ap);
760
David Benjamine1c3dad2017-09-28 16:33:09 -0400761 err_set_error_data(buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700762}
763
David Benjamin731d6cb2021-11-30 15:32:25 -0500764void ERR_set_error_data(char *data, int flags) {
765 if (!(flags & ERR_FLAG_STRING)) {
766 // We do not support non-string error data.
767 assert(0);
768 return;
769 }
Bob Beckfc524c12023-02-07 14:32:41 -0700770 // Disable deprecated functions on msvc so it doesn't complain about strdup.
771 OPENSSL_MSVC_PRAGMA(warning(push))
772 OPENSSL_MSVC_PRAGMA(warning(disable : 4996))
773 // We can not use OPENSSL_strdup because we don't want to call OPENSSL_malloc,
774 // which can affect the error stack.
775 char *copy = strdup(data);
776 OPENSSL_MSVC_PRAGMA(warning(pop))
777 if (copy != NULL) {
778 err_set_error_data(copy);
779 }
David Benjamin731d6cb2021-11-30 15:32:25 -0500780 if (flags & ERR_FLAG_MALLOCED) {
Bob Beckfc524c12023-02-07 14:32:41 -0700781 // We can not take ownership of |data| directly because it is allocated with
782 // |OPENSSL_malloc| and we will free it with system |free| later.
783 OPENSSL_free(data);
David Benjamin731d6cb2021-11-30 15:32:25 -0500784 }
785}
786
Adam Langley95c29f32014-06-20 12:00:00 -0700787int ERR_set_mark(void) {
788 ERR_STATE *const state = err_get_state();
789
Adam Langley69a01602014-11-17 17:26:55 -0800790 if (state == NULL || state->bottom == state->top) {
Adam Langley95c29f32014-06-20 12:00:00 -0700791 return 0;
792 }
David Benjamina65c2522017-10-03 17:34:37 -0400793 state->errors[state->top].mark = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700794 return 1;
795}
796
797int ERR_pop_to_mark(void) {
798 ERR_STATE *const state = err_get_state();
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langley69a01602014-11-17 17:26:55 -0800800 if (state == NULL) {
801 return 0;
802 }
803
Adam Langley95c29f32014-06-20 12:00:00 -0700804 while (state->bottom != state->top) {
Brian Smithefed2212015-01-28 16:20:02 -0800805 struct err_error_st *error = &state->errors[state->top];
Adam Langley95c29f32014-06-20 12:00:00 -0700806
David Benjamina65c2522017-10-03 17:34:37 -0400807 if (error->mark) {
808 error->mark = 0;
Brian Smithefed2212015-01-28 16:20:02 -0800809 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700810 }
811
812 err_clear(error);
813 if (state->top == 0) {
814 state->top = ERR_NUM_ERRORS - 1;
815 } else {
816 state->top--;
817 }
818 }
819
Brian Smithefed2212015-01-28 16:20:02 -0800820 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700821}
822
Adam Langley29b18672015-02-06 11:52:16 -0800823void ERR_load_crypto_strings(void) {}
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyb9e77a02015-03-31 18:36:20 -0700825void ERR_free_strings(void) {}
Adam Langley95c29f32014-06-20 12:00:00 -0700826
David Benjaminc44d2f42014-08-20 16:24:00 -0400827void ERR_load_BIO_strings(void) {}
Matt Braithwaite9febf192015-04-30 13:04:06 -0700828
829void ERR_load_ERR_strings(void) {}
David Benjaminb25a8992017-10-01 21:47:07 -0400830
David Benjamin5d626b22018-05-08 16:07:00 -0400831void ERR_load_RAND_strings(void) {}
832
David Benjaminb25a8992017-10-01 21:47:07 -0400833struct err_save_state_st {
834 struct err_error_st *errors;
835 size_t num_errors;
836};
837
838void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) {
839 if (state == NULL) {
840 return;
841 }
842 for (size_t i = 0; i < state->num_errors; i++) {
843 err_clear(&state->errors[i]);
844 }
Bob Beckfc524c12023-02-07 14:32:41 -0700845 free(state->errors);
846 free(state);
David Benjaminb25a8992017-10-01 21:47:07 -0400847}
848
849ERR_SAVE_STATE *ERR_save_state(void) {
850 ERR_STATE *const state = err_get_state();
851 if (state == NULL || state->top == state->bottom) {
852 return NULL;
853 }
854
Bob Beckfc524c12023-02-07 14:32:41 -0700855 ERR_SAVE_STATE *ret = malloc(sizeof(ERR_SAVE_STATE));
David Benjaminb25a8992017-10-01 21:47:07 -0400856 if (ret == NULL) {
857 return NULL;
858 }
859
860 // Errors are stored in the range (bottom, top].
861 size_t num_errors = state->top >= state->bottom
862 ? state->top - state->bottom
863 : ERR_NUM_ERRORS + state->top - state->bottom;
864 assert(num_errors < ERR_NUM_ERRORS);
Bob Beckfc524c12023-02-07 14:32:41 -0700865 ret->errors = malloc(num_errors * sizeof(struct err_error_st));
David Benjaminb25a8992017-10-01 21:47:07 -0400866 if (ret->errors == NULL) {
Bob Beckfc524c12023-02-07 14:32:41 -0700867 free(ret);
David Benjaminb25a8992017-10-01 21:47:07 -0400868 return NULL;
869 }
870 OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st));
871 ret->num_errors = num_errors;
872
873 for (size_t i = 0; i < num_errors; i++) {
874 size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS;
875 err_copy(&ret->errors[i], &state->errors[j]);
876 }
877 return ret;
878}
879
880void ERR_restore_state(const ERR_SAVE_STATE *state) {
881 if (state == NULL || state->num_errors == 0) {
882 ERR_clear_error();
883 return;
884 }
885
David Benjamin0f222e62023-07-03 12:24:51 -0400886 if (state->num_errors >= ERR_NUM_ERRORS) {
887 abort();
888 }
889
David Benjaminb25a8992017-10-01 21:47:07 -0400890 ERR_STATE *const dst = err_get_state();
891 if (dst == NULL) {
892 return;
893 }
894
895 for (size_t i = 0; i < state->num_errors; i++) {
896 err_copy(&dst->errors[i], &state->errors[i]);
897 }
David Benjamin0f222e62023-07-03 12:24:51 -0400898 dst->top = (unsigned)(state->num_errors - 1);
David Benjaminb25a8992017-10-01 21:47:07 -0400899 dst->bottom = ERR_NUM_ERRORS - 1;
900}