Coverage Report

Created: 2022-08-24 06:31

/src/libressl/crypto/err/err.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: err.c,v 1.48 2019/10/17 14:28:53 jsing Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
/* ====================================================================
59
 * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60
 *
61
 * Redistribution and use in source and binary forms, with or without
62
 * modification, are permitted provided that the following conditions
63
 * are met:
64
 *
65
 * 1. Redistributions of source code must retain the above copyright
66
 *    notice, this list of conditions and the following disclaimer.
67
 *
68
 * 2. Redistributions in binary form must reproduce the above copyright
69
 *    notice, this list of conditions and the following disclaimer in
70
 *    the documentation and/or other materials provided with the
71
 *    distribution.
72
 *
73
 * 3. All advertising materials mentioning features or use of this
74
 *    software must display the following acknowledgment:
75
 *    "This product includes software developed by the OpenSSL Project
76
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77
 *
78
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79
 *    endorse or promote products derived from this software without
80
 *    prior written permission. For written permission, please contact
81
 *    openssl-core@openssl.org.
82
 *
83
 * 5. Products derived from this software may not be called "OpenSSL"
84
 *    nor may "OpenSSL" appear in their names without prior written
85
 *    permission of the OpenSSL Project.
86
 *
87
 * 6. Redistributions of any form whatsoever must retain the following
88
 *    acknowledgment:
89
 *    "This product includes software developed by the OpenSSL Project
90
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91
 *
92
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103
 * OF THE POSSIBILITY OF SUCH DAMAGE.
104
 * ====================================================================
105
 *
106
 * This product includes cryptographic software written by Eric Young
107
 * (eay@cryptsoft.com).  This product includes software written by Tim
108
 * Hudson (tjh@cryptsoft.com).
109
 *
110
 */
111
112
#include <pthread.h>
113
#include <stdarg.h>
114
#include <stdio.h>
115
#include <string.h>
116
117
#include <openssl/opensslconf.h>
118
119
#include <openssl/bio.h>
120
#include <openssl/buffer.h>
121
#include <openssl/crypto.h>
122
#include <openssl/err.h>
123
#include <openssl/lhash.h>
124
125
DECLARE_LHASH_OF(ERR_STRING_DATA);
126
DECLARE_LHASH_OF(ERR_STATE);
127
128
static void err_load_strings(int lib, ERR_STRING_DATA *str);
129
130
static void ERR_STATE_free(ERR_STATE *s);
131
#ifndef OPENSSL_NO_ERR
132
static ERR_STRING_DATA ERR_str_libraries[] = {
133
  {ERR_PACK(ERR_LIB_NONE,0,0),    "unknown library"},
134
  {ERR_PACK(ERR_LIB_SYS,0,0),   "system library"},
135
  {ERR_PACK(ERR_LIB_BN,0,0),    "bignum routines"},
136
  {ERR_PACK(ERR_LIB_RSA,0,0),   "rsa routines"},
137
  {ERR_PACK(ERR_LIB_DH,0,0),    "Diffie-Hellman routines"},
138
  {ERR_PACK(ERR_LIB_EVP,0,0),   "digital envelope routines"},
139
  {ERR_PACK(ERR_LIB_BUF,0,0),   "memory buffer routines"},
140
  {ERR_PACK(ERR_LIB_OBJ,0,0),   "object identifier routines"},
141
  {ERR_PACK(ERR_LIB_PEM,0,0),   "PEM routines"},
142
  {ERR_PACK(ERR_LIB_DSA,0,0),   "dsa routines"},
143
  {ERR_PACK(ERR_LIB_X509,0,0),    "x509 certificate routines"},
144
  {ERR_PACK(ERR_LIB_ASN1,0,0),    "asn1 encoding routines"},
145
  {ERR_PACK(ERR_LIB_CONF,0,0),    "configuration file routines"},
146
  {ERR_PACK(ERR_LIB_CRYPTO,0,0),    "common libcrypto routines"},
147
  {ERR_PACK(ERR_LIB_EC,0,0),    "elliptic curve routines"},
148
  {ERR_PACK(ERR_LIB_SSL,0,0),   "SSL routines"},
149
  {ERR_PACK(ERR_LIB_BIO,0,0),   "BIO routines"},
150
  {ERR_PACK(ERR_LIB_PKCS7,0,0),   "PKCS7 routines"},
151
  {ERR_PACK(ERR_LIB_X509V3,0,0),    "X509 V3 routines"},
152
  {ERR_PACK(ERR_LIB_PKCS12,0,0),    "PKCS12 routines"},
153
  {ERR_PACK(ERR_LIB_RAND,0,0),    "random number generator"},
154
  {ERR_PACK(ERR_LIB_DSO,0,0),   "DSO support routines"},
155
  {ERR_PACK(ERR_LIB_TS,0,0),    "time stamp routines"},
156
  {ERR_PACK(ERR_LIB_ENGINE,0,0),    "engine routines"},
157
  {ERR_PACK(ERR_LIB_OCSP,0,0),    "OCSP routines"},
158
  {ERR_PACK(ERR_LIB_FIPS,0,0),    "FIPS routines"},
159
  {ERR_PACK(ERR_LIB_CMS,0,0),   "CMS routines"},
160
  {ERR_PACK(ERR_LIB_HMAC,0,0),    "HMAC routines"},
161
  {ERR_PACK(ERR_LIB_GOST,0,0),    "GOST routines"},
162
  {0, NULL},
163
};
164
165
static ERR_STRING_DATA ERR_str_functs[] = {
166
  {ERR_PACK(0,SYS_F_FOPEN, 0),      "fopen"},
167
  {ERR_PACK(0,SYS_F_CONNECT, 0),    "connect"},
168
  {ERR_PACK(0,SYS_F_GETSERVBYNAME, 0),  "getservbyname"},
169
  {ERR_PACK(0,SYS_F_SOCKET, 0),   "socket"},
170
  {ERR_PACK(0,SYS_F_IOCTLSOCKET, 0),  "ioctl"},
171
  {ERR_PACK(0,SYS_F_BIND, 0),   "bind"},
172
  {ERR_PACK(0,SYS_F_LISTEN, 0),   "listen"},
173
  {ERR_PACK(0,SYS_F_ACCEPT, 0),   "accept"},
174
  {ERR_PACK(0,SYS_F_OPENDIR, 0),    "opendir"},
175
  {ERR_PACK(0,SYS_F_FREAD, 0),    "fread"},
176
  {0, NULL},
177
};
178
179
static ERR_STRING_DATA ERR_str_reasons[] = {
180
  {ERR_R_SYS_LIB,       "system lib"},
181
  {ERR_R_BN_LIB,        "BN lib"},
182
  {ERR_R_RSA_LIB,       "RSA lib"},
183
  {ERR_R_DH_LIB,        "DH lib"},
184
  {ERR_R_EVP_LIB,       "EVP lib"},
185
  {ERR_R_BUF_LIB,       "BUF lib"},
186
  {ERR_R_OBJ_LIB,       "OBJ lib"},
187
  {ERR_R_PEM_LIB,       "PEM lib"},
188
  {ERR_R_DSA_LIB,       "DSA lib"},
189
  {ERR_R_X509_LIB,      "X509 lib"},
190
  {ERR_R_ASN1_LIB,      "ASN1 lib"},
191
  {ERR_R_CONF_LIB,      "CONF lib"},
192
  {ERR_R_CRYPTO_LIB,      "CRYPTO lib"},
193
  {ERR_R_EC_LIB,        "EC lib"},
194
  {ERR_R_SSL_LIB,       "SSL lib"},
195
  {ERR_R_BIO_LIB,       "BIO lib"},
196
  {ERR_R_PKCS7_LIB,     "PKCS7 lib"},
197
  {ERR_R_X509V3_LIB,      "X509V3 lib"},
198
  {ERR_R_PKCS12_LIB,      "PKCS12 lib"},
199
  {ERR_R_RAND_LIB,      "RAND lib"},
200
  {ERR_R_DSO_LIB,       "DSO lib"},
201
  {ERR_R_ENGINE_LIB,      "ENGINE lib"},
202
  {ERR_R_OCSP_LIB,      "OCSP lib"},
203
  {ERR_R_TS_LIB,        "TS lib"},
204
205
  {ERR_R_NESTED_ASN1_ERROR,   "nested asn1 error"},
206
  {ERR_R_BAD_ASN1_OBJECT_HEADER,    "bad asn1 object header"},
207
  {ERR_R_BAD_GET_ASN1_OBJECT_CALL,  "bad get asn1 object call"},
208
  {ERR_R_EXPECTING_AN_ASN1_SEQUENCE,  "expecting an asn1 sequence"},
209
  {ERR_R_ASN1_LENGTH_MISMATCH,    "asn1 length mismatch"},
210
  {ERR_R_MISSING_ASN1_EOS,    "missing asn1 eos"},
211
212
  {ERR_R_FATAL,       "fatal"},
213
  {ERR_R_MALLOC_FAILURE,      "malloc failure"},
214
  {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED, "called a function you should not call"},
215
  {ERR_R_PASSED_NULL_PARAMETER,   "passed a null parameter"},
216
  {ERR_R_INTERNAL_ERROR,      "internal error"},
217
  {ERR_R_DISABLED ,     "called a function that was disabled at compile-time"},
218
219
  {0, NULL},
220
};
221
#endif
222
223
224
/* Define the predeclared (but externally opaque) "ERR_FNS" type */
225
struct st_ERR_FNS {
226
  /* Works on the "error_hash" string table */
227
  LHASH_OF(ERR_STRING_DATA) *(*cb_err_get)(int create);
228
  void (*cb_err_del)(void);
229
  ERR_STRING_DATA *(*cb_err_get_item)(const ERR_STRING_DATA *);
230
  ERR_STRING_DATA *(*cb_err_set_item)(ERR_STRING_DATA *);
231
  ERR_STRING_DATA *(*cb_err_del_item)(ERR_STRING_DATA *);
232
  /* Works on the "thread_hash" error-state table */
233
  LHASH_OF(ERR_STATE) *(*cb_thread_get)(int create);
234
  void (*cb_thread_release)(LHASH_OF(ERR_STATE) **hash);
235
  ERR_STATE *(*cb_thread_get_item)(const ERR_STATE *);
236
  ERR_STATE *(*cb_thread_set_item)(ERR_STATE *);
237
  void (*cb_thread_del_item)(const ERR_STATE *);
238
  /* Returns the next available error "library" numbers */
239
  int (*cb_get_next_lib)(void);
240
};
241
242
/* Predeclarations of the "err_defaults" functions */
243
static LHASH_OF(ERR_STRING_DATA) *int_err_get(int create);
244
static void int_err_del(void);
245
static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
246
static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *);
247
static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *);
248
static LHASH_OF(ERR_STATE) *int_thread_get(int create);
249
static void int_thread_release(LHASH_OF(ERR_STATE) **hash);
250
static ERR_STATE *int_thread_get_item(const ERR_STATE *);
251
static ERR_STATE *int_thread_set_item(ERR_STATE *);
252
static void int_thread_del_item(const ERR_STATE *);
253
static int int_err_get_next_lib(void);
254
255
/* The static ERR_FNS table using these defaults functions */
256
static const ERR_FNS err_defaults = {
257
  int_err_get,
258
  int_err_del,
259
  int_err_get_item,
260
  int_err_set_item,
261
  int_err_del_item,
262
  int_thread_get,
263
  int_thread_release,
264
  int_thread_get_item,
265
  int_thread_set_item,
266
  int_thread_del_item,
267
  int_err_get_next_lib
268
};
269
270
/* The replacable table of ERR_FNS functions we use at run-time */
271
static const ERR_FNS *err_fns = NULL;
272
273
/* Eg. rather than using "err_get()", use "ERRFN(err_get)()". */
274
21.6M
#define ERRFN(a) err_fns->cb_##a
275
276
/* The internal state used by "err_defaults" - as such, the setting, reading,
277
 * creating, and deleting of this data should only be permitted via the
278
 * "err_defaults" functions. This way, a linked module can completely defer all
279
 * ERR state operation (together with requisite locking) to the implementations
280
 * and state in the loading application. */
281
static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
282
static LHASH_OF(ERR_STATE) *int_thread_hash = NULL;
283
static int int_thread_hash_references = 0;
284
static int int_err_library_number = ERR_LIB_USER;
285
286
static pthread_t err_init_thread;
287
288
/* Internal function that checks whether "err_fns" is set and if not, sets it to
289
 * the defaults. */
290
static void
291
err_fns_check(void)
292
14.4M
{
293
14.4M
  if (err_fns)
294
14.4M
    return;
295
296
2
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
297
2
  if (!err_fns)
298
2
    err_fns = &err_defaults;
299
2
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
300
2
}
301
302
/* API functions to get or set the underlying ERR functions. */
303
304
const ERR_FNS *
305
ERR_get_implementation(void)
306
0
{
307
0
  err_fns_check();
308
0
  return err_fns;
309
0
}
310
311
int
312
ERR_set_implementation(const ERR_FNS *fns)
313
0
{
314
0
  int ret = 0;
315
316
0
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
317
  /* It's too late if 'err_fns' is non-NULL. BTW: not much point setting
318
   * an error is there?! */
319
0
  if (!err_fns) {
320
0
    err_fns = fns;
321
0
    ret = 1;
322
0
  }
323
0
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
324
0
  return ret;
325
0
}
326
327
/* These are the callbacks provided to "lh_new()" when creating the LHASH tables
328
 * internal to the "err_defaults" implementation. */
329
330
static unsigned long get_error_values(int inc, int top, const char **file,
331
    int *line, const char **data, int *flags);
332
333
/* The internal functions used in the "err_defaults" implementation */
334
335
static unsigned long
336
err_string_data_hash(const ERR_STRING_DATA *a)
337
3.12k
{
338
3.12k
  unsigned long ret, l;
339
340
3.12k
  l = a->error;
341
3.12k
  ret = l^ERR_GET_LIB(l)^ERR_GET_FUNC(l);
342
3.12k
  return (ret^ret % 19*13);
343
3.12k
}
344
static IMPLEMENT_LHASH_HASH_FN(err_string_data, ERR_STRING_DATA)
345
346
static int
347
err_string_data_cmp(const ERR_STRING_DATA *a, const ERR_STRING_DATA *b)
348
200
{
349
200
  return (int)(a->error - b->error);
350
200
}
351
static IMPLEMENT_LHASH_COMP_FN(err_string_data, ERR_STRING_DATA)
352
353
static
354
LHASH_OF(ERR_STRING_DATA) *int_err_get(int create)
355
3.12k
{
356
3.12k
  LHASH_OF(ERR_STRING_DATA) *ret = NULL;
357
358
3.12k
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
359
3.12k
  if (!int_error_hash && create) {
360
2
    CRYPTO_push_info("int_err_get (err.c)");
361
2
    int_error_hash = lh_ERR_STRING_DATA_new();
362
2
    CRYPTO_pop_info();
363
2
  }
364
3.12k
  if (int_error_hash)
365
3.12k
    ret = int_error_hash;
366
3.12k
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
367
368
3.12k
  return ret;
369
3.12k
}
370
371
static void
372
int_err_del(void)
373
0
{
374
0
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
375
0
  if (int_error_hash) {
376
0
    lh_ERR_STRING_DATA_free(int_error_hash);
377
0
    int_error_hash = NULL;
378
0
  }
379
0
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
380
0
}
381
382
static ERR_STRING_DATA *
383
int_err_get_item(const ERR_STRING_DATA *d)
384
60
{
385
60
  ERR_STRING_DATA *p;
386
60
  LHASH_OF(ERR_STRING_DATA) *hash;
387
388
60
  err_fns_check();
389
60
  hash = ERRFN(err_get)(0);
390
60
  if (!hash)
391
0
    return NULL;
392
393
60
  CRYPTO_r_lock(CRYPTO_LOCK_ERR);
394
60
  p = lh_ERR_STRING_DATA_retrieve(hash, d);
395
60
  CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
396
397
60
  return p;
398
60
}
399
400
static ERR_STRING_DATA *
401
int_err_set_item(ERR_STRING_DATA *d)
402
3.06k
{
403
3.06k
  ERR_STRING_DATA *p;
404
3.06k
  LHASH_OF(ERR_STRING_DATA) *hash;
405
406
3.06k
  err_fns_check();
407
3.06k
  hash = ERRFN(err_get)(1);
408
3.06k
  if (!hash)
409
0
    return NULL;
410
411
3.06k
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
412
3.06k
  p = lh_ERR_STRING_DATA_insert(hash, d);
413
3.06k
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
414
415
3.06k
  return p;
416
3.06k
}
417
418
static ERR_STRING_DATA *
419
int_err_del_item(ERR_STRING_DATA *d)
420
0
{
421
0
  ERR_STRING_DATA *p;
422
0
  LHASH_OF(ERR_STRING_DATA) *hash;
423
424
0
  err_fns_check();
425
0
  hash = ERRFN(err_get)(0);
426
0
  if (!hash)
427
0
    return NULL;
428
429
0
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
430
0
  p = lh_ERR_STRING_DATA_delete(hash, d);
431
0
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
432
433
0
  return p;
434
0
}
435
436
static unsigned long
437
err_state_hash(const ERR_STATE *a)
438
7.22M
{
439
7.22M
  return CRYPTO_THREADID_hash(&a->tid) * 13;
440
7.22M
}
441
static IMPLEMENT_LHASH_HASH_FN(err_state, ERR_STATE)
442
443
static int
444
err_state_cmp(const ERR_STATE *a, const ERR_STATE *b)
445
7.22M
{
446
7.22M
  return CRYPTO_THREADID_cmp(&a->tid, &b->tid);
447
7.22M
}
448
static IMPLEMENT_LHASH_COMP_FN(err_state, ERR_STATE)
449
450
static
451
LHASH_OF(ERR_STATE) *int_thread_get(int create)
452
7.22M
{
453
7.22M
  LHASH_OF(ERR_STATE) *ret = NULL;
454
455
7.22M
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
456
7.22M
  if (!int_thread_hash && create) {
457
2
    CRYPTO_push_info("int_thread_get (err.c)");
458
2
    int_thread_hash = lh_ERR_STATE_new();
459
2
    CRYPTO_pop_info();
460
2
  }
461
7.22M
  if (int_thread_hash) {
462
7.22M
    int_thread_hash_references++;
463
7.22M
    ret = int_thread_hash;
464
7.22M
  }
465
7.22M
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
466
7.22M
  return ret;
467
7.22M
}
468
469
static void
470
int_thread_release(LHASH_OF(ERR_STATE) **hash)
471
7.22M
{
472
7.22M
  int i;
473
474
7.22M
  if (hash == NULL || *hash == NULL)
475
0
    return;
476
477
7.22M
  i = CRYPTO_add(&int_thread_hash_references, -1, CRYPTO_LOCK_ERR);
478
7.22M
  if (i > 0)
479
0
    return;
480
481
7.22M
  *hash = NULL;
482
7.22M
}
483
484
static ERR_STATE *
485
int_thread_get_item(const ERR_STATE *d)
486
7.22M
{
487
7.22M
  ERR_STATE *p;
488
7.22M
  LHASH_OF(ERR_STATE) *hash;
489
490
7.22M
  err_fns_check();
491
7.22M
  hash = ERRFN(thread_get)(0);
492
7.22M
  if (!hash)
493
2
    return NULL;
494
495
7.22M
  CRYPTO_r_lock(CRYPTO_LOCK_ERR);
496
7.22M
  p = lh_ERR_STATE_retrieve(hash, d);
497
7.22M
  CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
498
499
7.22M
  ERRFN(thread_release)(&hash);
500
7.22M
  return p;
501
7.22M
}
502
503
static ERR_STATE *
504
int_thread_set_item(ERR_STATE *d)
505
2
{
506
2
  ERR_STATE *p;
507
2
  LHASH_OF(ERR_STATE) *hash;
508
509
2
  err_fns_check();
510
2
  hash = ERRFN(thread_get)(1);
511
2
  if (!hash)
512
0
    return NULL;
513
514
2
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
515
2
  p = lh_ERR_STATE_insert(hash, d);
516
2
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
517
518
2
  ERRFN(thread_release)(&hash);
519
2
  return p;
520
2
}
521
522
static void
523
int_thread_del_item(const ERR_STATE *d)
524
0
{
525
0
  ERR_STATE *p;
526
0
  LHASH_OF(ERR_STATE) *hash;
527
528
0
  err_fns_check();
529
0
  hash = ERRFN(thread_get)(0);
530
0
  if (!hash)
531
0
    return;
532
533
0
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
534
0
  p = lh_ERR_STATE_delete(hash, d);
535
  /* make sure we don't leak memory */
536
0
  if (int_thread_hash_references == 1 &&
537
0
      int_thread_hash && lh_ERR_STATE_num_items(int_thread_hash) == 0) {
538
0
    lh_ERR_STATE_free(int_thread_hash);
539
0
    int_thread_hash = NULL;
540
0
  }
541
0
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
542
543
0
  ERRFN(thread_release)(&hash);
544
0
  if (p)
545
0
    ERR_STATE_free(p);
546
0
}
547
548
static int
549
int_err_get_next_lib(void)
550
0
{
551
0
  int ret;
552
553
0
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
554
0
  ret = int_err_library_number++;
555
0
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
556
557
0
  return ret;
558
0
}
559
560
561
#ifndef OPENSSL_NO_ERR
562
256
#define NUM_SYS_STR_REASONS 127
563
#define LEN_SYS_STR_REASON 32
564
565
static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
566
/* SYS_str_reasons is filled with copies of strerror() results at
567
 * initialization.
568
 * 'errno' values up to 127 should cover all usual errors,
569
 * others will be displayed numerically by ERR_error_string.
570
 * It is crucial that we have something for each reason code
571
 * that occurs in ERR_str_reasons, or bogus reason strings
572
 * will be returned for SYSerror(which always gets an errno
573
 * value and never one of those 'standard' reason codes. */
574
575
static void
576
build_SYS_str_reasons(void)
577
2
{
578
  /* malloc cannot be used here, use static storage instead */
579
2
  static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON];
580
2
  int i;
581
2
  static int init = 1;
582
583
2
  CRYPTO_r_lock(CRYPTO_LOCK_ERR);
584
2
  if (!init) {
585
0
    CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
586
0
    return;
587
0
  }
588
589
2
  CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
590
2
  CRYPTO_w_lock(CRYPTO_LOCK_ERR);
591
2
  if (!init) {
592
0
    CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
593
0
    return;
594
0
  }
595
596
256
  for (i = 1; i <= NUM_SYS_STR_REASONS; i++) {
597
254
    ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
598
599
254
    str->error = (unsigned long)i;
600
254
    if (str->string == NULL) {
601
254
      char (*dest)[LEN_SYS_STR_REASON] =
602
254
          &(strerror_tab[i - 1]);
603
254
      const char *src = strerror(i);
604
254
      if (src != NULL) {
605
254
        strlcpy(*dest, src, sizeof *dest);
606
254
        str->string = *dest;
607
254
      }
608
254
    }
609
254
    if (str->string == NULL)
610
0
      str->string = "unknown";
611
254
  }
612
613
  /* Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL},
614
   * as required by ERR_load_strings. */
615
616
2
  init = 0;
617
618
2
  CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
619
2
}
620
#endif
621
622
#define err_clear_data(p,i) \
623
7.51M
  do { \
624
7.51M
    if (((p)->err_data[i] != NULL) && \
625
7.51M
        (p)->err_data_flags[i] & ERR_TXT_MALLOCED) { \
626
1.98M
      free((p)->err_data[i]); \
627
1.98M
      (p)->err_data[i] = NULL; \
628
1.98M
    } \
629
7.51M
    (p)->err_data_flags[i] = 0; \
630
7.51M
  } while(0)
631
632
#define err_clear(p,i) \
633
311k
  do { \
634
311k
    (p)->err_flags[i] = 0; \
635
311k
    (p)->err_buffer[i] = 0; \
636
311k
    err_clear_data(p, i); \
637
311k
    (p)->err_file[i] = NULL; \
638
311k
    (p)->err_line[i] = -1; \
639
311k
  } while(0)
640
641
static void
642
ERR_STATE_free(ERR_STATE *s)
643
0
{
644
0
  int i;
645
646
0
  if (s == NULL)
647
0
    return;
648
649
0
  for (i = 0; i < ERR_NUM_ERRORS; i++) {
650
0
    err_clear_data(s, i);
651
0
  }
652
0
  free(s);
653
0
}
654
655
void
656
ERR_load_ERR_strings_internal(void)
657
2
{
658
2
  err_init_thread = pthread_self();
659
2
  err_fns_check();
660
2
#ifndef OPENSSL_NO_ERR
661
2
  err_load_strings(0, ERR_str_libraries);
662
2
  err_load_strings(0, ERR_str_reasons);
663
2
  err_load_strings(ERR_LIB_SYS, ERR_str_functs);
664
2
  build_SYS_str_reasons();
665
2
  err_load_strings(ERR_LIB_SYS, SYS_str_reasons);
666
2
#endif
667
2
}
668
669
670
void
671
ERR_load_ERR_strings(void)
672
116
{
673
116
  static pthread_once_t once = PTHREAD_ONCE_INIT;
674
675
116
  if (pthread_equal(pthread_self(), err_init_thread))
676
116
    return; /* don't recurse */
677
678
  /* Prayer and clean living lets you ignore errors, OpenSSL style */
679
0
  (void) OPENSSL_init_crypto(0, NULL);
680
681
0
  (void) pthread_once(&once, ERR_load_ERR_strings_internal);
682
0
}
683
684
static void
685
err_load_strings(int lib, ERR_STRING_DATA *str)
686
124
{
687
3.18k
  while (str->error) {
688
3.06k
    if (lib)
689
440
      str->error |= ERR_PACK(lib, 0, 0);
690
3.06k
    ERRFN(err_set_item)(str);
691
3.06k
    str++;
692
3.06k
  }
693
124
}
694
695
void
696
ERR_load_strings(int lib, ERR_STRING_DATA *str)
697
116
{
698
116
  ERR_load_ERR_strings();
699
116
  err_load_strings(lib, str);
700
116
}
701
702
void
703
ERR_unload_strings(int lib, ERR_STRING_DATA *str)
704
0
{
705
  /* Prayer and clean living lets you ignore errors, OpenSSL style */
706
0
  (void) OPENSSL_init_crypto(0, NULL);
707
708
0
  while (str->error) {
709
0
    if (lib)
710
0
      str->error |= ERR_PACK(lib, 0, 0);
711
0
    ERRFN(err_del_item)(str);
712
0
    str++;
713
0
  }
714
0
}
715
716
void
717
ERR_free_strings(void)
718
0
{
719
  /* Prayer and clean living lets you ignore errors, OpenSSL style */
720
0
  (void) OPENSSL_init_crypto(0, NULL);
721
722
0
  err_fns_check();
723
0
  ERRFN(err_del)();
724
0
}
725
726
/********************************************************/
727
728
void
729
ERR_put_error(int lib, int func, int reason, const char *file, int line)
730
5.22M
{
731
5.22M
  ERR_STATE *es;
732
5.22M
  int save_errno = errno;
733
734
5.22M
  es = ERR_get_state();
735
736
5.22M
  es->top = (es->top + 1) % ERR_NUM_ERRORS;
737
5.22M
  if (es->top == es->bottom)
738
4.98M
    es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
739
5.22M
  es->err_flags[es->top] = 0;
740
5.22M
  es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
741
5.22M
  es->err_file[es->top] = file;
742
5.22M
  es->err_line[es->top] = line;
743
5.22M
  err_clear_data(es, es->top);
744
5.22M
  errno = save_errno;
745
5.22M
}
746
747
void
748
ERR_clear_error(void)
749
19.4k
{
750
19.4k
  int i;
751
19.4k
  ERR_STATE *es;
752
753
19.4k
  es = ERR_get_state();
754
755
331k
  for (i = 0; i < ERR_NUM_ERRORS; i++) {
756
311k
    err_clear(es, i);
757
311k
  }
758
19.4k
  es->top = es->bottom = 0;
759
19.4k
}
760
761
762
unsigned long
763
ERR_get_error(void)
764
0
{
765
0
  return (get_error_values(1, 0, NULL, NULL, NULL, NULL));
766
0
}
767
768
unsigned long
769
ERR_get_error_line(const char **file, int *line)
770
0
{
771
0
  return (get_error_values(1, 0, file, line, NULL, NULL));
772
0
}
773
774
unsigned long
775
ERR_get_error_line_data(const char **file, int *line,
776
    const char **data, int *flags)
777
0
{
778
0
  return (get_error_values(1, 0, file, line, data, flags));
779
0
}
780
781
782
unsigned long
783
ERR_peek_error(void)
784
0
{
785
0
  return (get_error_values(0, 0, NULL, NULL, NULL, NULL));
786
0
}
787
788
unsigned long
789
ERR_peek_error_line(const char **file, int *line)
790
0
{
791
0
  return (get_error_values(0, 0, file, line, NULL, NULL));
792
0
}
793
794
unsigned long
795
ERR_peek_error_line_data(const char **file, int *line,
796
    const char **data, int *flags)
797
0
{
798
0
  return (get_error_values(0, 0, file, line, data, flags));
799
0
}
800
801
unsigned long
802
ERR_peek_last_error(void)
803
1.49k
{
804
1.49k
  return (get_error_values(0, 1, NULL, NULL, NULL, NULL));
805
1.49k
}
806
807
unsigned long
808
ERR_peek_last_error_line(const char **file, int *line)
809
0
{
810
0
  return (get_error_values(0, 1, file, line, NULL, NULL));
811
0
}
812
813
unsigned long
814
ERR_peek_last_error_line_data(const char **file, int *line,
815
    const char **data, int *flags)
816
0
{
817
0
  return (get_error_values(0, 1, file, line, data, flags));
818
0
}
819
820
static unsigned long
821
get_error_values(int inc, int top, const char **file, int *line,
822
    const char **data, int *flags)
823
1.49k
{
824
1.49k
  int i = 0;
825
1.49k
  ERR_STATE *es;
826
1.49k
  unsigned long ret;
827
828
1.49k
  es = ERR_get_state();
829
830
1.49k
  if (inc && top) {
831
0
    if (file)
832
0
      *file = "";
833
0
    if (line)
834
0
      *line = 0;
835
0
    if (data)
836
0
      *data = "";
837
0
    if (flags)
838
0
      *flags = 0;
839
840
0
    return ERR_R_INTERNAL_ERROR;
841
0
  }
842
843
1.49k
  if (es->bottom == es->top)
844
0
    return 0;
845
1.49k
  if (top)
846
1.49k
    i = es->top;       /* last error */
847
0
  else
848
0
    i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
849
850
1.49k
  ret = es->err_buffer[i];
851
1.49k
  if (inc) {
852
0
    es->bottom = i;
853
0
    es->err_buffer[i] = 0;
854
0
  }
855
856
1.49k
  if ((file != NULL) && (line != NULL)) {
857
0
    if (es->err_file[i] == NULL) {
858
0
      *file = "NA";
859
0
      if (line != NULL)
860
0
        *line = 0;
861
0
    } else {
862
0
      *file = es->err_file[i];
863
0
      if (line != NULL)
864
0
        *line = es->err_line[i];
865
0
    }
866
0
  }
867
868
1.49k
  if (data == NULL) {
869
1.49k
    if (inc) {
870
0
      err_clear_data(es, i);
871
0
    }
872
1.49k
  } else {
873
0
    if (es->err_data[i] == NULL) {
874
0
      *data = "";
875
0
      if (flags != NULL)
876
0
        *flags = 0;
877
0
    } else {
878
0
      *data = es->err_data[i];
879
0
      if (flags != NULL)
880
0
        *flags = es->err_data_flags[i];
881
0
    }
882
0
  }
883
1.49k
  return ret;
884
1.49k
}
885
886
void
887
ERR_error_string_n(unsigned long e, char *buf, size_t len)
888
0
{
889
0
  char lsbuf[30], fsbuf[30], rsbuf[30];
890
0
  const char *ls, *fs, *rs;
891
0
  int l, f, r, ret;
892
893
0
  l = ERR_GET_LIB(e);
894
0
  f = ERR_GET_FUNC(e);
895
0
  r = ERR_GET_REASON(e);
896
897
0
  ls = ERR_lib_error_string(e);
898
0
  fs = ERR_func_error_string(e);
899
0
  rs = ERR_reason_error_string(e);
900
901
0
  if (ls == NULL) {
902
0
    (void) snprintf(lsbuf, sizeof(lsbuf), "lib(%d)", l);
903
0
    ls = lsbuf;
904
0
  }
905
0
  if (fs == NULL) {
906
0
    (void) snprintf(fsbuf, sizeof(fsbuf), "func(%d)", f);
907
0
    fs = fsbuf;
908
0
  }
909
0
  if (rs == NULL) {
910
0
    (void) snprintf(rsbuf, sizeof(rsbuf), "reason(%d)", r);
911
0
    rs = rsbuf;
912
0
  }
913
914
0
  ret = snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs);
915
0
  if (ret == -1)
916
0
    return; /* can't happen, and can't do better if it does */
917
0
  if (ret >= len) {
918
    /* output may be truncated; make sure we always have 5
919
     * colon-separated fields, i.e. 4 colons ... */
920
0
#define NUM_COLONS 4
921
0
    if (len > NUM_COLONS) /* ... if possible */
922
0
    {
923
0
      int i;
924
0
      char *s = buf;
925
926
0
      for (i = 0; i < NUM_COLONS; i++) {
927
0
        char *colon = strchr(s, ':');
928
0
        if (colon == NULL ||
929
0
            colon > &buf[len - 1] - NUM_COLONS + i) {
930
          /* set colon no. i at last possible position
931
           * (buf[len-1] is the terminating 0)*/
932
0
          colon = &buf[len - 1] - NUM_COLONS + i;
933
0
          *colon = ':';
934
0
        }
935
0
        s = colon + 1;
936
0
      }
937
0
    }
938
0
  }
939
0
}
940
941
/* BAD for multi-threading: uses a local buffer if ret == NULL */
942
/* ERR_error_string_n should be used instead for ret != NULL
943
 * as ERR_error_string cannot know how large the buffer is */
944
char *
945
ERR_error_string(unsigned long e, char *ret)
946
0
{
947
0
  static char buf[256];
948
949
0
  if (ret == NULL)
950
0
    ret = buf;
951
0
  ERR_error_string_n(e, ret, 256);
952
953
0
  return ret;
954
0
}
955
956
LHASH_OF(ERR_STRING_DATA) *ERR_get_string_table(void)
957
0
{
958
0
  err_fns_check();
959
0
  return ERRFN(err_get)(0);
960
0
}
961
962
LHASH_OF(ERR_STATE) *ERR_get_err_state_table(void)
963
0
{
964
0
  err_fns_check();
965
0
  return ERRFN(thread_get)(0);
966
0
}
967
968
void
969
ERR_release_err_state_table(LHASH_OF(ERR_STATE) **hash)
970
0
{
971
0
  err_fns_check();
972
0
  ERRFN(thread_release)(hash);
973
0
}
974
975
const char *
976
ERR_lib_error_string(unsigned long e)
977
0
{
978
0
  ERR_STRING_DATA d, *p;
979
0
  unsigned long l;
980
981
0
  if (!OPENSSL_init_crypto(0, NULL))
982
0
    return NULL;
983
984
0
  err_fns_check();
985
0
  l = ERR_GET_LIB(e);
986
0
  d.error = ERR_PACK(l, 0, 0);
987
0
  p = ERRFN(err_get_item)(&d);
988
0
  return ((p == NULL) ? NULL : p->string);
989
0
}
990
991
const char *
992
ERR_func_error_string(unsigned long e)
993
60
{
994
60
  ERR_STRING_DATA d, *p;
995
60
  unsigned long l, f;
996
997
60
  err_fns_check();
998
60
  l = ERR_GET_LIB(e);
999
60
  f = ERR_GET_FUNC(e);
1000
60
  d.error = ERR_PACK(l, f, 0);
1001
60
  p = ERRFN(err_get_item)(&d);
1002
60
  return ((p == NULL) ? NULL : p->string);
1003
60
}
1004
1005
const char *
1006
ERR_reason_error_string(unsigned long e)
1007
0
{
1008
0
  ERR_STRING_DATA d, *p = NULL;
1009
0
  unsigned long l, r;
1010
1011
0
  err_fns_check();
1012
0
  l = ERR_GET_LIB(e);
1013
0
  r = ERR_GET_REASON(e);
1014
0
  d.error = ERR_PACK(l, 0, r);
1015
0
  p = ERRFN(err_get_item)(&d);
1016
0
  if (!p) {
1017
0
    d.error = ERR_PACK(0, 0, r);
1018
0
    p = ERRFN(err_get_item)(&d);
1019
0
  }
1020
0
  return ((p == NULL) ? NULL : p->string);
1021
0
}
1022
1023
void
1024
ERR_remove_thread_state(const CRYPTO_THREADID *id)
1025
0
{
1026
0
  ERR_STATE tmp;
1027
1028
0
  if (id)
1029
0
    CRYPTO_THREADID_cpy(&tmp.tid, id);
1030
0
  else
1031
0
    CRYPTO_THREADID_current(&tmp.tid);
1032
0
  err_fns_check();
1033
  /* thread_del_item automatically destroys the LHASH if the number of
1034
   * items reaches zero. */
1035
0
  ERRFN(thread_del_item)(&tmp);
1036
0
}
1037
1038
#ifndef OPENSSL_NO_DEPRECATED
1039
void
1040
ERR_remove_state(unsigned long pid)
1041
0
{
1042
0
  ERR_remove_thread_state(NULL);
1043
0
}
1044
#endif
1045
1046
ERR_STATE *
1047
ERR_get_state(void)
1048
7.22M
{
1049
7.22M
  static ERR_STATE fallback;
1050
7.22M
  ERR_STATE *ret, tmp, *tmpp = NULL;
1051
7.22M
  int i;
1052
7.22M
  CRYPTO_THREADID tid;
1053
1054
7.22M
  err_fns_check();
1055
7.22M
  CRYPTO_THREADID_current(&tid);
1056
7.22M
  CRYPTO_THREADID_cpy(&tmp.tid, &tid);
1057
7.22M
  ret = ERRFN(thread_get_item)(&tmp);
1058
1059
  /* ret == the error state, if NULL, make a new one */
1060
7.22M
  if (ret == NULL) {
1061
2
    ret = malloc(sizeof(ERR_STATE));
1062
2
    if (ret == NULL)
1063
0
      return (&fallback);
1064
2
    CRYPTO_THREADID_cpy(&ret->tid, &tid);
1065
2
    ret->top = 0;
1066
2
    ret->bottom = 0;
1067
34
    for (i = 0; i < ERR_NUM_ERRORS; i++) {
1068
32
      ret->err_data[i] = NULL;
1069
32
      ret->err_data_flags[i] = 0;
1070
32
    }
1071
2
    tmpp = ERRFN(thread_set_item)(ret);
1072
    /* To check if insertion failed, do a get. */
1073
2
    if (ERRFN(thread_get_item)(ret) != ret) {
1074
0
      ERR_STATE_free(ret); /* could not insert it */
1075
0
      return (&fallback);
1076
0
    }
1077
    /* If a race occured in this function and we came second, tmpp
1078
     * is the first one that we just replaced. */
1079
2
    if (tmpp)
1080
0
      ERR_STATE_free(tmpp);
1081
2
  }
1082
7.22M
  return ret;
1083
7.22M
}
1084
1085
int
1086
ERR_get_next_error_library(void)
1087
0
{
1088
0
  err_fns_check();
1089
0
  return ERRFN(get_next_lib)();
1090
0
}
1091
1092
void
1093
ERR_set_error_data(char *data, int flags)
1094
1.98M
{
1095
1.98M
  ERR_STATE *es;
1096
1.98M
  int i;
1097
1098
1.98M
  es = ERR_get_state();
1099
1100
1.98M
  i = es->top;
1101
1.98M
  if (i == 0)
1102
104k
    i = ERR_NUM_ERRORS - 1;
1103
1104
1.98M
  err_clear_data(es, i);
1105
1.98M
  es->err_data[i] = data;
1106
1.98M
  es->err_data_flags[i] = flags;
1107
1.98M
}
1108
1109
void
1110
ERR_asprintf_error_data(char * format, ...)
1111
1.98M
{
1112
1.98M
  char *errbuf = NULL;
1113
1.98M
  va_list ap;
1114
1.98M
  int r;
1115
1116
1.98M
  va_start(ap, format);
1117
1.98M
  r = vasprintf(&errbuf, format, ap);
1118
1.98M
  va_end(ap);
1119
1.98M
  if (r == -1)
1120
0
    ERR_set_error_data("malloc failed", ERR_TXT_STRING);
1121
1.98M
  else
1122
1.98M
    ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING);
1123
1.98M
}
1124
1125
void
1126
ERR_add_error_vdata(int num, va_list args)
1127
0
{
1128
0
  char format[129];
1129
0
  char *errbuf;
1130
0
  int i;
1131
1132
0
  format[0] = '\0';
1133
0
  for (i = 0; i < num; i++) {
1134
0
    if (strlcat(format, "%s", sizeof(format)) >= sizeof(format)) {
1135
0
      ERR_set_error_data("too many errors", ERR_TXT_STRING);
1136
0
      return;
1137
0
    }
1138
0
  }
1139
0
  if (vasprintf(&errbuf, format, args) == -1)
1140
0
    ERR_set_error_data("malloc failed", ERR_TXT_STRING);
1141
0
  else
1142
0
    ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING);
1143
0
}
1144
1145
void
1146
ERR_add_error_data(int num, ...)
1147
0
{
1148
0
  va_list args;
1149
0
  va_start(args, num);
1150
0
  ERR_add_error_vdata(num, args);
1151
0
  va_end(args);
1152
0
}
1153
1154
int
1155
ERR_set_mark(void)
1156
0
{
1157
0
  ERR_STATE *es;
1158
1159
0
  es = ERR_get_state();
1160
1161
0
  if (es->bottom == es->top)
1162
0
    return 0;
1163
0
  es->err_flags[es->top] |= ERR_FLAG_MARK;
1164
0
  return 1;
1165
0
}
1166
1167
int
1168
ERR_pop_to_mark(void)
1169
0
{
1170
0
  ERR_STATE *es;
1171
1172
0
  es = ERR_get_state();
1173
1174
0
  while (es->bottom != es->top &&
1175
0
      (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
1176
0
    err_clear(es, es->top);
1177
0
    es->top -= 1;
1178
0
    if (es->top == -1)
1179
0
      es->top = ERR_NUM_ERRORS - 1;
1180
0
  }
1181
1182
0
  if (es->bottom == es->top)
1183
0
    return 0;
1184
0
  es->err_flags[es->top]&=~ERR_FLAG_MARK;
1185
0
  return 1;
1186
0
}
1187
1188
void
1189
err_clear_last_constant_time(int clear)
1190
0
{
1191
0
  ERR_STATE *es;
1192
0
  int top;
1193
1194
0
  es = ERR_get_state();
1195
0
  if (es == NULL)
1196
0
        return;
1197
1198
0
  top = es->top;
1199
1200
0
  es->err_flags[top] &= ~(0 - clear);
1201
0
  es->err_buffer[top] &= ~(0UL - clear);
1202
0
  es->err_file[top] = (const char *)((uintptr_t)es->err_file[top] &
1203
0
      ~((uintptr_t)0 - clear));
1204
0
  es->err_line[top] |= 0 - clear;
1205
1206
0
  es->top = (top + ERR_NUM_ERRORS - clear) % ERR_NUM_ERRORS;
1207
0
}