]>
Commit | Line | Data |
---|---|---|
7c0e20dc RL |
1 | /* |
2 | * Copyright 2019 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 | ||
14e275e8 RL |
10 | /* TODO: When ERR_STATE becomes opaque, this musts be removed */ |
11 | #define OSSL_FORCE_ERR_STATE | |
12 | ||
7c0e20dc RL |
13 | #include <string.h> |
14 | #include <openssl/err.h> | |
706457b7 | 15 | #include "err_local.h" |
7c0e20dc RL |
16 | |
17 | void ERR_new(void) | |
18 | { | |
19 | ERR_STATE *es; | |
20 | ||
78715dcc | 21 | es = ossl_err_get_state_int(); |
7c0e20dc RL |
22 | if (es == NULL) |
23 | return; | |
24 | ||
25 | /* Allocate a slot */ | |
26 | err_get_slot(es); | |
27 | err_clear(es, es->top, 0); | |
28 | } | |
29 | ||
30 | void ERR_set_debug(const char *file, int line, const char *func) | |
31 | { | |
32 | ERR_STATE *es; | |
33 | ||
78715dcc | 34 | es = ossl_err_get_state_int(); |
7c0e20dc RL |
35 | if (es == NULL) |
36 | return; | |
37 | ||
38 | err_set_debug(es, es->top, file, line, func); | |
39 | } | |
40 | ||
41 | void ERR_set_error(int lib, int reason, const char *fmt, ...) | |
42 | { | |
43 | va_list args; | |
44 | ||
45 | va_start(args, fmt); | |
46 | ERR_vset_error(lib, reason, fmt, args); | |
47 | va_end(args); | |
48 | } | |
49 | ||
50 | void ERR_vset_error(int lib, int reason, const char *fmt, va_list args) | |
51 | { | |
52 | ERR_STATE *es; | |
53 | char *buf = NULL; | |
54 | size_t buf_size = 0; | |
55 | unsigned long flags = 0; | |
56 | size_t i; | |
57 | ||
78715dcc | 58 | es = ossl_err_get_state_int(); |
7c0e20dc RL |
59 | if (es == NULL) |
60 | return; | |
61 | i = es->top; | |
62 | ||
63 | if (fmt != NULL) { | |
64 | int printed_len = 0; | |
65 | char *rbuf = NULL; | |
66 | ||
67 | buf = es->err_data[i]; | |
68 | buf_size = es->err_data_size[i]; | |
69 | ||
70 | /* | |
71 | * To protect the string we just grabbed from tampering by other | |
72 | * functions we may call, or to protect them from freeing a pointer | |
73 | * that may no longer be valid at that point, we clear away the | |
74 | * data pointer and the flags. We will set them again at the end | |
75 | * of this function. | |
76 | */ | |
77 | es->err_data[i] = NULL; | |
78 | es->err_data_flags[i] = 0; | |
79 | ||
80 | /* | |
81 | * Try to maximize the space available. If that fails, we use what | |
82 | * we have. | |
83 | */ | |
84 | if (buf_size < ERR_MAX_DATA_SIZE | |
85 | && (rbuf = OPENSSL_realloc(buf, ERR_MAX_DATA_SIZE)) != NULL) { | |
86 | buf = rbuf; | |
87 | buf_size = ERR_MAX_DATA_SIZE; | |
88 | } | |
89 | ||
90 | if (buf != NULL) { | |
189dbdd9 | 91 | printed_len = BIO_vsnprintf(buf, buf_size, fmt, args); |
7c0e20dc RL |
92 | } |
93 | if (printed_len < 0) | |
94 | printed_len = 0; | |
a60b5331 P |
95 | if (buf != NULL) |
96 | buf[printed_len] = '\0'; | |
7c0e20dc RL |
97 | |
98 | /* | |
99 | * Try to reduce the size, but only if we maximized above. If that | |
100 | * fails, we keep what we have. | |
101 | * (According to documentation, realloc leaves the old buffer untouched | |
102 | * if it fails) | |
103 | */ | |
104 | if ((rbuf = OPENSSL_realloc(buf, printed_len + 1)) != NULL) { | |
105 | buf = rbuf; | |
106 | buf_size = printed_len + 1; | |
a60b5331 | 107 | buf[printed_len] = '\0'; |
7c0e20dc RL |
108 | } |
109 | ||
110 | if (buf != NULL) | |
111 | flags = ERR_TXT_MALLOCED | ERR_TXT_STRING; | |
112 | } | |
113 | ||
114 | err_clear_data(es, es->top, 0); | |
115 | err_set_error(es, es->top, lib, reason); | |
116 | if (fmt != NULL) | |
117 | err_set_data(es, es->top, buf, buf_size, flags); | |
118 | } |