1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "net/base/io_buffer.h"
11
12 #include <utility>
13
14 #include "base/check_op.h"
15 #include "base/containers/heap_array.h"
16 #include "base/numerics/safe_math.h"
17
18 namespace net {
19
20 // TODO(eroman): IOBuffer is being converted to require buffer sizes and offsets
21 // be specified as "size_t" rather than "int" (crbug.com/488553). To facilitate
22 // this move (since LOTS of code needs to be updated), this function ensures
23 // that sizes can be safely converted to an "int" without truncation. The
24 // assert ensures calling this with an "int" argument is also safe.
AssertValidBufferSize(size_t size)25 void IOBuffer::AssertValidBufferSize(size_t size) {
26 static_assert(sizeof(size_t) >= sizeof(int));
27 base::CheckedNumeric<int>(size).ValueOrDie();
28 }
29
30 IOBuffer::IOBuffer() = default;
31
IOBuffer(base::span<char> data)32 IOBuffer::IOBuffer(base::span<char> data)
33 : data_(data.data()), size_(data.size()) {
34 AssertValidBufferSize(size_);
35 }
36
IOBuffer(base::span<uint8_t> data)37 IOBuffer::IOBuffer(base::span<uint8_t> data)
38 : IOBuffer(base::as_writable_chars(data)) {}
39
40 IOBuffer::~IOBuffer() = default;
41
42 IOBufferWithSize::IOBufferWithSize() = default;
43
IOBufferWithSize(size_t buffer_size)44 IOBufferWithSize::IOBufferWithSize(size_t buffer_size) {
45 AssertValidBufferSize(buffer_size);
46 storage_ = base::HeapArray<char>::Uninit(buffer_size);
47 size_ = storage_.size();
48 data_ = storage_.data();
49 }
50
~IOBufferWithSize()51 IOBufferWithSize::~IOBufferWithSize() {
52 // Clear pointer before this destructor makes it dangle.
53 data_ = nullptr;
54 }
55
StringIOBuffer(std::string s)56 StringIOBuffer::StringIOBuffer(std::string s) : string_data_(std::move(s)) {
57 // Can't pass `s.data()` directly to IOBuffer constructor since moving
58 // from `s` may invalidate it. This is especially true for libc++ short
59 // string optimization where the data may be held in the string variable
60 // itself, instead of in a movable backing store.
61 AssertValidBufferSize(string_data_.size());
62 data_ = string_data_.data();
63 size_ = string_data_.size();
64 }
65
~StringIOBuffer()66 StringIOBuffer::~StringIOBuffer() {
67 // Clear pointer before this destructor makes it dangle.
68 data_ = nullptr;
69 }
70
DrainableIOBuffer(scoped_refptr<IOBuffer> base,size_t size)71 DrainableIOBuffer::DrainableIOBuffer(scoped_refptr<IOBuffer> base, size_t size)
72 : IOBuffer(base->span().first(size)), base_(std::move(base)) {}
73
DidConsume(int bytes)74 void DrainableIOBuffer::DidConsume(int bytes) {
75 SetOffset(used_ + bytes);
76 }
77
BytesRemaining() const78 int DrainableIOBuffer::BytesRemaining() const {
79 return size_ - used_;
80 }
81
82 // Returns the number of consumed bytes.
BytesConsumed() const83 int DrainableIOBuffer::BytesConsumed() const {
84 return used_;
85 }
86
SetOffset(int bytes)87 void DrainableIOBuffer::SetOffset(int bytes) {
88 CHECK_GE(bytes, 0);
89 CHECK_LE(bytes, size_);
90 used_ = bytes;
91 data_ = base_->data() + used_;
92 }
93
~DrainableIOBuffer()94 DrainableIOBuffer::~DrainableIOBuffer() {
95 // Clear ptr before this destructor destroys the |base_| instance,
96 // making it dangle.
97 data_ = nullptr;
98 }
99
100 GrowableIOBuffer::GrowableIOBuffer() = default;
101
SetCapacity(int capacity)102 void GrowableIOBuffer::SetCapacity(int capacity) {
103 CHECK_GE(capacity, 0);
104 // this will get reset in `set_offset`.
105 data_ = nullptr;
106 size_ = 0;
107
108 // realloc will crash if it fails.
109 real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity)));
110
111 capacity_ = capacity;
112 if (offset_ > capacity)
113 set_offset(capacity);
114 else
115 set_offset(offset_); // The pointer may have changed.
116 }
117
set_offset(int offset)118 void GrowableIOBuffer::set_offset(int offset) {
119 CHECK_GE(offset, 0);
120 CHECK_LE(offset, capacity_);
121 offset_ = offset;
122 data_ = real_data_.get() + offset;
123 size_ = capacity_ - offset;
124 }
125
RemainingCapacity()126 int GrowableIOBuffer::RemainingCapacity() {
127 return capacity_ - offset_;
128 }
129
everything()130 base::span<uint8_t> GrowableIOBuffer::everything() {
131 return base::as_writable_bytes(
132 // SAFETY: The capacity_ is the size of the allocation.
133 UNSAFE_BUFFERS(
134 base::span(real_data_.get(), base::checked_cast<size_t>(capacity_))));
135 }
136
everything() const137 base::span<const uint8_t> GrowableIOBuffer::everything() const {
138 return base::as_bytes(
139 // SAFETY: The capacity_ is the size of the allocation.
140 UNSAFE_BUFFERS(
141 base::span(real_data_.get(), base::checked_cast<size_t>(capacity_))));
142 }
143
span_before_offset()144 base::span<uint8_t> GrowableIOBuffer::span_before_offset() {
145 return everything().first(base::checked_cast<size_t>(offset_));
146 }
147
span_before_offset() const148 base::span<const uint8_t> GrowableIOBuffer::span_before_offset() const {
149 return everything().first(base::checked_cast<size_t>(offset_));
150 }
151
~GrowableIOBuffer()152 GrowableIOBuffer::~GrowableIOBuffer() {
153 data_ = nullptr;
154 }
155
156 PickledIOBuffer::PickledIOBuffer() = default;
157
Done()158 void PickledIOBuffer::Done() {
159 data_ = const_cast<char*>(pickle_.data_as_char());
160 size_ = pickle_.size();
161 }
162
~PickledIOBuffer()163 PickledIOBuffer::~PickledIOBuffer() {
164 // Avoid dangling ptr when this destructor destroys the pickle.
165 data_ = nullptr;
166 }
167
WrappedIOBuffer(base::span<const char> data)168 WrappedIOBuffer::WrappedIOBuffer(base::span<const char> data)
169 : IOBuffer(base::span(const_cast<char*>(data.data()), data.size())) {}
170
WrappedIOBuffer(base::span<const uint8_t> data)171 WrappedIOBuffer::WrappedIOBuffer(base::span<const uint8_t> data)
172 : WrappedIOBuffer(base::as_chars(data)) {}
173
174 WrappedIOBuffer::~WrappedIOBuffer() = default;
175
176 } // namespace net
177