Skip to content

Commit

Permalink
Provide safe copy assertion in RawStatData::initialize()
Browse files Browse the repository at this point in the history
Signed-off-by: James Buckland <jbuckland@google.com>
  • Loading branch information
ambuc committed Jul 11, 2018
1 parent 71f6b24 commit 2aa9d8c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 6 additions & 3 deletions source/common/stats/stats_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ bool TagExtractorImpl::extractTag(const std::string& stat_name, std::vector<Tag>
}

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

Thread::ReleasableLockGuard lock(mutex_);
auto ret = stats_.insert(data);
Expand Down Expand Up @@ -331,11 +332,13 @@ void HeapRawStatDataAllocator::free(RawStatData& data) {
::free(&data);
}

void RawStatData::initialize(absl::string_view key) {
void RawStatData::initialize(absl::string_view key, uint64_t num_bytes_allocated) {
ASSERT(!initialized());
ref_count_ = 1;

uint64_t xfer_size = key.size();
ASSERT(xfer_size <= num_bytes_allocated);

memcpy(name_, key.data(), xfer_size);
name_[xfer_size] = '\0';
}
Expand Down
5 changes: 3 additions & 2 deletions source/common/stats/stats_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ struct RawStatData {
/**
* Initializes this object to have the specified key,
* a refcount of 1, and all other values zero. Required for the HeapRawStatDataAllocator, which
* does not expect stat name truncation.
* does not expect stat name truncation. We pass in the number of bytes allocated in order to
* assert the copy is safe inline.
*/
void initialize(absl::string_view key);
void initialize(absl::string_view key, uint64_t num_bytes_allocated);

/**
* Initializes this object to have the specified key,
Expand Down

0 comments on commit 2aa9d8c

Please sign in to comment.