Coverage Report

Created: 2026-08-31 06:56

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