MCPcopy Create free account
hub / github.com/buger/jsonparser / pathShapeMatchesData

Function pathShapeMatchesData

json_fuzz_test.go:1023–1064  ·  view source on GitHub ↗

pathShapeMatchesData is a stricter version of pathShapeMatchesRoot that also verifies container-type compatibility at EVERY nesting level, not just the root. This catches the nested-D6 divergence: an array-index path component ([N]) targeting a non-array value at depth > 0, or an object-key componen

(data []byte, path []string)

Source from the content-addressed store, hash-verified

1021// on the documented-open nested-D6 divergence (where Set produces invalid
1022// JSON like `{"co":1,42}` when [N] targets an object value).
1023func pathShapeMatchesData(data []byte, path []string) bool {
1024 // Delegate the root-level checks (empty key, special bytes, root D6)
1025 // to the existing helper from path_fuzz_test.go.
1026 if !pathShapeMatchesRoot(data, path) {
1027 return false
1028 }
1029 if len(path) < 2 {
1030 return true // root check is sufficient for single-component paths
1031 }
1032 // Walk the path, resolving each component and checking the container
1033 // type at the next level. If the path doesn't fully resolve (Set would
1034 // create intermediate structure), we conservatively allow it — the
1035 // json.Valid post-condition will still catch real corruption.
1036 current := data
1037 for i := 0; i < len(path)-1; i++ {
1038 comp := path[i]
1039 isArrayIdx := len(comp) > 0 && comp[0] == '['
1040 if isArrayIdx && !containerIs(current, '[') {
1041 return false // nested D6: array-index on non-array value
1042 }
1043 if !isArrayIdx && !containerIs(current, '{') {
1044 return false // nested D6: object-key on non-object value
1045 }
1046 val, _, _, err := Get(current, comp)
1047 if err != nil {
1048 // Path prefix doesn't resolve — Set would create structure.
1049 // We can't check deeper levels; conservatively allow.
1050 return true
1051 }
1052 current = val
1053 }
1054 // Check the last component against its resolved parent.
1055 last := path[len(path)-1]
1056 isLastArrayIdx := len(last) > 0 && last[0] == '['
1057 if isLastArrayIdx && !containerIs(current, '[') {
1058 return false
1059 }
1060 if !isLastArrayIdx && !containerIs(current, '{') {
1061 return false
1062 }
1063 return true
1064}
1065
1066// =============================================================================
1067// Gate runner (the assertions that catch each bug class)

Callers 2

FuzzSequenceFunction · 0.85
runStructureAwareGatesFunction · 0.85

Calls 3

pathShapeMatchesRootFunction · 0.85
containerIsFunction · 0.85
GetFunction · 0.85

Tested by

no test coverage detected