Coverage Report

Created: 2025-11-03 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/bio/printf.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/bio.h>
16
17
#include <assert.h>
18
#include <stdarg.h>
19
#include <stdio.h>
20
21
#include <openssl/err.h>
22
#include <openssl/mem.h>
23
24
465k
int BIO_printf(BIO *bio, const char *format, ...) {
25
465k
  va_list args;
26
465k
  char buf[256], *out, out_malloced = 0;
27
465k
  int out_len, ret;
28
29
465k
  va_start(args, format);
30
465k
  out_len = vsnprintf(buf, sizeof(buf), format, args);
31
465k
  va_end(args);
32
465k
  if (out_len < 0) {
33
0
    return -1;
34
0
  }
35
36
465k
  if ((size_t)out_len >= sizeof(buf)) {
37
87
    const size_t requested_len = (size_t)out_len;
38
    // The output was truncated. Note that vsnprintf's return value does not
39
    // include a trailing NUL, but the buffer must be sized for it.
40
87
    out = reinterpret_cast<char *>(OPENSSL_malloc(requested_len + 1));
41
87
    out_malloced = 1;
42
87
    if (out == nullptr) {
43
0
      return -1;
44
0
    }
45
87
    va_start(args, format);
46
87
    out_len = vsnprintf(out, requested_len + 1, format, args);
47
87
    va_end(args);
48
87
    assert(out_len == (int)requested_len);
49
465k
  } else {
50
465k
    out = buf;
51
465k
  }
52
53
465k
  ret = BIO_write(bio, out, out_len);
54
465k
  if (out_malloced) {
55
87
    OPENSSL_free(out);
56
87
  }
57
58
465k
  return ret;
59
465k
}