Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/err/err_prn.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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 "internal/cryptlib.h"
12
#include <openssl/crypto.h>
13
#include <openssl/buffer.h>
14
#include <openssl/err.h>
15
#include "err_local.h"
16
17
68.1k
#define ERR_PRINT_BUF_SIZE 4096
18
void ERR_print_errors_cb(int (*cb)(const char *str, size_t len, void *u),
19
    void *u)
20
110k
{
21
110k
    CRYPTO_THREAD_ID tid = CRYPTO_THREAD_get_current_id();
22
110k
    unsigned long l;
23
110k
    const char *file, *data, *func;
24
110k
    int line, flags;
25
26
140k
    while ((l = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
27
29.6k
        char buf[ERR_PRINT_BUF_SIZE] = "";
28
29.6k
        char *hex = NULL;
29
29.6k
        int offset;
30
31
29.6k
        if ((flags & ERR_TXT_STRING) == 0)
32
29.3k
            data = "";
33
34
29.6k
        hex = ossl_buf2hexstr_sep((const unsigned char *)&tid, sizeof(tid), '\0');
35
29.6k
        snprintf(buf, sizeof(buf), "%s:", hex == NULL ? "<null>" : hex);
36
29.6k
        offset = (int)strlen(buf);
37
29.6k
        ossl_err_string_int(l, func, buf + offset, sizeof(buf) - offset);
38
29.6k
        offset += (int)strlen(buf + offset);
39
29.6k
        snprintf(buf + offset, sizeof(buf) - offset, ":%s:%d:%s\n",
40
29.6k
            file, line, data);
41
29.6k
        OPENSSL_free(hex);
42
29.6k
        if (cb(buf, strlen(buf), u) <= 0)
43
0
            break; /* abort outputting the error report */
44
29.6k
    }
45
110k
}
46
47
/* auxiliary function for incrementally reporting texts via the error queue */
48
static void put_error(int lib, const char *func, int reason,
49
    const char *file, int line)
50
0
{
51
0
    ERR_new();
52
0
    ERR_set_debug(file, line, func);
53
0
    ERR_set_error(lib, reason, NULL /* no data here, so fmt is NULL */);
54
0
}
55
56
68.1k
#define TYPICAL_MAX_OUTPUT_BEFORE_DATA 100
57
90.8k
#define MAX_DATA_LEN (ERR_PRINT_BUF_SIZE - TYPICAL_MAX_OUTPUT_BEFORE_DATA)
58
void ERR_add_error_txt(const char *separator, const char *txt)
59
22.7k
{
60
22.7k
    const char *file = NULL;
61
22.7k
    int line;
62
22.7k
    const char *func = NULL;
63
22.7k
    const char *data = NULL;
64
22.7k
    int flags;
65
22.7k
    unsigned long err = ERR_peek_last_error();
66
67
22.7k
    if (separator == NULL)
68
19.2k
        separator = "";
69
22.7k
    if (err == 0)
70
0
        put_error(ERR_LIB_NONE, NULL, 0, "", 0);
71
72
22.7k
    do {
73
22.7k
        size_t available_len, data_len;
74
22.7k
        const char *curr = txt, *next = txt;
75
22.7k
        const char *leading_separator = separator;
76
22.7k
        int trailing_separator = 0;
77
22.7k
        char *tmp;
78
79
22.7k
        ERR_peek_last_error_all(&file, &line, &func, &data, &flags);
80
22.7k
        if ((flags & ERR_TXT_STRING) == 0) {
81
9.08k
            data = "";
82
9.08k
            leading_separator = "";
83
9.08k
        }
84
22.7k
        data_len = strlen(data);
85
86
        /* workaround for limit of ERR_print_errors_cb() */
87
22.7k
        if (data_len >= MAX_DATA_LEN
88
22.7k
            || strlen(separator) >= (size_t)(MAX_DATA_LEN - data_len))
89
0
            available_len = 0;
90
22.7k
        else
91
22.7k
            available_len = MAX_DATA_LEN - data_len - strlen(separator) - 1;
92
        /* MAX_DATA_LEN > available_len >= 0 */
93
94
22.7k
        if (*separator == '\0') {
95
19.2k
            const size_t len_next = strlen(next);
96
97
19.2k
            if (len_next <= available_len) {
98
19.2k
                next += len_next;
99
19.2k
                curr = NULL; /* no need to split */
100
19.2k
            } else {
101
0
                next += available_len;
102
0
                curr = next; /* will split at this point */
103
0
            }
104
19.2k
        } else {
105
21.9k
            while (*next != '\0' && (size_t)(next - txt) <= available_len) {
106
18.5k
                curr = next;
107
18.5k
                next = strstr(curr, separator);
108
18.5k
                if (next != NULL) {
109
15.8k
                    next += strlen(separator);
110
15.8k
                    trailing_separator = *next == '\0';
111
15.8k
                } else {
112
2.66k
                    next = curr + strlen(curr);
113
2.66k
                }
114
18.5k
            }
115
3.47k
            if ((size_t)(next - txt) <= available_len)
116
3.47k
                curr = NULL; /* the above loop implies *next == '\0' */
117
3.47k
        }
118
22.7k
        if (curr != NULL) {
119
            /* split error msg at curr since error data would get too long */
120
0
            if (curr != txt) {
121
0
                tmp = OPENSSL_strndup(txt, curr - txt);
122
0
                if (tmp == NULL)
123
0
                    return;
124
0
                ERR_add_error_data(2, separator, tmp);
125
0
                OPENSSL_free(tmp);
126
0
            }
127
0
            put_error(ERR_GET_LIB(err), func, err, file, line);
128
0
            txt = curr;
129
22.7k
        } else {
130
22.7k
            if (trailing_separator) {
131
801
                tmp = OPENSSL_strndup(txt, next - strlen(separator) - txt);
132
801
                if (tmp == NULL)
133
0
                    return;
134
                /* output txt without the trailing separator */
135
801
                ERR_add_error_data(2, leading_separator, tmp);
136
801
                OPENSSL_free(tmp);
137
21.9k
            } else {
138
21.9k
                ERR_add_error_data(2, leading_separator, txt);
139
21.9k
            }
140
22.7k
            txt = next; /* finished */
141
22.7k
        }
142
22.7k
    } while (*txt != '\0');
143
22.7k
}
144
145
void ERR_add_error_mem_bio(const char *separator, BIO *bio)
146
4.48k
{
147
4.48k
    if (bio != NULL) {
148
4.48k
        char *str;
149
4.48k
        long len = BIO_get_mem_data(bio, &str);
150
151
4.48k
        if (len > 0) {
152
4.48k
            if (str[len - 1] != '\0') {
153
4.48k
                if (BIO_write(bio, "", 1) <= 0)
154
0
                    return;
155
156
4.48k
                len = BIO_get_mem_data(bio, &str);
157
4.48k
            }
158
4.48k
            if (len > 1)
159
4.48k
                ERR_add_error_txt(separator, str);
160
4.48k
        }
161
4.48k
    }
162
4.48k
}
163
164
static int print_bio(const char *str, size_t len, void *bp)
165
29.6k
{
166
29.6k
    return BIO_write((BIO *)bp, str, (int)len);
167
29.6k
}
168
169
void ERR_print_errors(BIO *bp)
170
28.8k
{
171
28.8k
    ERR_print_errors_cb(print_bio, bp);
172
28.8k
}
173
174
#ifndef OPENSSL_NO_STDIO
175
void ERR_print_errors_fp(FILE *fp)
176
81.9k
{
177
81.9k
    BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
178
81.9k
    if (bio == NULL)
179
0
        return;
180
181
81.9k
    ERR_print_errors_cb(print_bio, bio);
182
81.9k
    BIO_free(bio);
183
81.9k
}
184
#endif