Coverage Report

Created: 2026-07-12 07:21

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