Merge pull request #125615 from benluddy/apiextensions-json-unmarshal-copy

Copy input to UnmarshalJSON on the apiextensions JSON types.
This commit is contained in:
Kubernetes Prow Robot 2024-07-08 08:22:42 -07:00 committed by GitHub
commit d2d55831e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 2 deletions

View File

@ -130,7 +130,7 @@ func (s JSON) MarshalJSON() ([]byte, error) {
func (s *JSON) UnmarshalJSON(data []byte) error {
if len(data) > 0 && !bytes.Equal(data, nullLiteral) {
s.Raw = data
s.Raw = append(s.Raw[0:0], data...)
}
return nil
}

View File

@ -148,3 +148,24 @@ func TestJSONSchemaPropsOrArrayMarshalJSON(t *testing.T) {
}
}
}
func TestJSONUnderlyingArrayReuse(t *testing.T) {
const want = `{"foo":"bar"}`
b := []byte(want)
var s JSON
if err := s.UnmarshalJSON(b); err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Underlying array is modified.
copy(b[2:5], "bar")
copy(b[8:11], "foo")
// If UnmarshalJSON copied the bytes of its argument, then it should not have been affected
// by the mutation.
if got := string(s.Raw); got != want {
t.Errorf("unexpected mutation, got %s want %s", got, want)
}
}

View File

@ -130,7 +130,7 @@ func (s JSON) MarshalJSON() ([]byte, error) {
func (s *JSON) UnmarshalJSON(data []byte) error {
if len(data) > 0 && !bytes.Equal(data, nullLiteral) {
s.Raw = data
s.Raw = append(s.Raw[0:0], data...)
}
return nil
}

View File

@ -148,3 +148,24 @@ func TestJSONSchemaPropsOrArrayMarshalJSON(t *testing.T) {
}
}
}
func TestJSONUnderlyingArrayReuse(t *testing.T) {
const want = `{"foo":"bar"}`
b := []byte(want)
var s JSON
if err := s.UnmarshalJSON(b); err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Underlying array is modified.
copy(b[2:5], "bar")
copy(b[8:11], "foo")
// If UnmarshalJSON copied the bytes of its argument, then it should not have been affected
// by the mutation.
if got := string(s.Raw); got != want {
t.Errorf("unexpected mutation, got %s want %s", got, want)
}
}