/src/openssl/crypto/bio/bio_print.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2025 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 "internal/cryptlib.h" |
13 | | #include "crypto/ctype.h" |
14 | | #include "internal/numbers.h" |
15 | | #include <openssl/bio.h> |
16 | | #include <openssl/configuration.h> |
17 | | |
18 | | |
19 | | int BIO_printf(BIO *bio, const char *format, ...) |
20 | 0 | { |
21 | 0 | va_list args; |
22 | 0 | int ret; |
23 | |
|
24 | 0 | va_start(args, format); |
25 | |
|
26 | 0 | ret = BIO_vprintf(bio, format, args); |
27 | |
|
28 | 0 | va_end(args); |
29 | 0 | return ret; |
30 | 0 | } |
31 | | |
32 | | #if defined(_WIN32) |
33 | | /* |
34 | | * _MSC_VER described here: |
35 | | * https://learn.microsoft.com/en-us/cpp/overview/compiler-versions?view=msvc-170 |
36 | | * |
37 | | * Beginning with the UCRT in Visual Studio 2015 and Windows 10, snprintf is no |
38 | | * longer identical to _snprintf. The snprintf behavior is now C99 standard |
39 | | * conformant. The difference is that if you run out of buffer, snprintf |
40 | | * null-terminates the end of the buffer and returns the number of characters |
41 | | * that would have been required whereas _snprintf doesn't null-terminate the |
42 | | * buffer and returns -1. Also, snprintf() includes one more character in the |
43 | | * output because it doesn't null-terminate the buffer. |
44 | | * [ https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l?view=msvc-170#remarks |
45 | | * |
46 | | * for older MSVC (older than 2015) we can use _vscprintf() and _vsnprintf() |
47 | | * as suggested here: |
48 | | * https://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 |
49 | | * |
50 | | */ |
51 | | static int msvc_bio_vprintf(BIO *bio, const char *format, va_list args) |
52 | | { |
53 | | char buf[512]; |
54 | | char *abuf; |
55 | | int ret, sz; |
56 | | |
57 | | sz = _vsnprintf_s(buf, sizeof (buf), _TRUNCATE, format, args); |
58 | | if (sz == -1) { |
59 | | sz = _vscprintf(format, args) + 1; |
60 | | abuf = (char *) OPENSSL_malloc(sz); |
61 | | if (abuf == NULL) { |
62 | | ret = -1; |
63 | | } else { |
64 | | sz = _vsnprintf(abuf, sz, format, args); |
65 | | ret = BIO_write(bio, abuf, sz); |
66 | | OPENSSL_free(abuf); |
67 | | } |
68 | | } else { |
69 | | ret = BIO_write(bio, buf, sz); |
70 | | } |
71 | | |
72 | | return ret; |
73 | | } |
74 | | |
75 | | /* |
76 | | * This function is for unit test on windows only when built with Visual Studio |
77 | | */ |
78 | | int ossl_BIO_snprintf_msvc(char *buf, size_t n, const char *format, ...) |
79 | | { |
80 | | va_list args; |
81 | | int ret; |
82 | | |
83 | | va_start(args, format); |
84 | | ret = _vsnprintf_s(buf, n, _TRUNCATE, format, args); |
85 | | va_end(args); |
86 | | |
87 | | return ret; |
88 | | } |
89 | | |
90 | | #endif |
91 | | |
92 | | int BIO_vprintf(BIO *bio, const char *format, va_list args) |
93 | 0 | { |
94 | 0 | va_list cp_args; |
95 | 0 | #if !defined(_MSC_VER) || _MSC_VER > 1900 |
96 | 0 | int sz; |
97 | 0 | #endif |
98 | 0 | int ret = -1; |
99 | |
|
100 | 0 | va_copy(cp_args, args); |
101 | | #if defined(_MSC_VER) && _MSC_VER < 1900 |
102 | | ret = msvc_bio_vprintf(bio, format, cp_args); |
103 | | #else |
104 | 0 | char buf[512]; |
105 | 0 | char *abuf; |
106 | | /* |
107 | | * some compilers modify va_list, hence each call to v*printf() |
108 | | * should operate with its own instance of va_list. The first |
109 | | * call to vsnprintf() here uses args we got in function argument. |
110 | | * The second call is going to use cp_args we made earlier. |
111 | | */ |
112 | 0 | sz = vsnprintf(buf, sizeof (buf), format, args); |
113 | 0 | if (sz >= 0) { |
114 | 0 | if ((size_t)sz > sizeof (buf)) { |
115 | 0 | sz += 1; |
116 | 0 | abuf = (char *) OPENSSL_malloc(sz); |
117 | 0 | if (abuf == NULL) { |
118 | 0 | ret = -1; |
119 | 0 | } else { |
120 | 0 | sz = vsnprintf(abuf, sz, format, cp_args); |
121 | 0 | ret = BIO_write(bio, abuf, sz); |
122 | 0 | OPENSSL_free(abuf); |
123 | 0 | } |
124 | 0 | } else { |
125 | | /* vsnprintf returns length not including nul-terminator */ |
126 | 0 | ret = BIO_write(bio, buf, sz); |
127 | 0 | } |
128 | 0 | } |
129 | 0 | #endif |
130 | 0 | va_end(cp_args); |
131 | 0 | return ret; |
132 | 0 | } |
133 | | |
134 | | /* |
135 | | * For historical reasons BIO_snprintf and friends return a failure for string |
136 | | * truncation (-1) instead of the POSIX requirement of a success with the |
137 | | * number of characters that would have been written. Upon seeing -1 on |
138 | | * return, the caller must treat output buf as unsafe (as a buf with missing |
139 | | * nul terminator). |
140 | | */ |
141 | | int BIO_snprintf(char *buf, size_t n, const char *format, ...) |
142 | 22.1k | { |
143 | 22.1k | va_list args; |
144 | 22.1k | int ret; |
145 | | |
146 | 22.1k | va_start(args, format); |
147 | | |
148 | | #if defined(_MSC_VER) && _MSC_VER < 1900 |
149 | | ret = _vsnprintf_s(buf, n, _TRUNCATE, format, args); |
150 | | #else |
151 | 22.1k | ret = vsnprintf(buf, n, format, args); |
152 | 22.1k | if ((size_t)ret >= n) |
153 | 0 | ret = -1; |
154 | 22.1k | #endif |
155 | 22.1k | va_end(args); |
156 | | |
157 | 22.1k | return ret; |
158 | 22.1k | } |
159 | | |
160 | | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) |
161 | 16 | { |
162 | 16 | int ret; |
163 | | |
164 | | #if defined(_MSC_VER) && _MSC_VER < 1900 |
165 | | ret = _vsnprintf_s(buf, n, _TRUNCATE, format, args); |
166 | | #else |
167 | 16 | ret = vsnprintf(buf, n, format, args); |
168 | 16 | if ((size_t)ret >= n) |
169 | 0 | ret = -1; |
170 | 16 | #endif |
171 | 16 | return ret; |
172 | 16 | } |