-
Notifications
You must be signed in to change notification settings - Fork 5.4k
stats: fix a bug where empty StatName cannot initialize storage correctly #45348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wbpcode
wants to merge
3
commits into
envoyproxy:main
Choose a base branch
from
wbpcode:dev-minor-stats-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+32
−30
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -178,6 +178,14 @@ class SymbolTable final { | |
| */ | ||
| static void appendEncoding(uint64_t number, MemBlockBuilder<uint8_t>& mem_block); | ||
|
|
||
| /** | ||
| * Appends stat_name's data into storage. | ||
| * | ||
| * @param stat_name the stat_name to append. | ||
| * @param mem_block the memory block to append to. | ||
| */ | ||
| static void appendStatNameDataTo(StatName stat_name, MemBlockBuilder<uint8_t>& mem_block); | ||
|
|
||
| /** | ||
| * Appends stat_name's bytes into mem_block, which must have been allocated to | ||
| * allow for stat_name.size() bytes. | ||
|
|
@@ -633,36 +641,15 @@ class StatName { | |
| size_t dataSize() const { return dataAsStringView().size(); } | ||
|
|
||
| /** | ||
| * @return size_t the number of bytes in the symbol array, including the | ||
| * overhead for the size itself. | ||
| * @return size_t the number of bytes in the symbol array, including the overhead for the size | ||
| * itself. NOTE: this will return at least one byte even when there is no backing storage, to | ||
| * indicate the encoded length prefix when encoding the StatName into a StatNameList and so on. | ||
| * However, you can NEVER assume that data()/dataIncludingSize() will return non-nullptr just | ||
| * because this returns non-zero; the empty()/dataSize() APIs should be used to check whether the | ||
| * StatName has valid data. | ||
| */ | ||
| size_t size() const { return SymbolTable::Encoding::totalSizeBytes(dataSize()); } | ||
|
|
||
| /** | ||
| * Copies the entire StatName representation into a MemBlockBuilder, including | ||
| * the length metadata at the beginning. The MemBlockBuilder must not have | ||
| * any other data in it. | ||
| * | ||
| * @param mem_block_builder the builder to receive the storage. | ||
| */ | ||
| void copyToMemBlock(MemBlockBuilder<uint8_t>& mem_block_builder) { | ||
| ASSERT(mem_block_builder.size() == 0); | ||
| mem_block_builder.appendData(absl::MakeSpan(size_and_data_, size())); | ||
| } | ||
|
|
||
| /** | ||
| * Appends the data portion of the StatName representation into a | ||
| * MemBlockBuilder, excluding the length metadata. This is appropriate for | ||
| * join(), where several stat-names are combined, and we only need the | ||
| * aggregated length metadata. | ||
| * | ||
| * @param mem_block_builder the builder to receive the storage. | ||
| */ | ||
| void appendDataToMemBlock(MemBlockBuilder<uint8_t>& storage) { | ||
| auto sv = dataAsStringView(); | ||
| storage.appendData(absl::MakeSpan(reinterpret_cast<const uint8_t*>(sv.data()), sv.size())); | ||
| } | ||
|
Comment on lines
-653
to
-664
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To keep consistence, we move this logic to Encoding as a helper method. |
||
|
|
||
| #ifndef ENVOY_CONFIG_COVERAGE | ||
| void debugPrint(); | ||
| #endif | ||
|
|
@@ -695,7 +682,6 @@ class StatName { | |
| */ | ||
| bool startsWith(StatName symbolic_prefix) const; | ||
|
|
||
|
wbpcode marked this conversation as resolved.
|
||
| private: | ||
| /** | ||
| * Casts the raw data as a string_view, decoding the varint length prefix. | ||
| * All accessors that need the data pointer and/or size should delegate to | ||
|
|
@@ -709,6 +695,7 @@ class StatName { | |
| return {reinterpret_cast<const char*>(size_and_data_ + prefix_size), data_size}; | ||
| } | ||
|
|
||
| private: | ||
| const uint8_t* size_and_data_{nullptr}; | ||
|
wbpcode marked this conversation as resolved.
|
||
| }; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method doesn't take the special case where size_and_data_ be null take into account. At that case the size() will return 1, but the size_and_data_ is null, then crash. The
Encoding::appendToMemBlockhave handled that correctly. So we remove this method and reuse theEncoding::appendToMemBlock.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just document this and add an ASSERT? This is a pretty big change for a corner-case, especially un-inlining a performance critical function.