Coverage Report

Created: 2026-07-16 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/log/internal/proto.cc
Line
Count
Source
1
// Copyright 2020 The Abseil Authors.
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
// All of the data that passes through this code is trusted because it flows
16
// through a closed loop within the absl::LogMessage object. It is not a robust
17
// protocol buffer encoder or decoder.
18
//
19
// Encoding: When `LOG(INFO) << "foo"` is called, the library uses the Encode*
20
// functions to build a protocol buffer in a private, fixed-size internal buffer
21
// (`LogMessageData::encoded_buf`).
22
//
23
// Decoding: During the same logging call, `LogMessage::Flush()` calls
24
// `FinalizeEncodingAndFormat()`, which uses `ProtoField::DecodeFrom` to parse
25
// that same internal buffer to generate the human-readable string for
26
// text-based log sinks.
27
28
#include "absl/log/internal/proto.h"
29
30
#include <algorithm>
31
#include <cassert>
32
#include <cstddef>
33
#include <cstdint>
34
#include <cstring>
35
36
#include "absl/base/config.h"
37
#include "absl/types/span.h"
38
39
namespace absl {
40
ABSL_NAMESPACE_BEGIN
41
namespace log_internal {
42
namespace {
43
0
void EncodeRawVarint(uint64_t value, size_t size, absl::Span<char> *buf) {
44
0
  for (size_t s = 0; s < size; s++) {
45
0
    (*buf)[s] = static_cast<char>((value & 0x7f) | (s + 1 == size ? 0 : 0x80));
46
0
    value >>= 7;
47
0
  }
48
0
  buf->remove_prefix(size);
49
0
}
50
}  // namespace
51
52
0
bool EncodeVarint(uint64_t tag, uint64_t value, absl::Span<char> *buf) {
53
0
  const uint64_t tag_type = MakeTagType(tag, WireType::kVarint);
54
0
  const size_t tag_type_size = VarintSize(tag_type);
55
0
  const size_t value_size = VarintSize(value);
56
0
  if (tag_type_size + value_size > buf->size()) {
57
0
    buf->remove_suffix(buf->size());
58
0
    return false;
59
0
  }
60
0
  EncodeRawVarint(tag_type, tag_type_size, buf);
61
0
  EncodeRawVarint(value, value_size, buf);
62
0
  return true;
63
0
}
64
65
0
bool Encode64Bit(uint64_t tag, uint64_t value, absl::Span<char> *buf) {
66
0
  const uint64_t tag_type = MakeTagType(tag, WireType::k64Bit);
67
0
  const size_t tag_type_size = VarintSize(tag_type);
68
0
  if (tag_type_size + sizeof(value) > buf->size()) {
69
0
    buf->remove_suffix(buf->size());
70
0
    return false;
71
0
  }
72
0
  EncodeRawVarint(tag_type, tag_type_size, buf);
73
0
  for (size_t s = 0; s < sizeof(value); s++) {
74
0
    (*buf)[s] = static_cast<char>(value & 0xff);
75
0
    value >>= 8;
76
0
  }
77
0
  buf->remove_prefix(sizeof(value));
78
0
  return true;
79
0
}
80
81
0
bool Encode32Bit(uint64_t tag, uint32_t value, absl::Span<char> *buf) {
82
0
  const uint64_t tag_type = MakeTagType(tag, WireType::k32Bit);
83
0
  const size_t tag_type_size = VarintSize(tag_type);
84
0
  if (tag_type_size + sizeof(value) > buf->size()) {
85
0
    buf->remove_suffix(buf->size());
86
0
    return false;
87
0
  }
88
0
  EncodeRawVarint(tag_type, tag_type_size, buf);
89
0
  for (size_t s = 0; s < sizeof(value); s++) {
90
0
    (*buf)[s] = static_cast<char>(value & 0xff);
91
0
    value >>= 8;
92
0
  }
93
0
  buf->remove_prefix(sizeof(value));
94
0
  return true;
95
0
}
96
97
bool EncodeBytes(uint64_t tag, absl::Span<const char> value,
98
0
                 absl::Span<char> *buf) {
99
0
  const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited);
100
0
  const size_t tag_type_size = VarintSize(tag_type);
101
0
  uint64_t length = value.size();
102
0
  const size_t length_size = VarintSize(length);
103
0
  if (tag_type_size + length_size + value.size() > buf->size()) {
104
0
    buf->remove_suffix(buf->size());
105
0
    return false;
106
0
  }
107
0
  EncodeRawVarint(tag_type, tag_type_size, buf);
108
0
  EncodeRawVarint(length, length_size, buf);
109
0
  memcpy(buf->data(), value.data(), value.size());
110
0
  buf->remove_prefix(value.size());
111
0
  return true;
112
0
}
113
114
bool EncodeBytesTruncate(uint64_t tag, absl::Span<const char> value,
115
0
                         absl::Span<char> *buf) {
116
0
  const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited);
117
0
  const size_t tag_type_size = VarintSize(tag_type);
118
0
  uint64_t length = value.size();
119
0
  const size_t length_size =
120
0
      VarintSize(std::min<uint64_t>(length, buf->size()));
121
0
  if (tag_type_size + length_size <= buf->size() &&
122
0
      tag_type_size + length_size + value.size() > buf->size()) {
123
0
    value.remove_suffix(tag_type_size + length_size + value.size() -
124
0
                        buf->size());
125
0
    length = value.size();
126
0
  }
127
0
  if (tag_type_size + length_size + value.size() > buf->size()) {
128
0
    buf->remove_suffix(buf->size());
129
0
    return false;
130
0
  }
131
0
  EncodeRawVarint(tag_type, tag_type_size, buf);
132
0
  EncodeRawVarint(length, length_size, buf);
133
0
  memcpy(buf->data(), value.data(), value.size());
134
0
  buf->remove_prefix(value.size());
135
0
  return true;
136
0
}
137
138
[[nodiscard]] absl::Span<char> EncodeMessageStart(uint64_t tag,
139
                                                  uint64_t max_size,
140
0
                                                  absl::Span<char> *buf) {
141
0
  const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited);
142
0
  const size_t tag_type_size = VarintSize(tag_type);
143
0
  max_size = std::min<uint64_t>(max_size, buf->size());
144
0
  const size_t length_size = VarintSize(max_size);
145
0
  if (tag_type_size + length_size > buf->size()) {
146
0
    buf->remove_suffix(buf->size());
147
0
    return absl::Span<char>();
148
0
  }
149
0
  EncodeRawVarint(tag_type, tag_type_size, buf);
150
0
  const absl::Span<char> ret = buf->subspan(0, length_size);
151
0
  EncodeRawVarint(0, length_size, buf);
152
0
  return ret;
153
0
}
154
155
0
void EncodeMessageLength(absl::Span<char> msg, const absl::Span<char> *buf) {
156
0
  if (!msg.data()) return;
157
0
  assert(buf->data() >= msg.data());
158
0
  if (buf->data() < msg.data()) return;
159
0
  EncodeRawVarint(
160
0
      static_cast<uint64_t>(buf->data() - (msg.data() + msg.size())),
161
0
      msg.size(), &msg);
162
0
}
163
164
namespace {
165
0
uint64_t DecodeVarint(absl::Span<const char> *buf) {
166
0
  uint64_t value = 0;
167
0
  size_t s = 0;
168
0
  while (s < buf->size()) {
169
0
    value |= static_cast<uint64_t>(static_cast<unsigned char>((*buf)[s]) & 0x7f)
170
0
             << 7 * s;
171
0
    if (!((*buf)[s++] & 0x80)) break;
172
0
  }
173
0
  buf->remove_prefix(s);
174
0
  return value;
175
0
}
176
177
0
uint64_t Decode64Bit(absl::Span<const char> *buf) {
178
0
  uint64_t value = 0;
179
0
  size_t s = 0;
180
0
  while (s < buf->size()) {
181
0
    value |= static_cast<uint64_t>(static_cast<unsigned char>((*buf)[s]))
182
0
             << 8 * s;
183
0
    if (++s == sizeof(value)) break;
184
0
  }
185
0
  buf->remove_prefix(s);
186
0
  return value;
187
0
}
188
189
0
uint32_t Decode32Bit(absl::Span<const char> *buf) {
190
0
  uint32_t value = 0;
191
0
  size_t s = 0;
192
0
  while (s < buf->size()) {
193
0
    value |= static_cast<uint32_t>(static_cast<unsigned char>((*buf)[s]))
194
0
             << 8 * s;
195
0
    if (++s == sizeof(value)) break;
196
0
  }
197
0
  buf->remove_prefix(s);
198
0
  return value;
199
0
}
200
}  // namespace
201
202
0
bool ProtoField::DecodeFrom(absl::Span<const char> *data) {
203
0
  if (data->empty()) return false;
204
0
  const uint64_t tag_type = DecodeVarint(data);
205
0
  tag_ = tag_type >> 3;
206
0
  type_ = static_cast<WireType>(tag_type & 0x07);
207
0
  switch (type_) {
208
0
    case WireType::kVarint:
209
0
      value_ = DecodeVarint(data);
210
0
      break;
211
0
    case WireType::k64Bit:
212
0
      value_ = Decode64Bit(data);
213
0
      break;
214
0
    case WireType::kLengthDelimited: {
215
0
      value_ = DecodeVarint(data);
216
0
      data_ = data->subspan(
217
0
          0, static_cast<size_t>(std::min<uint64_t>(value_, data->size())));
218
0
      data->remove_prefix(data_.size());
219
0
      break;
220
0
    }
221
0
    case WireType::k32Bit:
222
0
      value_ = Decode32Bit(data);
223
0
      break;
224
0
  }
225
0
  return true;
226
0
}
227
228
}  // namespace log_internal
229
ABSL_NAMESPACE_END
230
}  // namespace absl