Coverage Report

Created: 2026-04-30 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/err/err.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
// Ensure we can't call OPENSSL_malloc circularly.
16
#define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC
17
#include <openssl/err.h>
18
19
#include <assert.h>
20
#include <errno.h>
21
#include <inttypes.h>
22
#include <limits.h>
23
#include <stdarg.h>
24
#include <string.h>
25
26
#if defined(OPENSSL_WINDOWS)
27
#include <windows.h>
28
#endif
29
30
#include <openssl/mem.h>
31
32
#include "../internal.h"
33
#include "./internal.h"
34
35
36
using namespace bssl;
37
38
namespace {
39
struct err_error_st {
40
  // file contains the filename where the error occurred.
41
  const char *file;
42
  // data contains a NUL-terminated string with optional data. It is allocated
43
  // with system |malloc| and must be freed with |free| (not |OPENSSL_free|)
44
  char *data;
45
  // packed contains the error library and reason, as packed by ERR_PACK.
46
  uint32_t packed;
47
  // line contains the line number where the error occurred.
48
  uint16_t line;
49
  // mark indicates a reversion point in the queue. See |ERR_pop_to_mark|.
50
  unsigned mark : 1;
51
};
52
53
// ERR_STATE contains the per-thread, error queue.
54
typedef struct err_state_st {
55
  // errors contains up to ERR_NUM_ERRORS - 1 most recent errors, organised as a
56
  // ring buffer.
57
  struct err_error_st errors[ERR_NUM_ERRORS];
58
  // top contains the index of the most recent error. If |top| equals |bottom|
59
  // then the queue is empty.
60
  unsigned top;
61
  // bottom contains the index before the least recent error in the queue.
62
  unsigned bottom;
63
64
  // to_free, if not NULL, contains a pointer owned by this structure that was
65
  // previously a |data| pointer of one of the elements of |errors|.
66
  void *to_free;
67
} ERR_STATE;
68
}  // namespace
69
70
BSSL_NAMESPACE_BEGIN
71
72
extern const uint32_t kOpenSSLReasonValues[];
73
extern const size_t kOpenSSLReasonValuesLen;
74
extern const char kOpenSSLReasonStringData[];
75
76
BSSL_NAMESPACE_END
77
78
6.10k
static char *strdup_libc_malloc(const char *str) {
79
  // |strdup| is not in C until C23, so MSVC triggers deprecation warnings, and
80
  // glibc and musl gate it on a feature macro. Reimplementing it is easier.
81
6.10k
  size_t len = strlen(str);
82
6.10k
  char *ret = reinterpret_cast<char *>(malloc(len + 1));
83
6.10k
  if (ret != nullptr) {
84
6.10k
    memcpy(ret, str, len + 1);
85
6.10k
  }
86
6.10k
  return ret;
87
6.10k
}
88
89
// err_clear clears the given queued error.
90
7.61M
static void err_clear(struct err_error_st *error) {
91
7.61M
  free(error->data);
92
7.61M
  OPENSSL_memset(error, 0, sizeof(struct err_error_st));
93
7.61M
}
94
95
89.5k
static void err_copy(struct err_error_st *dst, const struct err_error_st *src) {
96
89.5k
  err_clear(dst);
97
89.5k
  dst->file = src->file;
98
89.5k
  if (src->data != nullptr) {
99
    // We can't use OPENSSL_strdup because we don't want to call OPENSSL_malloc,
100
    // which can affect the error stack.
101
6.10k
    dst->data = strdup_libc_malloc(src->data);
102
6.10k
  }
103
89.5k
  dst->packed = src->packed;
104
89.5k
  dst->line = src->line;
105
89.5k
}
106
107
108
// global_next_library contains the next custom library value to return.
109
static int global_next_library = ERR_NUM_LIBS;
110
111
// global_next_library_mutex protects |global_next_library| from concurrent
112
// updates.
113
static StaticMutex global_next_library_mutex;
114
115
0
static void err_state_free(void *statep) {
116
0
  ERR_STATE *state = reinterpret_cast<ERR_STATE *>(statep);
117
118
0
  if (state == nullptr) {
119
0
    return;
120
0
  }
121
122
0
  for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) {
123
0
    err_clear(&state->errors[i]);
124
0
  }
125
0
  free(state->to_free);
126
0
  free(state);
127
0
}
128
129
// err_get_state gets the ERR_STATE object for the current thread.
130
1.36M
static ERR_STATE *err_get_state() {
131
1.36M
  ERR_STATE *state = reinterpret_cast<ERR_STATE *>(
132
1.36M
      CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR));
133
1.36M
  if (state == nullptr) {
134
29
    state = reinterpret_cast<ERR_STATE *>(malloc(sizeof(ERR_STATE)));
135
29
    if (state == nullptr) {
136
0
      return nullptr;
137
0
    }
138
29
    OPENSSL_memset(state, 0, sizeof(ERR_STATE));
139
29
    if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state,
140
29
                                 err_state_free)) {
141
0
      return nullptr;
142
0
    }
143
29
  }
144
145
1.36M
  return state;
146
1.36M
}
147
148
static uint32_t get_error_values(int inc, int top, const char **file, int *line,
149
133k
                                 const char **data, int *flags) {
150
133k
  unsigned i = 0;
151
133k
  ERR_STATE *state;
152
133k
  struct err_error_st *error;
153
133k
  uint32_t ret;
154
155
133k
  state = err_get_state();
156
133k
  if (state == nullptr || state->bottom == state->top) {
157
10.9k
    return 0;
158
10.9k
  }
159
160
122k
  if (top) {
161
83.6k
    assert(!inc);
162
    // last error
163
83.6k
    i = state->top;
164
83.6k
  } else {
165
38.9k
    i = (state->bottom + 1) % ERR_NUM_ERRORS;
166
38.9k
  }
167
168
122k
  error = &state->errors[i];
169
122k
  ret = error->packed;
170
171
122k
  if (file != nullptr && line != nullptr) {
172
8.37k
    if (error->file == nullptr) {
173
0
      *file = "NA";
174
0
      *line = 0;
175
8.37k
    } else {
176
8.37k
      *file = error->file;
177
8.37k
      *line = error->line;
178
8.37k
    }
179
8.37k
  }
180
181
122k
  if (data != nullptr) {
182
8.37k
    if (error->data == nullptr) {
183
6.98k
      *data = "";
184
6.98k
      if (flags != nullptr) {
185
6.98k
        *flags = 0;
186
6.98k
      }
187
6.98k
    } else {
188
1.38k
      *data = error->data;
189
1.38k
      if (flags != nullptr) {
190
        // Without |ERR_FLAG_MALLOCED|, rust-openssl assumes the string has a
191
        // static lifetime. In both cases, we retain ownership of the string,
192
        // and the caller is not expected to free it.
193
1.38k
        *flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED;
194
1.38k
      }
195
      // If this error is being removed, take ownership of data from
196
      // the error. The semantics are such that the caller doesn't
197
      // take ownership either. Instead the error system takes
198
      // ownership and retains it until the next call that affects the
199
      // error queue.
200
1.38k
      if (inc) {
201
1.38k
        if (error->data != nullptr) {
202
1.38k
          free(state->to_free);
203
1.38k
          state->to_free = error->data;
204
1.38k
        }
205
1.38k
        error->data = nullptr;
206
1.38k
      }
207
1.38k
    }
208
8.37k
  }
209
210
122k
  if (inc) {
211
8.37k
    assert(!top);
212
8.37k
    err_clear(error);
213
8.37k
    state->bottom = i;
214
8.37k
  }
215
216
122k
  return ret;
217
122k
}
218
219
0
uint32_t ERR_get_error() {
220
0
  return get_error_values(1 /* inc */, 0 /* bottom */, nullptr, nullptr,
221
0
                          nullptr, nullptr);
222
0
}
223
224
0
uint32_t ERR_get_error_line(const char **file, int *line) {
225
0
  return get_error_values(1 /* inc */, 0 /* bottom */, file, line, nullptr,
226
0
                          nullptr);
227
0
}
228
229
uint32_t ERR_get_error_line_data(const char **file, int *line,
230
10.9k
                                 const char **data, int *flags) {
231
10.9k
  return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags);
232
10.9k
}
233
234
39.0k
uint32_t ERR_peek_error() {
235
39.0k
  return get_error_values(0 /* peek */, 0 /* bottom */, nullptr, nullptr,
236
39.0k
                          nullptr, nullptr);
237
39.0k
}
238
239
0
uint32_t ERR_peek_error_line(const char **file, int *line) {
240
0
  return get_error_values(0 /* peek */, 0 /* bottom */, file, line, nullptr,
241
0
                          nullptr);
242
0
}
243
244
uint32_t ERR_peek_error_line_data(const char **file, int *line,
245
0
                                  const char **data, int *flags) {
246
0
  return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data,
247
0
                          flags);
248
0
}
249
250
83.6k
uint32_t ERR_peek_last_error() {
251
83.6k
  return get_error_values(0 /* peek */, 1 /* top */, nullptr, nullptr, nullptr,
252
83.6k
                          nullptr);
253
83.6k
}
254
255
0
uint32_t ERR_peek_last_error_line(const char **file, int *line) {
256
0
  return get_error_values(0 /* peek */, 1 /* top */, file, line, nullptr,
257
0
                          nullptr);
258
0
}
259
260
uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
261
0
                                       const char **data, int *flags) {
262
0
  return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags);
263
0
}
264
265
421k
void ERR_clear_error() {
266
421k
  ERR_STATE *const state = err_get_state();
267
421k
  unsigned i;
268
269
421k
  if (state == nullptr) {
270
0
    return;
271
0
  }
272
273
7.16M
  for (i = 0; i < ERR_NUM_ERRORS; i++) {
274
6.74M
    err_clear(&state->errors[i]);
275
6.74M
  }
276
421k
  free(state->to_free);
277
421k
  state->to_free = nullptr;
278
279
421k
  state->top = state->bottom = 0;
280
421k
}
281
282
0
void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
283
0
  if (tid != nullptr) {
284
0
    assert(0);
285
0
    return;
286
0
  }
287
288
0
  ERR_clear_error();
289
0
}
290
291
0
int ERR_get_next_error_library() {
292
0
  MutexWriteLock lock(&global_next_library_mutex);
293
0
  return global_next_library++;
294
0
}
295
296
0
void ERR_remove_state(unsigned long pid) { ERR_clear_error(); }
297
298
105k
void ERR_clear_system_error() { errno = 0; }
299
300
// err_string_cmp is a compare function for searching error values with
301
// |bsearch| in |err_string_lookup|.
302
69.7k
static int err_string_cmp(const void *a, const void *b) {
303
69.7k
  const uint32_t a_key = *((const uint32_t *)a) >> 15;
304
69.7k
  const uint32_t b_key = *((const uint32_t *)b) >> 15;
305
306
69.7k
  if (a_key < b_key) {
307
40.0k
    return -1;
308
40.0k
  } else if (a_key > b_key) {
309
21.3k
    return 1;
310
21.3k
  } else {
311
8.37k
    return 0;
312
8.37k
  }
313
69.7k
}
314
315
// err_string_lookup looks up the string associated with |lib| and |key| in
316
// |values| and |string_data|. It returns the string or NULL if not found.
317
static const char *err_string_lookup(uint32_t lib, uint32_t key,
318
                                     const uint32_t *values, size_t num_values,
319
8.37k
                                     const char *string_data) {
320
  // |values| points to data in err_data.h, which is generated by
321
  // err_data_generate.go. It's an array of uint32_t values. Each value has the
322
  // following structure:
323
  //   | lib  |    key    |    offset     |
324
  //   |6 bits|  11 bits  |    15 bits    |
325
  //
326
  // The |lib| value is a library identifier: one of the |ERR_LIB_*| values.
327
  // The |key| is a reason code, depending on the context.
328
  // The |offset| is the number of bytes from the start of |string_data| where
329
  // the (NUL terminated) string for this value can be found.
330
  //
331
  // Values are sorted based on treating the |lib| and |key| part as an
332
  // unsigned integer.
333
8.37k
  if (lib >= (1 << 6) || key >= (1 << 11)) {
334
0
    return nullptr;
335
0
  }
336
8.37k
  uint32_t search_key = lib << 26 | key << 15;
337
8.37k
  const uint32_t *result = reinterpret_cast<const uint32_t *>(bsearch(
338
8.37k
      &search_key, values, num_values, sizeof(uint32_t), err_string_cmp));
339
8.37k
  if (result == nullptr) {
340
0
    return nullptr;
341
0
  }
342
343
8.37k
  return &string_data[(*result) & 0x7fff];
344
8.37k
}
345
346
namespace {
347
typedef struct library_name_st {
348
  const char *str;
349
  const char *symbol;
350
  const char *reason_symbol;
351
} LIBRARY_NAME;
352
}  // namespace
353
354
static const LIBRARY_NAME kLibraryNames[ERR_NUM_LIBS] = {
355
    {"invalid library (0)", nullptr, nullptr},
356
    {"unknown library", "NONE", "NONE_LIB"},
357
    {"system library", "SYS", "SYS_LIB"},
358
    {"bignum routines", "BN", "BN_LIB"},
359
    {"RSA routines", "RSA", "RSA_LIB"},
360
    {"Diffie-Hellman routines", "DH", "DH_LIB"},
361
    {"public key routines", "EVP", "EVP_LIB"},
362
    {"memory buffer routines", "BUF", "BUF_LIB"},
363
    {"object identifier routines", "OBJ", "OBJ_LIB"},
364
    {"PEM routines", "PEM", "PEM_LIB"},
365
    {"DSA routines", "DSA", "DSA_LIB"},
366
    {"X.509 certificate routines", "X509", "X509_LIB"},
367
    {"ASN.1 encoding routines", "ASN1", "ASN1_LIB"},
368
    {"configuration file routines", "CONF", "CONF_LIB"},
369
    {"common libcrypto routines", "CRYPTO", "CRYPTO_LIB"},
370
    {"elliptic curve routines", "EC", "EC_LIB"},
371
    {"SSL routines", "SSL", "SSL_LIB"},
372
    {"BIO routines", "BIO", "BIO_LIB"},
373
    {"PKCS7 routines", "PKCS7", "PKCS7_LIB"},
374
    {"PKCS8 routines", "PKCS8", "PKCS8_LIB"},
375
    {"X509 V3 routines", "X509V3", "X509V3_LIB"},
376
    {"random number generator", "RAND", "RAND_LIB"},
377
    {"ENGINE routines", "ENGINE", "ENGINE_LIB"},
378
    {"OCSP routines", "OCSP", "OCSP_LIB"},
379
    {"UI routines", "UI", "UI_LIB"},
380
    {"COMP routines", "COMP", "COMP_LIB"},
381
    {"ECDSA routines", "ECDSA", "ECDSA_LIB"},
382
    {"ECDH routines", "ECDH", "ECDH_LIB"},
383
    {"HMAC routines", "HMAC", "HMAC_LIB"},
384
    {"Digest functions", "DIGEST", "DIGEST_LIB"},
385
    {"Cipher functions", "CIPHER", "CIPHER_LIB"},
386
    {"HKDF functions", "HKDF", "HKDF_LIB"},
387
    {"Trust Token functions", "TRUST_TOKEN", "TRUST_TOKEN_LIB"},
388
    {"User defined functions", "USER", "USER_LIB"},
389
};
390
391
8.37k
static const char *err_lib_error_string(uint32_t packed_error) {
392
8.37k
  const uint32_t lib = ERR_GET_LIB(packed_error);
393
8.37k
  return lib >= ERR_NUM_LIBS ? nullptr : kLibraryNames[lib].str;
394
8.37k
}
395
396
0
const char *ERR_lib_error_string(uint32_t packed_error) {
397
0
  const char *ret = err_lib_error_string(packed_error);
398
0
  return ret == nullptr ? "unknown library" : ret;
399
0
}
400
401
0
const char *ERR_lib_symbol_name(uint32_t packed_error) {
402
0
  const uint32_t lib = ERR_GET_LIB(packed_error);
403
0
  return lib >= ERR_NUM_LIBS ? nullptr : kLibraryNames[lib].symbol;
404
0
}
405
406
0
const char *ERR_func_error_string(uint32_t packed_error) {
407
0
  return "OPENSSL_internal";
408
0
}
409
410
8.37k
static const char *err_reason_error_string(uint32_t packed_error, int symbol) {
411
8.37k
  const uint32_t lib = ERR_GET_LIB(packed_error);
412
8.37k
  const uint32_t reason = ERR_GET_REASON(packed_error);
413
414
8.37k
  if (lib == ERR_LIB_SYS) {
415
0
    if (!symbol && reason < 127) {
416
0
      return strerror(reason);
417
0
    }
418
0
    return nullptr;
419
0
  }
420
421
8.37k
  if (reason < ERR_NUM_LIBS) {
422
0
    return symbol ? kLibraryNames[reason].reason_symbol
423
0
                  : kLibraryNames[reason].str;
424
0
  }
425
426
8.37k
  if (reason < 100) {
427
    // TODO(davidben): All our other reason strings match the symbol name. Only
428
    // the common ones differ. Should we just consistently return the symbol
429
    // name?
430
0
    switch (reason) {
431
0
      case ERR_R_MALLOC_FAILURE:
432
0
        return symbol ? "MALLOC_FAILURE" : "malloc failure";
433
0
      case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
434
0
        return symbol ? "SHOULD_NOT_HAVE_BEEN_CALLED"
435
0
                      : "function should not have been called";
436
0
      case ERR_R_PASSED_NULL_PARAMETER:
437
0
        return symbol ? "PASSED_NULL_PARAMETER" : "passed a null parameter";
438
0
      case ERR_R_INTERNAL_ERROR:
439
0
        return symbol ? "INTERNAL_ERROR" : "internal error";
440
0
      case ERR_R_OVERFLOW:
441
0
        return symbol ? "OVERFLOW" : "overflow";
442
0
      default:
443
0
        return nullptr;
444
0
    }
445
0
  }
446
447
  // Unlike OpenSSL, BoringSSL's reason strings already match symbol name, so we
448
  // do not need to check |symbol|.
449
8.37k
  return err_string_lookup(lib, reason, kOpenSSLReasonValues,
450
8.37k
                           kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
451
8.37k
}
452
453
0
const char *ERR_reason_error_string(uint32_t packed_error) {
454
0
  const char *ret = err_reason_error_string(packed_error, /*symbol=*/0);
455
0
  return ret == nullptr ? "unknown error" : ret;
456
0
}
457
458
0
const char *ERR_reason_symbol_name(uint32_t packed_error) {
459
0
  return err_reason_error_string(packed_error, /*symbol=*/1);
460
0
}
461
462
0
char *ERR_error_string(uint32_t packed_error, char *ret) {
463
0
  static char buf[ERR_ERROR_STRING_BUF_LEN];
464
465
0
  if (ret == nullptr) {
466
    // TODO(fork): remove this.
467
0
    ret = buf;
468
0
  }
469
470
0
#if !defined(NDEBUG)
471
  // This is aimed to help catch callers who don't provide
472
  // |ERR_ERROR_STRING_BUF_LEN| bytes of space.
473
0
  OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
474
0
#endif
475
476
0
  return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
477
0
}
478
479
8.37k
char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
480
8.37k
  if (len == 0) {
481
0
    return nullptr;
482
0
  }
483
484
8.37k
  unsigned lib = ERR_GET_LIB(packed_error);
485
8.37k
  unsigned reason = ERR_GET_REASON(packed_error);
486
487
8.37k
  const char *lib_str = err_lib_error_string(packed_error);
488
8.37k
  const char *reason_str = err_reason_error_string(packed_error, /*symbol=*/0);
489
490
8.37k
  char lib_buf[32], reason_buf[32];
491
8.37k
  if (lib_str == nullptr) {
492
0
    snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
493
0
    lib_str = lib_buf;
494
0
  }
495
496
8.37k
  if (reason_str == nullptr) {
497
0
    snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
498
0
    reason_str = reason_buf;
499
0
  }
500
501
8.37k
  int ret = snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
502
8.37k
                     packed_error, lib_str, reason_str);
503
8.37k
  if (ret >= 0 && (size_t)ret >= len) {
504
    // The output was truncated; make sure we always have 5 colon-separated
505
    // fields, i.e. 4 colons.
506
0
    static const unsigned num_colons = 4;
507
0
    unsigned i;
508
0
    char *s = buf;
509
510
0
    if (len <= num_colons) {
511
      // In this situation it's not possible to ensure that the correct number
512
      // of colons are included in the output.
513
0
      return buf;
514
0
    }
515
516
0
    for (i = 0; i < num_colons; i++) {
517
0
      char *colon = strchr(s, ':');
518
0
      char *last_pos = &buf[len - 1] - num_colons + i;
519
520
0
      if (colon == nullptr || colon > last_pos) {
521
        // set colon |i| at last possible position (buf[len-1] is the
522
        // terminating 0). If we're setting this colon, then all whole of the
523
        // rest of the string must be colons in order to have the correct
524
        // number.
525
0
        OPENSSL_memset(last_pos, ':', num_colons - i);
526
0
        break;
527
0
      }
528
529
0
      s = colon + 1;
530
0
    }
531
0
  }
532
533
8.37k
  return buf;
534
8.37k
}
535
536
2.52k
void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
537
2.52k
  char buf[ERR_ERROR_STRING_BUF_LEN];
538
2.52k
  char buf2[1024];
539
2.52k
  const char *file, *data;
540
2.52k
  int line, flags;
541
2.52k
  uint32_t packed_error;
542
543
  // thread_hash is the least-significant bits of the |ERR_STATE| pointer value
544
  // for this thread.
545
2.52k
  const unsigned long thread_hash = (uintptr_t)err_get_state();
546
547
10.9k
  for (;;) {
548
10.9k
    packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
549
10.9k
    if (packed_error == 0) {
550
2.52k
      break;
551
2.52k
    }
552
553
8.37k
    ERR_error_string_n(packed_error, buf, sizeof(buf));
554
8.37k
    snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, file,
555
8.37k
             line, (flags & ERR_FLAG_STRING) ? data : "");
556
8.37k
    if (callback(buf2, strlen(buf2), ctx) <= 0) {
557
0
      break;
558
0
    }
559
8.37k
  }
560
2.52k
}
561
562
0
static int print_errors_to_file(const char *msg, size_t msg_len, void *ctx) {
563
0
  assert(msg[msg_len] == '\0');
564
0
  FILE *fp = reinterpret_cast<FILE *>(ctx);
565
0
  int res = fputs(msg, fp);
566
0
  return res < 0 ? 0 : 1;
567
0
}
568
569
0
void ERR_print_errors_fp(FILE *file) {
570
0
  ERR_print_errors_cb(print_errors_to_file, file);
571
0
}
572
573
// err_set_error_data sets the data on the most recent error.
574
45.9k
static void err_set_error_data(char *data) {
575
45.9k
  ERR_STATE *const state = err_get_state();
576
45.9k
  struct err_error_st *error;
577
578
45.9k
  if (state == nullptr || state->top == state->bottom) {
579
0
    free(data);
580
0
    return;
581
0
  }
582
583
45.9k
  error = &state->errors[state->top];
584
585
45.9k
  free(error->data);
586
45.9k
  error->data = data;
587
45.9k
}
588
589
void ERR_put_error(int library, int unused, int reason, const char *file,
590
709k
                   unsigned line) {
591
709k
  ERR_STATE *const state = err_get_state();
592
709k
  struct err_error_st *error;
593
594
709k
  if (state == nullptr) {
595
0
    return;
596
0
  }
597
598
709k
  if (library == ERR_LIB_SYS && reason == 0) {
599
#if defined(OPENSSL_WINDOWS)
600
    reason = GetLastError();
601
#else
602
0
    reason = errno;
603
0
#endif
604
0
  }
605
606
709k
  state->top = (state->top + 1) % ERR_NUM_ERRORS;
607
709k
  if (state->top == state->bottom) {
608
262k
    state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
609
262k
  }
610
611
709k
  error = &state->errors[state->top];
612
709k
  err_clear(error);
613
709k
  error->file = file;
614
709k
  error->line = line;
615
709k
  error->packed = ERR_PACK(library, reason);
616
709k
}
617
618
// ERR_add_error_data_vdata takes a variable number of const char* pointers,
619
// concatenates them and sets the result as the data on the most recent
620
// error.
621
31.1k
static void err_add_error_vdata(unsigned num, va_list args) {
622
31.1k
  size_t total_size = 0;
623
31.1k
  const char *substr;
624
31.1k
  char *buf;
625
626
31.1k
  va_list args_copy;
627
31.1k
  va_copy(args_copy, args);
628
134k
  for (size_t i = 0; i < num; i++) {
629
103k
    substr = va_arg(args_copy, const char *);
630
103k
    if (substr == nullptr) {
631
3.20k
      continue;
632
3.20k
    }
633
99.9k
    size_t substr_len = strlen(substr);
634
99.9k
    if (SIZE_MAX - total_size < substr_len) {
635
0
      return;  // Would overflow.
636
0
    }
637
99.9k
    total_size += substr_len;
638
99.9k
  }
639
31.1k
  va_end(args_copy);
640
31.1k
  if (total_size == SIZE_MAX) {
641
0
    return;  // Would overflow.
642
0
  }
643
31.1k
  total_size += 1;  // NUL terminator.
644
31.1k
  if ((buf = reinterpret_cast<char *>(malloc(total_size))) == nullptr) {
645
0
    return;
646
0
  }
647
31.1k
  buf[0] = '\0';
648
134k
  for (size_t i = 0; i < num; i++) {
649
103k
    substr = va_arg(args, const char *);
650
103k
    if (substr == nullptr) {
651
3.20k
      continue;
652
3.20k
    }
653
99.9k
    if (OPENSSL_strlcat(buf, substr, total_size) >= total_size) {
654
0
      assert(0);  // should not be possible.
655
0
    }
656
99.9k
  }
657
31.1k
  err_set_error_data(buf);
658
31.1k
}
659
660
31.1k
void ERR_add_error_data(unsigned count, ...) {
661
31.1k
  va_list args;
662
31.1k
  va_start(args, count);
663
31.1k
  err_add_error_vdata(count, args);
664
31.1k
  va_end(args);
665
31.1k
}
666
667
14.8k
void ERR_add_error_dataf(const char *format, ...) {
668
14.8k
  char *buf = nullptr;
669
14.8k
  va_list ap;
670
671
14.8k
  va_start(ap, format);
672
14.8k
  if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) {
673
0
    return;
674
0
  }
675
14.8k
  va_end(ap);
676
677
14.8k
  err_set_error_data(buf);
678
14.8k
}
679
680
0
void ERR_set_error_data(char *data, int flags) {
681
0
  if (!(flags & ERR_FLAG_STRING)) {
682
    // We do not support non-string error data.
683
0
    assert(0);
684
0
    return;
685
0
  }
686
  // We can not use OPENSSL_strdup because we don't want to call OPENSSL_malloc,
687
  // which can affect the error stack.
688
0
  char *copy = strdup_libc_malloc(data);
689
0
  if (copy != nullptr) {
690
0
    err_set_error_data(copy);
691
0
  }
692
0
  if (flags & ERR_FLAG_MALLOCED) {
693
    // We can not take ownership of |data| directly because it is allocated with
694
    // |OPENSSL_malloc| and we will free it with system |free| later.
695
0
    OPENSSL_free(data);
696
0
  }
697
0
}
698
699
0
int ERR_set_mark() {
700
0
  ERR_STATE *const state = err_get_state();
701
702
0
  if (state == nullptr || state->bottom == state->top) {
703
0
    return 0;
704
0
  }
705
0
  state->errors[state->top].mark = 1;
706
0
  return 1;
707
0
}
708
709
0
int ERR_pop_to_mark() {
710
0
  ERR_STATE *const state = err_get_state();
711
712
0
  if (state == nullptr) {
713
0
    return 0;
714
0
  }
715
716
0
  while (state->bottom != state->top) {
717
0
    struct err_error_st *error = &state->errors[state->top];
718
719
0
    if (error->mark) {
720
0
      error->mark = 0;
721
0
      return 1;
722
0
    }
723
724
0
    err_clear(error);
725
0
    if (state->top == 0) {
726
0
      state->top = ERR_NUM_ERRORS - 1;
727
0
    } else {
728
0
      state->top--;
729
0
    }
730
0
  }
731
732
0
  return 0;
733
0
}
734
735
0
void ERR_load_crypto_strings() {}
736
737
0
void ERR_free_strings() {}
738
739
0
void ERR_load_BIO_strings() {}
740
741
0
void ERR_load_ERR_strings() {}
742
743
0
void ERR_load_RAND_strings() {}
744
745
BSSL_NAMESPACE_BEGIN
746
747
struct err_save_state_st {
748
  struct err_error_st *errors;
749
  size_t num_errors;
750
};
751
752
BSSL_NAMESPACE_END
753
754
31.4k
void bssl::ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) {
755
31.4k
  if (state == nullptr) {
756
0
    return;
757
0
  }
758
91.9k
  for (size_t i = 0; i < state->num_errors; i++) {
759
60.5k
    err_clear(&state->errors[i]);
760
60.5k
  }
761
31.4k
  free(state->errors);
762
31.4k
  free(state);
763
31.4k
}
764
765
32.1k
ERR_SAVE_STATE *bssl::ERR_save_state() {
766
32.1k
  ERR_STATE *const state = err_get_state();
767
32.1k
  if (state == nullptr || state->top == state->bottom) {
768
690
    return nullptr;
769
690
  }
770
771
31.4k
  ERR_SAVE_STATE *ret =
772
31.4k
      reinterpret_cast<ERR_SAVE_STATE *>(malloc(sizeof(ERR_SAVE_STATE)));
773
31.4k
  if (ret == nullptr) {
774
0
    return nullptr;
775
0
  }
776
777
  // Errors are stored in the range (bottom, top].
778
31.4k
  size_t num_errors = state->top >= state->bottom
779
31.4k
                          ? state->top - state->bottom
780
31.4k
                          : ERR_NUM_ERRORS + state->top - state->bottom;
781
31.4k
  assert(num_errors < ERR_NUM_ERRORS);
782
31.4k
  ret->errors = reinterpret_cast<err_error_st *>(
783
31.4k
      malloc(num_errors * sizeof(struct err_error_st)));
784
31.4k
  if (ret->errors == nullptr) {
785
0
    free(ret);
786
0
    return nullptr;
787
0
  }
788
31.4k
  OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st));
789
31.4k
  ret->num_errors = num_errors;
790
791
91.9k
  for (size_t i = 0; i < num_errors; i++) {
792
60.5k
    size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS;
793
60.5k
    err_copy(&ret->errors[i], &state->errors[j]);
794
60.5k
  }
795
31.4k
  return ret;
796
31.4k
}
797
798
15.9k
void bssl::ERR_restore_state(const ERR_SAVE_STATE *state) {
799
15.9k
  if (state == nullptr || state->num_errors == 0) {
800
680
    ERR_clear_error();
801
680
    return;
802
680
  }
803
804
15.2k
  if (state->num_errors >= ERR_NUM_ERRORS) {
805
0
    abort();
806
0
  }
807
808
15.2k
  ERR_STATE *const dst = err_get_state();
809
15.2k
  if (dst == nullptr) {
810
0
    return;
811
0
  }
812
813
44.2k
  for (size_t i = 0; i < state->num_errors; i++) {
814
29.0k
    err_copy(&dst->errors[i], &state->errors[i]);
815
29.0k
  }
816
15.2k
  dst->top = (unsigned)(state->num_errors - 1);
817
15.2k
  dst->bottom = ERR_NUM_ERRORS - 1;
818
15.2k
}