Fix grammar in certificates.txt
[openssl.git] / crypto / err / err_blocks.c
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
10 #include <string.h>
11 #include <openssl/err.h>
12 #include "err_locl.h"
13
14 void ERR_new(void)
15 {
16     ERR_STATE *es;
17
18     es = ERR_get_state();
19     if (es == NULL)
20         return;
21
22     /* Allocate a slot */
23     err_get_slot(es);
24     err_clear(es, es->top, 0);
25 }
26
27 void ERR_set_debug(const char *file, int line, const char *func)
28 {
29     ERR_STATE *es;
30
31     es = ERR_get_state();
32     if (es == NULL)
33         return;
34
35     err_set_debug(es, es->top, file, line, func);
36 }
37
38 void ERR_set_error(int lib, int reason, const char *fmt, ...)
39 {
40     va_list args;
41
42     va_start(args, fmt);
43     ERR_vset_error(lib, reason, fmt, args);
44     va_end(args);
45 }
46
47 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
48 {
49     ERR_STATE *es;
50     char *buf = NULL;
51     size_t buf_size = 0;
52     unsigned long flags = 0;
53     size_t i;
54
55     es = ERR_get_state();
56     if (es == NULL)
57         return;
58     i = es->top;
59
60     if (fmt != NULL) {
61         int printed_len = 0;
62         char *rbuf = NULL;
63
64         buf = es->err_data[i];
65         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         es->err_data[i] = NULL;
75         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         if (buf_size < ERR_MAX_DATA_SIZE
82             && (rbuf = OPENSSL_realloc(buf, ERR_MAX_DATA_SIZE)) != NULL) {
83             buf = rbuf;
84             buf_size = ERR_MAX_DATA_SIZE;
85         }
86
87         if (buf != NULL) {
88             printed_len = BIO_vsnprintf(buf, buf_size, fmt, args);
89         }
90         if (printed_len < 0)
91             printed_len = 0;
92         buf[printed_len] = '\0';
93
94         /*
95          * Try to reduce the size, but only if we maximized above.  If that
96          * fails, we keep what we have.
97          * (According to documentation, realloc leaves the old buffer untouched
98          * if it fails)
99          */
100         if ((rbuf = OPENSSL_realloc(buf, printed_len + 1)) != NULL) {
101             buf = rbuf;
102             buf_size = printed_len + 1;
103         }
104
105         if (buf != NULL)
106             flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
107     }
108
109     err_clear_data(es, es->top, 0);
110     err_set_error(es, es->top, lib, reason);
111     if (fmt != NULL)
112         err_set_data(es, es->top, buf, buf_size, flags);
113 }