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