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