Coverage Report

Created: 2026-06-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/buf/buf.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/buf.h>
16
17
#include <string.h>
18
19
#include <openssl/err.h>
20
#include <openssl/mem.h>
21
22
#include "../internal.h"
23
#include "../mem_internal.h"
24
25
26
using namespace bssl;
27
28
481k
BUF_MEM *BUF_MEM_new() { return New<BUF_MEM>(); }
29
30
474k
void BUF_MEM_free(BUF_MEM *buf) {
31
474k
  if (buf == nullptr) {
32
0
    return;
33
0
  }
34
474k
  OPENSSL_free(buf->data);
35
474k
  Delete(buf);
36
474k
}
37
38
2.54M
int BUF_MEM_reserve(BUF_MEM *buf, size_t cap) {
39
2.54M
  if (buf->max >= cap) {
40
1.90M
    return 1;
41
1.90M
  }
42
43
639k
  size_t n = cap + 3;
44
639k
  if (n < cap) {
45
0
    OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
46
0
    return 0;
47
0
  }
48
639k
  n = n / 3;
49
639k
  size_t alloc_size = n * 4;
50
639k
  if (alloc_size / 4 != n) {
51
0
    OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
52
0
    return 0;
53
0
  }
54
55
639k
  char *new_buf =
56
639k
      reinterpret_cast<char *>(OPENSSL_realloc(buf->data, alloc_size));
57
639k
  if (new_buf == nullptr) {
58
0
    return 0;
59
0
  }
60
61
639k
  buf->data = new_buf;
62
639k
  buf->max = alloc_size;
63
639k
  return 1;
64
639k
}
65
66
290k
size_t BUF_MEM_grow(BUF_MEM *buf, size_t len) {
67
290k
  if (!BUF_MEM_reserve(buf, len)) {
68
0
    return 0;
69
0
  }
70
290k
  if (buf->length < len) {
71
284k
    OPENSSL_memset(&buf->data[buf->length], 0, len - buf->length);
72
284k
  }
73
290k
  buf->length = len;
74
290k
  return len;
75
290k
}
76
77
0
size_t BUF_MEM_grow_clean(BUF_MEM *buf, size_t len) {
78
0
  return BUF_MEM_grow(buf, len);
79
0
}
80
81
2.06M
int BUF_MEM_append(BUF_MEM *buf, const void *in, size_t len) {
82
  // Work around a C language bug. See https://crbug.com/1019588.
83
2.06M
  if (len == 0) {
84
10.0k
    return 1;
85
10.0k
  }
86
2.05M
  size_t new_len = buf->length + len;
87
2.05M
  if (new_len < len) {
88
0
    OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
89
0
    return 0;
90
0
  }
91
2.05M
  if (!BUF_MEM_reserve(buf, new_len)) {
92
0
    return 0;
93
0
  }
94
2.05M
  OPENSSL_memcpy(buf->data + buf->length, in, len);
95
2.05M
  buf->length = new_len;
96
2.05M
  return 1;
97
2.05M
}
98
99
0
char *BUF_strdup(const char *str) { return OPENSSL_strdup(str); }
100
101
0
size_t BUF_strnlen(const char *str, size_t max_len) {
102
0
  return OPENSSL_strnlen(str, max_len);
103
0
}
104
105
0
char *BUF_strndup(const char *str, size_t size) {
106
0
  return OPENSSL_strndup(str, size);
107
0
}
108
109
0
size_t BUF_strlcpy(char *dst, const char *src, size_t dst_size) {
110
0
  return OPENSSL_strlcpy(dst, src, dst_size);
111
0
}
112
113
0
size_t BUF_strlcat(char *dst, const char *src, size_t dst_size) {
114
0
  return OPENSSL_strlcat(dst, src, dst_size);
115
0
}
116
117
0
void *BUF_memdup(const void *data, size_t size) {
118
0
  return OPENSSL_memdup(data, size);
119
0
}