Coverage Report

Created: 2026-09-12 06:55

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
54.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
102k
{
21
102k
    CRYPTO_THREAD_ID tid = CRYPTO_THREAD_get_current_id();
22
102k
    unsigned long l;
23
102k
    const char *file, *data, *func;
24
102k
    int line, flags;
25
26
126k
    while ((l = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
27
23.9k
        char buf[ERR_PRINT_BUF_SIZE] = "";
28
23.9k
        char *hex = NULL;
29
23.9k
        int offset;
30
31
23.9k
        if ((flags & ERR_TXT_STRING) == 0)
32
23.8k
            data = "";
33
34
23.9k
        hex = ossl_buf2hexstr_sep((const unsigned char *)&tid, sizeof(tid), '\0');
35
23.9k
        snprintf(buf, sizeof(buf), "%s:", hex == NULL ? "<null>" : hex);
36
23.9k
        offset = (int)strlen(buf);
37
23.9k
        ossl_err_string_int(l, func, buf + offset, sizeof(buf) - offset);
38
23.9k
        offset += (int)strlen(buf + offset);
39
23.9k
        snprintf(buf + offset, sizeof(buf) - offset, ":%s:%d:%s\n",
40
23.9k
            file, line, data);
41
23.9k
        OPENSSL_free(hex);
42
23.9k
        if (cb(buf, strlen(buf), u) <= 0)
43
0
            break; /* abort outputting the error report */
44
23.9k
    }
45
102k
}
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
54.1k
#define TYPICAL_MAX_OUTPUT_BEFORE_DATA 100
57
72.1k
#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
18.0k
{
60
18.0k
    const char *file = NULL;
61
18.0k
    int line;
62
18.0k
    const char *func = NULL;
63
18.0k
    const char *data = NULL;
64
18.0k
    int flags;
65
18.0k
    unsigned long err = ERR_peek_last_error();
66
67
18.0k
    if (separator == NULL)
68
15.1k
        separator = "";
69
18.0k
    if (err == 0)
70
0
        put_error(ERR_LIB_NONE, NULL, 0, "", 0);
71
72
18.0k
    do {
73
18.0k
        size_t available_len, data_len;
74
18.0k
        const char *curr = txt, *next = txt;
75
18.0k
        const char *leading_separator = separator;
76
18.0k
        int trailing_separator = 0;
77
18.0k
        char *tmp;
78
79
18.0k
        ERR_peek_last_error_all(&file, &line, &func, &data, &flags);
80
18.0k
        if ((flags & ERR_TXT_STRING) == 0) {
81
7.27k
            data = "";
82
7.27k
            leading_separator = "";
83
7.27k
        }
84
18.0k
        data_len = strlen(data);
85
86
        /* workaround for limit of ERR_print_errors_cb() */
87
18.0k
        if (data_len >= MAX_DATA_LEN
88
18.0k
            || strlen(separator) >= (size_t)(MAX_DATA_LEN - data_len))
89
0
            available_len = 0;
90
18.0k
        else
91
18.0k
            available_len = MAX_DATA_LEN - data_len - strlen(separator) - 1;
92
        /* MAX_DATA_LEN > available_len >= 0 */
93
94
18.0k
        if (*separator == '\0') {
95
15.1k
            const size_t len_next = strlen(next);
96
97
15.1k
            if (len_next <= available_len) {
98
15.1k
                next += len_next;
99
15.1k
                curr = NULL; /* no need to split */
100
15.1k
            } else {
101
0
                next += available_len;
102
0
                curr = next; /* will split at this point */
103
0
            }
104
15.1k
        } else {
105
20.4k
            while (*next != '\0' && (size_t)(next - txt) <= available_len) {
106
17.4k
                curr = next;
107
17.4k
                next = strstr(curr, separator);
108
17.4k
                if (next != NULL) {
109
15.3k
                    next += strlen(separator);
110
15.3k
                    trailing_separator = *next == '\0';
111
15.3k
                } else {
112
2.14k
                    next = curr + strlen(curr);
113
2.14k
                }
114
17.4k
            }
115
2.91k
            if ((size_t)(next - txt) <= available_len)
116
2.91k
                curr = NULL; /* the above loop implies *next == '\0' */
117
2.91k
        }
118
18.0k
        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
18.0k
        } else {
130
18.0k
            if (trailing_separator) {
131
777
                tmp = OPENSSL_strndup(txt, next - strlen(separator) - txt);
132
777
                if (tmp == NULL)
133
0
                    return;
134
                /* output txt without the trailing separator */
135
777
                ERR_add_error_data(2, leading_separator, tmp);
136
777
                OPENSSL_free(tmp);
137
17.2k
            } else {
138
17.2k
                ERR_add_error_data(2, leading_separator, txt);
139
17.2k
            }
140
18.0k
            txt = next; /* finished */
141
18.0k
        }
142
18.0k
    } while (*txt != '\0');
143
18.0k
}
144
145
void ERR_add_error_mem_bio(const char *separator, BIO *bio)
146
3.60k
{
147
3.60k
    if (bio != NULL) {
148
3.60k
        char *str;
149
3.60k
        long len = BIO_get_mem_data(bio, &str);
150
151
3.60k
        if (len > 0) {
152
3.60k
            if (str[len - 1] != '\0') {
153
3.60k
                if (BIO_write(bio, "", 1) <= 0)
154
0
                    return;
155
156
3.60k
                len = BIO_get_mem_data(bio, &str);
157
3.60k
            }
158
3.60k
            if (len > 1)
159
3.60k
                ERR_add_error_txt(separator, str);
160
3.60k
        }
161
3.60k
    }
162
3.60k
}
163
164
static int print_bio(const char *str, size_t len, void *bp)
165
23.9k
{
166
23.9k
    return BIO_write((BIO *)bp, str, (int)len);
167
23.9k
}
168
169
void ERR_print_errors(BIO *bp)
170
23.3k
{
171
23.3k
    ERR_print_errors_cb(print_bio, bp);
172
23.3k
}
173
174
#ifndef OPENSSL_NO_STDIO
175
void ERR_print_errors_fp(FILE *fp)
176
79.3k
{
177
79.3k
    BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
178
79.3k
    if (bio == NULL)
179
0
        return;
180
181
79.3k
    ERR_print_errors_cb(print_bio, bio);
182
79.3k
    BIO_free(bio);
183
79.3k
}
184
#endif