Coverage Report

Created: 2025-08-28 06:59

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