fix: prevent slice aliasing in LabelValues.With#1311
Open
Yanhu007 wants to merge 1 commit intogo-kit:masterfrom
Open
fix: prevent slice aliasing in LabelValues.With#1311Yanhu007 wants to merge 1 commit intogo-kit:masterfrom
Yanhu007 wants to merge 1 commit intogo-kit:masterfrom
Conversation
LabelValues.With used append directly, which reuses the underlying
array when capacity is available. This caused sibling With calls
to overwrite each other's label values:
base := counter.With("a", "1")
c1 := base.With("b", "2") // has ["a","1","b","2"]
c2 := base.With("c", "3") // overwrites c1 to ["a","1","c","3"]
Fix by allocating a new slice in With to prevent aliasing.
Add test to verify sibling With calls don't corrupt each other.
Fixes go-kit#1296
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1296
Problem
LabelValues.Withusesappenddirectly, which reuses the underlying array when spare capacity exists. This causes siblingWithcalls to overwrite each other's label values:This affects all metrics types (Counter, Gauge, Histogram) across all providers that use
LabelValues.With.Fix
Allocate a new slice in
Withto prevent aliasing the parent's backing array:Added a test that verifies sibling
Withcalls don't corrupt each other.