Coverage Report

Created: 2026-06-08 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/err/err_blocks.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 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
972
{
16
972
    ERR_STATE *es;
17
18
972
    es = ossl_err_get_state_int(1);
19
972
    if (es == NULL)
20
0
        return;
21
22
    /* Allocate a slot */
23
972
    err_get_slot(es);
24
972
    err_clear(es, es->top, 0);
25
972
}
26
27
void ERR_set_debug(const char *file, int line, const char *func)
28
972
{
29
972
    ERR_STATE *es;
30
31
972
    es = ossl_err_get_state_int(1);
32
972
    if (es == NULL)
33
0
        return;
34
35
972
    err_set_debug(es, es->top, file, line, func);
36
972
}
37
38
void ERR_set_error(int lib, int reason, const char *fmt, ...)
39
972
{
40
972
    va_list args;
41
42
972
    va_start(args, fmt);
43
972
    ERR_vset_error(lib, reason, fmt, args);
44
972
    va_end(args);
45
972
}
46
47
void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
48
972
{
49
972
    ERR_STATE *es;
50
972
    char *buf = NULL;
51
972
    size_t buf_size = 0;
52
972
    unsigned long flags = 0;
53
972
    size_t i;
54
55
972
    es = ossl_err_get_state_int(1);
56
972
    if (es == NULL)
57
0
        return;
58
972
    i = es->top;
59
60
972
    if (fmt != NULL) {
61
970
        int printed_len = 0;
62
970
        char *rbuf = NULL;
63
64
970
        buf = es->err_data[i];
65
970
        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
970
        es->err_data[i] = NULL;
75
970
        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
970
        if (buf_size < ERR_MAX_DATA_SIZE
82
970
            && (rbuf = OPENSSL_realloc(buf, ERR_MAX_DATA_SIZE)) != NULL) {
83
970
            buf = rbuf;
84
970
            buf_size = ERR_MAX_DATA_SIZE;
85
970
        }
86
87
970
        if (buf != NULL) {
88
970
            printed_len = BIO_vsnprintf(buf, buf_size, fmt, args);
89
970
        }
90
970
        if (printed_len < 0)
91
0
            printed_len = 0;
92
970
        if (buf != NULL)
93
970
            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
970
        if ((rbuf = OPENSSL_realloc(buf, printed_len + 1)) != NULL) {
102
970
            buf = rbuf;
103
970
            buf_size = printed_len + 1;
104
970
            buf[printed_len] = '\0';
105
970
        }
106
107
970
        if (buf != NULL)
108
970
            flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
109
970
    }
110
111
972
    err_clear_data(es, es->top, 0);
112
972
    err_set_error(es, es->top, lib, reason);
113
972
    if (fmt != NULL)
114
970
        err_set_data(es, es->top, buf, buf_size, flags);
115
972
}