-
Notifications
You must be signed in to change notification settings - Fork 69
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
msgpack: truncate long string prefix refinements #167
Merged
apparentlymart
merged 2 commits into
zclconf:main
from
kmoe:msgpack-truncate-prefix-refinement
Aug 24, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package msgpack | |
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/zclconf/go-cty/cty" | ||
|
@@ -50,7 +51,6 @@ func TestRoundTrip(t *testing.T) { | |
cty.UnknownVal(cty.String).Refine().NotNull().StringPrefix("foo-").NewValue(), | ||
cty.String, | ||
}, | ||
|
||
{ | ||
cty.True, | ||
cty.Bool, | ||
|
@@ -350,3 +350,58 @@ func TestRoundTrip(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
// Unknown values with very long string prefix refinements do not round-trip | ||
// losslessly. If the prefix is longer than 256 bytes it will be truncated to | ||
// a maximum of 256 bytes. | ||
func TestRoundTrip_truncatesStringPrefixRefinement(t *testing.T) { | ||
tests := []struct { | ||
Value cty.Value | ||
Type cty.Type | ||
RoundTripValue cty.Value | ||
}{ | ||
{ | ||
cty.UnknownVal(cty.String).Refine().StringPrefix(strings.Repeat("a", 1024)).NewValue(), | ||
cty.String, | ||
cty.UnknownVal(cty.String).Refine().StringPrefix(strings.Repeat("a", 255)).NewValue(), | ||
}, | ||
{ | ||
cty.UnknownVal(cty.String).Refine().NotNull().StringPrefix(strings.Repeat("b", 1024)).NewValue(), | ||
cty.String, | ||
cty.UnknownVal(cty.String).Refine().NotNull().StringPrefix(strings.Repeat("b", 255)).NewValue(), | ||
}, | ||
{ | ||
cty.UnknownVal(cty.String).Refine().StringPrefix(strings.Repeat("c", 255) + "-").NewValue(), | ||
cty.String, | ||
cty.UnknownVal(cty.String).Refine().StringPrefix(strings.Repeat("c", 255) + "-").NewValue(), | ||
}, | ||
{ | ||
cty.UnknownVal(cty.String).Refine().StringPrefix(strings.Repeat("d", 255) + "🤷🤷").NewValue(), | ||
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. Happy to add comments for any of these that need it. For example, in this case the |
||
|
||
cty.String, | ||
cty.UnknownVal(cty.String).Refine().StringPrefix(strings.Repeat("d", 255)).NewValue(), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(fmt.Sprintf("%#v as %#v", test.Value, test.Type), func(t *testing.T) { | ||
b, err := Marshal(test.Value, test.Type) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Logf("encoded as %x", b) | ||
|
||
got, err := Unmarshal(b, test.Type) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !got.RawEquals(test.RoundTripValue) { | ||
t.Errorf( | ||
"unexpected value after round-trip\ninput: %#v\nexpect: %#v\nresult: %#v", | ||
test.Value, test.RoundTripValue, got) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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
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.
I assume this is a mistake, unless "takes into account grapheme cluster boundaries" is intended to have a different meaning here than in line 14.
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.
I think what I was intending to say here is that this function trims off zero or more normalization chunks so as to avoid the possibility that the resulting string might end with one that can be the prefix of a grapheme cluster chunk. Or in other words, this function potentially violates the usual rule that cty treats grapheme clusters as indivisible units under substring operations; it in fact intentionally divides them because it's trying to detect the possibility of partial ones.
But I agree that this comment is more confusing than helpful in retrospect, so I'm fine with removing it and if some confusion arises later about this the discussion about it will hopefully suggest a clearer way to explain the tradeoff this function is making.