/src/openssl/crypto/err/err_blocks.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <string.h> |
11 | | #include <openssl/err.h> |
12 | | #include "err_local.h" |
13 | | |
14 | | void ERR_new(void) |
15 | 1.04k | { |
16 | 1.04k | ERR_STATE *es; |
17 | | |
18 | 1.04k | es = ossl_err_get_state_int(); |
19 | 1.04k | if (es == NULL) |
20 | 0 | return; |
21 | | |
22 | | /* Allocate a slot */ |
23 | 1.04k | err_get_slot(es); |
24 | 1.04k | err_clear(es, es->top, 0); |
25 | 1.04k | } |
26 | | |
27 | | void ERR_set_debug(const char *file, int line, const char *func) |
28 | 1.04k | { |
29 | 1.04k | ERR_STATE *es; |
30 | | |
31 | 1.04k | es = ossl_err_get_state_int(); |
32 | 1.04k | if (es == NULL) |
33 | 0 | return; |
34 | | |
35 | 1.04k | err_set_debug(es, es->top, file, line, func); |
36 | 1.04k | } |
37 | | |
38 | | void ERR_set_error(int lib, int reason, const char *fmt, ...) |
39 | 1.04k | { |
40 | 1.04k | va_list args; |
41 | | |
42 | 1.04k | va_start(args, fmt); |
43 | 1.04k | ERR_vset_error(lib, reason, fmt, args); |
44 | 1.04k | va_end(args); |
45 | 1.04k | } |
46 | | |
47 | | void ERR_vset_error(int lib, int reason, const char *fmt, va_list args) |
48 | 1.04k | { |
49 | 1.04k | ERR_STATE *es; |
50 | 1.04k | char *buf = NULL; |
51 | 1.04k | size_t buf_size = 0; |
52 | 1.04k | unsigned long flags = 0; |
53 | 1.04k | size_t i; |
54 | | |
55 | 1.04k | es = ossl_err_get_state_int(); |
56 | 1.04k | if (es == NULL) |
57 | 0 | return; |
58 | 1.04k | i = es->top; |
59 | | |
60 | 1.04k | if (fmt != NULL) { |
61 | 1.03k | int printed_len = 0; |
62 | 1.03k | char *rbuf = NULL; |
63 | | |
64 | 1.03k | buf = es->err_data[i]; |
65 | 1.03k | buf_size = es->err_data_size[i]; |
66 | | |
67 | | /* |
68 | | * To protect the string we just grabbed from tampering by other |
69 | | * functions we may call, or to protect them from freeing a pointer |
70 | | * that may no longer be valid at that point, we clear away the |
71 | | * data pointer and the flags. We will set them again at the end |
72 | | * of this function. |
73 | | */ |
74 | 1.03k | es->err_data[i] = NULL; |
75 | 1.03k | es->err_data_flags[i] = 0; |
76 | | |
77 | | /* |
78 | | * Try to maximize the space available. If that fails, we use what |
79 | | * we have. |
80 | | */ |
81 | 1.03k | if (buf_size < ERR_MAX_DATA_SIZE |
82 | 1.03k | && (rbuf = OPENSSL_realloc(buf, ERR_MAX_DATA_SIZE)) != NULL) { |
83 | 1.03k | buf = rbuf; |
84 | 1.03k | buf_size = ERR_MAX_DATA_SIZE; |
85 | 1.03k | } |
86 | | |
87 | 1.03k | if (buf != NULL) { |
88 | 1.03k | printed_len = BIO_vsnprintf(buf, buf_size, fmt, args); |
89 | 1.03k | } |
90 | 1.03k | if (printed_len < 0) |
91 | 0 | printed_len = 0; |
92 | 1.03k | if (buf != NULL) |
93 | 1.03k | buf[printed_len] = '\0'; |
94 | | |
95 | | /* |
96 | | * Try to reduce the size, but only if we maximized above. If that |
97 | | * fails, we keep what we have. |
98 | | * (According to documentation, realloc leaves the old buffer untouched |
99 | | * if it fails) |
100 | | */ |
101 | 1.03k | if ((rbuf = OPENSSL_realloc(buf, printed_len + 1)) != NULL) { |
102 | 1.03k | buf = rbuf; |
103 | 1.03k | buf_size = printed_len + 1; |
104 | 1.03k | buf[printed_len] = '\0'; |
105 | 1.03k | } |
106 | | |
107 | 1.03k | if (buf != NULL) |
108 | 1.03k | flags = ERR_TXT_MALLOCED | ERR_TXT_STRING; |
109 | 1.03k | } |
110 | | |
111 | 1.04k | err_clear_data(es, es->top, 0); |
112 | 1.04k | err_set_error(es, es->top, lib, reason); |
113 | 1.04k | if (fmt != NULL) |
114 | 1.03k | err_set_data(es, es->top, buf, buf_size, flags); |
115 | 1.04k | } |