Skip to content

Commit

Permalink
Empty objects start with a constant hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephburnett committed Feb 28, 2022
1 parent 2caa226 commit 5048bb6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func fuzz(t *testing.T, aStr, bStr string) {
}, {
"patch", "list",
}} {
a, _ = ReadJsonString(aStr) // Fresh parsed copy.
var metadata []Metadata
switch format[0] {
case "jd":
Expand Down
12 changes: 10 additions & 2 deletions lib/fuzz_backport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ import "testing"
// To run fuzzing use `go1.18beta2 test -tags=test_fuzz ./lib/ -fuzz=FuzzJd`
func TestFuzzBackport(t *testing.T) {
for _, backport := range [][2]string{{
"[]", "0", // FuzzJd/e193f6c4bfd5b8d3c12e1ac42162b2ccd7a31f9aafd466066c1ec7a95da48e1e
// FuzzJd/e193f6c4bfd5b8d3c12e1ac42162b2ccd7a31f9aafd466066c1ec7a95da48e1e
"[]",
"0",
}, {
"{}", " ", // FuzzJd/868060b2021521d32933f40415c6f95b38fda5f5c6bdb7fa6664d046c637c03c
// FuzzJd/868060b2021521d32933f40415c6f95b38fda5f5c6bdb7fa6664d046c637c03c
"{}",
" ",
}, {
// FuzzJd/61c145c6c646c53946229fb0125821ff47c91b63e87da5709002b4fee8b96ca4
"[{},[]]",
"[{},[{},[]]]",
}} {
fuzz(t, backport[0], backport[1])
}
Expand Down
6 changes: 5 additions & 1 deletion lib/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func (o jsonObject) ident(metadata []Metadata) [8]byte {
if len(keys) == 0 {
return o.hashCode(metadata)
}
hashes := make(hashCodes, 0)
hashes := hashCodes{
// We start with a constant hash to distinguish between
// an empty object and an empty array.
[8]byte{0x4B, 0x08, 0xD2, 0x0F, 0xBD, 0xC8, 0xDE, 0x9A}, // random bytes
}
for key := range keys {
v, ok := o.properties[key]
if ok {
Expand Down

0 comments on commit 5048bb6

Please sign in to comment.