Coverage Report

Created: 2026-08-14 07:01

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.97k
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.97k
  size_t len = strlen(str);
82
6.97k
  char *ret = reinterpret_cast<char *>(malloc(len + 1));
83
6.97k
  if (ret != nullptr) {
84
6.97k
    memcpy(ret, str, len + 1);
85
6.97k
  }
86
6.97k
  return ret;
87
6.97k
}
88
89
// err_clear clears the given queued error.
90
7.76M
static void err_clear(struct err_error_st *error) {
91
7.76M
  free(error->data);
92
7.76M
  OPENSSL_memset(error, 0, sizeof(struct err_error_st));
93
7.76M
}
94
95
84.4k
static void err_copy(struct err_error_st *dst, const struct err_error_st *src) {
96
84.4k
  err_clear(dst);
97
84.4k
  dst->file = src->file;
98
84.4k
  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.97k
    dst->data = strdup_libc_malloc(src->data);
102
6.97k
  }
103
84.4k
  dst->packed = src->packed;
104
84.4k
  dst->line = src->line;
105
84.4k
}
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.27M
static ERR_STATE *err_get_state() {
131
1.27M
  ERR_STATE *state = reinterpret_cast<ERR_STATE *>(
132
1.27M
      CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR));
133
1.27M
  if (state == nullptr) {
134
28
    state = reinterpret_cast<ERR_STATE *>(malloc(sizeof(ERR_STATE)));
135
28
    if (state == nullptr) {
136
0
      return nullptr;
137
0
    }
138
28
    OPENSSL_memset(state, 0, sizeof(ERR_STATE));
139
28
    if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state,
140
28
                                 err_state_free)) {
141
0
      return nullptr;
142
0
    }
143
28
  }
144
145
1.27M
  return state;
146
1.27M
}
147
148
static uint32_t get_error_values(int inc, int top, const char **file, int *line,
149
93.0k
                                 const char **data, int *flags) {
150
93.0k
  unsigned i = 0;
151
93.0k
  ERR_STATE *state;
152
93.0k
  struct err_error_st *error;
153
93.0k
  uint32_t ret;
154
155
93.0k
  state = err_get_state();
156
93.0k
  if (state == nullptr || state->bottom == state->top) {
157
2.40k
    return 0;
158
2.40k
  }
159
160
90.6k
  if (top) {
161
82.3k
    assert(!inc);
162
    // last error
163
82.3k
    i = state->top;
164
82.3k
  } else {
165
8.33k
    i = (state->bottom + 1) % ERR_NUM_ERRORS;
166
8.33k
  }
167
168
90.6k
  error = &state->errors[i];
169
90.6k
  ret = error->packed;
170
171
90.6k
  if (file != nullptr && line != nullptr) {
172
7.13k
    if (error->file == nullptr) {
173
0
      *file = "NA";
174
0
      *line = 0;
175
7.13k
    } else {
176
7.13k
      *file = error->file;
177
7.13k
      *line = error->line;
178
7.13k
    }
179
7.13k
  }
180
181
90.6k
  if (data != nullptr) {
182
7.13k
    if (error->data == nullptr) {
183
3.91k
      *data = "";
184
3.91k
      if (flags != nullptr) {
185
3.91k
        *flags = 0;
186
3.91k
      }
187
3.91k
    } else {
188
3.22k
      *data = error->data;
189
3.22k
      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
3.22k
        *flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED;
194
3.22k
      }
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
3.22k
      if (inc) {
201
3.22k
        if (error->data != nullptr) {
202
3.22k
          free(state->to_free);
203
3.22k
          state->to_free = error->data;
204
3.22k
        }
205
3.22k
        error->data = nullptr;
206
3.22k
      }
207
3.22k
    }
208
7.13k
  }
209
210
90.6k
  if (inc) {
211
7.13k
    assert(!top);
212
7.13k
    err_clear(error);
213
7.13k
    state->bottom = i;
214
7.13k
  }
215
216
90.6k
  return ret;
217
90.6k
}
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
9.54k
                                 const char **data, int *flags) {
231
9.54k
  return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags);
232
9.54k
}
233
234
1.19k
uint32_t ERR_peek_error() {
235
1.19k
  return get_error_values(0 /* peek */, 0 /* bottom */, nullptr, nullptr,
236
1.19k
                          nullptr, nullptr);
237
1.19k
}
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
82.3k
uint32_t ERR_peek_last_error() {
251
82.3k
  return get_error_values(0 /* peek */, 1 /* top */, nullptr, nullptr, nullptr,
252
82.3k
                          nullptr);
253
82.3k
}
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
435k
void ERR_clear_error() {
266
435k
  ERR_STATE *const state = err_get_state();
267
435k
  unsigned i;
268
269
435k
  if (state == nullptr) {
270
0
    return;
271
0
  }
272
273
7.39M
  for (i = 0; i < ERR_NUM_ERRORS; i++) {
274
6.96M
    err_clear(&state->errors[i]);
275
6.96M
  }
276
435k
  free(state->to_free);
277
435k
  state->to_free = nullptr;
278
279
435k
  state->top = state->bottom = 0;
280
435k
}
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
119k
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
57.0k
static int err_string_cmp(const void *a, const void *b) {
303
57.0k
  const uint32_t a_key = *((const uint32_t *)a) >> 15;
304
57.0k
  const uint32_t b_key = *((const uint32_t *)b) >> 15;
305
306
57.0k
  if (a_key < b_key) {
307
32.1k
    return -1;
308
32.1k
  } else if (a_key > b_key) {
309
17.6k
    return 1;
310
17.6k
  } else {
311
7.13k
    return 0;
312
7.13k
  }
313
57.0k
}
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
7.13k
                                     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
7.13k
  if (lib >= (1 << 6) || key >= (1 << 11)) {
334
0
    return nullptr;
335
0
  }
336
7.13k
  uint32_t search_key = lib << 26 | key << 15;
337
7.13k
  const uint32_t *result = reinterpret_cast<const uint32_t *>(bsearch(
338
7.13k
      &search_key, values, num_values, sizeof(uint32_t), err_string_cmp));
339
7.13k
  if (result == nullptr) {
340
0
    return nullptr;
341
0
  }
342
343
7.13k
  return &string_data[(*result) & 0x7fff];
344
7.13k
}
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
    {"CMS routines", "CMS", "CMS_LIB"},
389
    {"User defined functions", "USER", "USER_LIB"},
390
};
391
392
7.13k
static const char *err_lib_error_string(uint32_t packed_error) {
393
7.13k
  const uint32_t lib = ERR_GET_LIB(packed_error);
394
7.13k
  return lib >= ERR_NUM_LIBS ? nullptr : kLibraryNames[lib].str;
395
7.13k
}
396
397
0
const char *ERR_lib_error_string(uint32_t packed_error) {
398
0
  const char *ret = err_lib_error_string(packed_error);
399
0
  return ret == nullptr ? "unknown library" : ret;
400
0
}
401
402
0
const char *ERR_lib_symbol_name(uint32_t packed_error) {
403
0
  const uint32_t lib = ERR_GET_LIB(packed_error);
404
0
  return lib >= ERR_NUM_LIBS ? nullptr : kLibraryNames[lib].symbol;
405
0
}
406
407
0
const char *ERR_func_error_string(uint32_t packed_error) {
408
0
  return "OPENSSL_internal";
409
0
}
410
411
7.13k
static const char *err_reason_error_string(uint32_t packed_error, int symbol) {
412
7.13k
  const uint32_t lib = ERR_GET_LIB(packed_error);
413
7.13k
  const uint32_t reason = ERR_GET_REASON(packed_error);
414
415
7.13k
  if (lib == ERR_LIB_SYS) {
416
0
    if (!symbol && reason < 127) {
417
0
      return strerror(reason);
418
0
    }
419
0
    return nullptr;
420
0
  }
421
422
7.13k
  if (reason < ERR_NUM_LIBS) {
423
0
    return symbol ? kLibraryNames[reason].reason_symbol
424
0
                  : kLibraryNames[reason].str;
425
0
  }
426
427
7.13k
  if (reason < 100) {
428
    // TODO(davidben): All our other reason strings match the symbol name. Only
429
    // the common ones differ. Should we just consistently return the symbol
430
    // name?
431
0
    switch (reason) {
432
0
      case ERR_R_MALLOC_FAILURE:
433
0
        return symbol ? "MALLOC_FAILURE" : "malloc failure";
434
0
      case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
435
0
        return symbol ? "SHOULD_NOT_HAVE_BEEN_CALLED"
436
0
                      : "function should not have been called";
437
0
      case ERR_R_PASSED_NULL_PARAMETER:
438
0
        return symbol ? "PASSED_NULL_PARAMETER" : "passed a null parameter";
439
0
      case ERR_R_INTERNAL_ERROR:
440
0
        return symbol ? "INTERNAL_ERROR" : "internal error";
441
0
      case ERR_R_OVERFLOW:
442
0
        return symbol ? "OVERFLOW" : "overflow";
443
0
      default:
444
0
        return nullptr;
445
0
    }
446
0
  }
447
448
  // Unlike OpenSSL, BoringSSL's reason strings already match symbol name, so we
449
  // do not need to check `symbol`.
450
7.13k
  return err_string_lookup(lib, reason, kOpenSSLReasonValues,
451
7.13k
                           kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
452
7.13k
}
453
454
0
const char *ERR_reason_error_string(uint32_t packed_error) {
455
0
  const char *ret = err_reason_error_string(packed_error, /*symbol=*/0);
456
0
  return ret == nullptr ? "unknown error" : ret;
457
0
}
458
459
0
const char *ERR_reason_symbol_name(uint32_t packed_error) {
460
0
  return err_reason_error_string(packed_error, /*symbol=*/1);
461
0
}
462
463
0
char *ERR_error_string(uint32_t packed_error, char *ret) {
464
0
  static char buf[ERR_ERROR_STRING_BUF_LEN];
465
466
0
  if (ret == nullptr) {
467
    // TODO(fork): remove this.
468
0
    ret = buf;
469
0
  }
470
471
0
#if !defined(NDEBUG)
472
  // This is aimed to help catch callers who don't provide
473
  // `ERR_ERROR_STRING_BUF_LEN` bytes of space.
474
0
  OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
475
0
#endif
476
477
0
  return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
478
0
}
479
480
7.13k
char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
481
7.13k
  if (len == 0) {
482
0
    return nullptr;
483
0
  }
484
485
7.13k
  unsigned lib = ERR_GET_LIB(packed_error);
486
7.13k
  unsigned reason = ERR_GET_REASON(packed_error);
487
488
7.13k
  const char *lib_str = err_lib_error_string(packed_error);
489
7.13k
  const char *reason_str = err_reason_error_string(packed_error, /*symbol=*/0);
490
491
7.13k
  char lib_buf[32], reason_buf[32];
492
7.13k
  if (lib_str == nullptr) {
493
0
    snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
494
0
    lib_str = lib_buf;
495
0
  }
496
497
7.13k
  if (reason_str == nullptr) {
498
0
    snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
499
0
    reason_str = reason_buf;
500
0
  }
501
502
7.13k
  int ret = snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
503
7.13k
                     packed_error, lib_str, reason_str);
504
7.13k
  if (ret >= 0 && (size_t)ret >= len) {
505
    // The output was truncated; make sure we always have 5 colon-separated
506
    // fields, i.e. 4 colons.
507
0
    static const unsigned num_colons = 4;
508
0
    unsigned i;
509
0
    char *s = buf;
510
511
0
    if (len <= num_colons) {
512
      // In this situation it's not possible to ensure that the correct number
513
      // of colons are included in the output.
514
0
      return buf;
515
0
    }
516
517
0
    for (i = 0; i < num_colons; i++) {
518
0
      char *colon = strchr(s, ':');
519
0
      char *last_pos = &buf[len - 1] - num_colons + i;
520
521
0
      if (colon == nullptr || colon > last_pos) {
522
        // set colon `i` at last possible position (`buf[len-1]` is the
523
        // terminating 0). If we're setting this colon, then all whole of the
524
        // rest of the string must be colons in order to have the correct
525
        // number.
526
0
        OPENSSL_memset(last_pos, ':', num_colons - i);
527
0
        break;
528
0
      }
529
530
0
      s = colon + 1;
531
0
    }
532
0
  }
533
534
7.13k
  return buf;
535
7.13k
}
536
537
2.40k
void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
538
2.40k
  char buf[ERR_ERROR_STRING_BUF_LEN];
539
2.40k
  char buf2[1024];
540
2.40k
  const char *file, *data;
541
2.40k
  int line, flags;
542
2.40k
  uint32_t packed_error;
543
544
  // thread_hash is the least-significant bits of the `ERR_STATE` pointer value
545
  // for this thread.
546
2.40k
  const unsigned long thread_hash = (uintptr_t)err_get_state();
547
548
9.54k
  for (;;) {
549
9.54k
    packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
550
9.54k
    if (packed_error == 0) {
551
2.40k
      break;
552
2.40k
    }
553
554
7.13k
    ERR_error_string_n(packed_error, buf, sizeof(buf));
555
7.13k
    snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, file,
556
7.13k
             line, (flags & ERR_FLAG_STRING) ? data : "");
557
7.13k
    if (callback(buf2, strlen(buf2), ctx) <= 0) {
558
0
      break;
559
0
    }
560
7.13k
  }
561
2.40k
}
562
563
0
static int print_errors_to_file(const char *msg, size_t msg_len, void *ctx) {
564
0
  assert(msg[msg_len] == '\0');
565
0
  FILE *fp = reinterpret_cast<FILE *>(ctx);
566
0
  int res = fputs(msg, fp);
567
0
  return res < 0 ? 0 : 1;
568
0
}
569
570
0
void ERR_print_errors_fp(FILE *file) {
571
0
  ERR_print_errors_cb(print_errors_to_file, file);
572
0
}
573
574
// err_set_error_data sets the data on the most recent error.
575
41.7k
static void err_set_error_data(char *data) {
576
41.7k
  ERR_STATE *const state = err_get_state();
577
41.7k
  struct err_error_st *error;
578
579
41.7k
  if (state == nullptr || state->top == state->bottom) {
580
0
    free(data);
581
0
    return;
582
0
  }
583
584
41.7k
  error = &state->errors[state->top];
585
586
41.7k
  free(error->data);
587
41.7k
  error->data = data;
588
41.7k
}
589
590
void ERR_put_error(int library, int unused, int reason, const char *file,
591
653k
                   unsigned line) {
592
653k
  ERR_STATE *const state = err_get_state();
593
653k
  struct err_error_st *error;
594
595
653k
  if (state == nullptr) {
596
0
    return;
597
0
  }
598
599
653k
  if (library == ERR_LIB_SYS && reason == 0) {
600
#if defined(OPENSSL_WINDOWS)
601
    reason = GetLastError();
602
#else
603
0
    reason = errno;
604
0
#endif
605
0
  }
606
607
653k
  state->top = (state->top + 1) % ERR_NUM_ERRORS;
608
653k
  if (state->top == state->bottom) {
609
217k
    state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
610
217k
  }
611
612
653k
  error = &state->errors[state->top];
613
653k
  err_clear(error);
614
653k
  error->file = file;
615
653k
  error->line = line;
616
653k
  error->packed = ERR_PACK(library, reason);
617
653k
}
618
619
// ERR_add_error_data_vdata takes a variable number of const char* pointers,
620
// concatenates them and sets the result as the data on the most recent
621
// error.
622
26.6k
static void err_add_error_vdata(unsigned num, va_list args) {
623
26.6k
  size_t total_size = 0;
624
26.6k
  const char *substr;
625
26.6k
  char *buf;
626
627
26.6k
  va_list args_copy;
628
26.6k
  va_copy(args_copy, args);
629
91.6k
  for (size_t i = 0; i < num; i++) {
630
64.9k
    substr = va_arg(args_copy, const char *);
631
64.9k
    if (substr == nullptr) {
632
0
      continue;
633
0
    }
634
64.9k
    size_t substr_len = strlen(substr);
635
64.9k
    if (SIZE_MAX - total_size < substr_len) {
636
0
      return;  // Would overflow.
637
0
    }
638
64.9k
    total_size += substr_len;
639
64.9k
  }
640
26.6k
  va_end(args_copy);
641
26.6k
  if (total_size == SIZE_MAX) {
642
0
    return;  // Would overflow.
643
0
  }
644
26.6k
  total_size += 1;  // NUL terminator.
645
26.6k
  if ((buf = reinterpret_cast<char *>(malloc(total_size))) == nullptr) {
646
0
    return;
647
0
  }
648
26.6k
  buf[0] = '\0';
649
91.6k
  for (size_t i = 0; i < num; i++) {
650
64.9k
    substr = va_arg(args, const char *);
651
64.9k
    if (substr == nullptr) {
652
0
      continue;
653
0
    }
654
64.9k
    if (OPENSSL_strlcat(buf, substr, total_size) >= total_size) {
655
0
      assert(0);  // should not be possible.
656
0
    }
657
64.9k
  }
658
26.6k
  err_set_error_data(buf);
659
26.6k
}
660
661
26.6k
void ERR_add_error_data(unsigned count, ...) {
662
26.6k
  va_list args;
663
26.6k
  va_start(args, count);
664
26.6k
  err_add_error_vdata(count, args);
665
26.6k
  va_end(args);
666
26.6k
}
667
668
15.0k
void ERR_add_error_dataf(const char *format, ...) {
669
15.0k
  char *buf = nullptr;
670
15.0k
  va_list ap;
671
672
15.0k
  va_start(ap, format);
673
15.0k
  if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) {
674
0
    return;
675
0
  }
676
15.0k
  va_end(ap);
677
678
15.0k
  err_set_error_data(buf);
679
15.0k
}
680
681
0
void ERR_set_error_data(char *data, int flags) {
682
0
  if (!(flags & ERR_FLAG_STRING)) {
683
    // We do not support non-string error data.
684
0
    assert(0);
685
0
    return;
686
0
  }
687
  // We can not use OPENSSL_strdup because we don't want to call OPENSSL_malloc,
688
  // which can affect the error stack.
689
0
  char *copy = strdup_libc_malloc(data);
690
0
  if (copy != nullptr) {
691
0
    err_set_error_data(copy);
692
0
  }
693
0
  if (flags & ERR_FLAG_MALLOCED) {
694
    // We can not take ownership of `data` directly because it is allocated with
695
    // `OPENSSL_malloc` and we will free it with system `free` later.
696
0
    OPENSSL_free(data);
697
0
  }
698
0
}
699
700
0
int ERR_set_mark() {
701
0
  ERR_STATE *const state = err_get_state();
702
703
0
  if (state == nullptr || state->bottom == state->top) {
704
0
    return 0;
705
0
  }
706
0
  state->errors[state->top].mark = 1;
707
0
  return 1;
708
0
}
709
710
0
int ERR_pop_to_mark() {
711
0
  ERR_STATE *const state = err_get_state();
712
713
0
  if (state == nullptr) {
714
0
    return 0;
715
0
  }
716
717
0
  while (state->bottom != state->top) {
718
0
    struct err_error_st *error = &state->errors[state->top];
719
720
0
    if (error->mark) {
721
0
      error->mark = 0;
722
0
      return 1;
723
0
    }
724
725
0
    err_clear(error);
726
0
    if (state->top == 0) {
727
0
      state->top = ERR_NUM_ERRORS - 1;
728
0
    } else {
729
0
      state->top--;
730
0
    }
731
0
  }
732
733
0
  return 0;
734
0
}
735
736
0
void ERR_load_crypto_strings() {}
737
738
0
void ERR_free_strings() {}
739
740
0
void ERR_load_BIO_strings() {}
741
742
0
void ERR_load_ERR_strings() {}
743
744
0
void ERR_load_RAND_strings() {}
745
746
BSSL_NAMESPACE_BEGIN
747
748
struct err_save_state_st {
749
  struct err_error_st *errors;
750
  size_t num_errors;
751
};
752
753
BSSL_NAMESPACE_END
754
755
30.6k
void bssl::ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) {
756
30.6k
  if (state == nullptr) {
757
0
    return;
758
0
  }
759
87.8k
  for (size_t i = 0; i < state->num_errors; i++) {
760
57.1k
    err_clear(&state->errors[i]);
761
57.1k
  }
762
30.6k
  free(state->errors);
763
30.6k
  free(state);
764
30.6k
}
765
766
31.3k
ERR_SAVE_STATE *bssl::ERR_save_state() {
767
31.3k
  ERR_STATE *const state = err_get_state();
768
31.3k
  if (state == nullptr || state->top == state->bottom) {
769
629
    return nullptr;
770
629
  }
771
772
30.6k
  ERR_SAVE_STATE *ret =
773
30.6k
      reinterpret_cast<ERR_SAVE_STATE *>(malloc(sizeof(ERR_SAVE_STATE)));
774
30.6k
  if (ret == nullptr) {
775
0
    return nullptr;
776
0
  }
777
778
  // Errors are stored in the range (bottom, top].
779
30.6k
  size_t num_errors = state->top >= state->bottom
780
30.6k
                          ? state->top - state->bottom
781
30.6k
                          : ERR_NUM_ERRORS + state->top - state->bottom;
782
30.6k
  assert(num_errors < ERR_NUM_ERRORS);
783
30.6k
  ret->errors = reinterpret_cast<err_error_st *>(
784
30.6k
      malloc(num_errors * sizeof(struct err_error_st)));
785
30.6k
  if (ret->errors == nullptr) {
786
0
    free(ret);
787
0
    return nullptr;
788
0
  }
789
30.6k
  OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st));
790
30.6k
  ret->num_errors = num_errors;
791
792
87.8k
  for (size_t i = 0; i < num_errors; i++) {
793
57.1k
    size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS;
794
57.1k
    err_copy(&ret->errors[i], &state->errors[j]);
795
57.1k
  }
796
30.6k
  return ret;
797
30.6k
}
798
799
15.5k
void bssl::ERR_restore_state(const ERR_SAVE_STATE *state) {
800
15.5k
  if (state == nullptr || state->num_errors == 0) {
801
622
    ERR_clear_error();
802
622
    return;
803
622
  }
804
805
14.9k
  if (state->num_errors >= ERR_NUM_ERRORS) {
806
0
    abort();
807
0
  }
808
809
14.9k
  ERR_STATE *const dst = err_get_state();
810
14.9k
  if (dst == nullptr) {
811
0
    return;
812
0
  }
813
814
42.2k
  for (size_t i = 0; i < state->num_errors; i++) {
815
27.2k
    err_copy(&dst->errors[i], &state->errors[i]);
816
27.2k
  }
817
14.9k
  dst->top = (unsigned)(state->num_errors - 1);
818
14.9k
  dst->bottom = ERR_NUM_ERRORS - 1;
819
14.9k
}