Skip to content

Commit 2aa9d8c

Browse files
committed
Provide safe copy assertion in RawStatData::initialize()
Signed-off-by: James Buckland <[email protected]>
1 parent 71f6b24 commit 2aa9d8c

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

source/common/stats/stats_impl.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ bool TagExtractorImpl::extractTag(const std::string& stat_name, std::vector<Tag>
136136
}
137137

138138
RawStatData* HeapRawStatDataAllocator::alloc(const std::string& name) {
139-
RawStatData* data = static_cast<RawStatData*>(::calloc(RawStatData::sizeGivenName(name), 1));
140-
data->initialize(name);
139+
uint64_t num_bytes_to_allocate = RawStatData::sizeGivenName(name);
140+
RawStatData* data = static_cast<RawStatData*>(::calloc(num_bytes_to_allocate, 1));
141+
data->initialize(name, num_bytes_to_allocate);
141142

142143
Thread::ReleasableLockGuard lock(mutex_);
143144
auto ret = stats_.insert(data);
@@ -331,11 +332,13 @@ void HeapRawStatDataAllocator::free(RawStatData& data) {
331332
::free(&data);
332333
}
333334

334-
void RawStatData::initialize(absl::string_view key) {
335+
void RawStatData::initialize(absl::string_view key, uint64_t num_bytes_allocated) {
335336
ASSERT(!initialized());
336337
ref_count_ = 1;
337338

338339
uint64_t xfer_size = key.size();
340+
ASSERT(xfer_size <= num_bytes_allocated);
341+
339342
memcpy(name_, key.data(), xfer_size);
340343
name_[xfer_size] = '\0';
341344
}

source/common/stats/stats_impl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,10 @@ struct RawStatData {
219219
/**
220220
* Initializes this object to have the specified key,
221221
* a refcount of 1, and all other values zero. Required for the HeapRawStatDataAllocator, which
222-
* does not expect stat name truncation.
222+
* does not expect stat name truncation. We pass in the number of bytes allocated in order to
223+
* assert the copy is safe inline.
223224
*/
224-
void initialize(absl::string_view key);
225+
void initialize(absl::string_view key, uint64_t num_bytes_allocated);
225226

226227
/**
227228
* Initializes this object to have the specified key,

0 commit comments

Comments
 (0)