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

Function truncateMidEscape

json_fuzz_test.go:649–676  ·  view source on GitHub ↗

truncateMidEscape cuts inside a \uXXXX escape sequence. This is the truncated_escape_sequence class: the parser's Unescape routine must detect the truncated escape and return MalformedStringEscapeError.

(r *rand.Rand, data []byte)

Source from the content-addressed store, hash-verified

647// truncated_escape_sequence class: the parser's Unescape routine must
648// detect the truncated escape and return MalformedStringEscapeError.
649func truncateMidEscape(r *rand.Rand, data []byte) []byte {
650 var cuts []int
651 for i := 0; i+1 < len(data); i++ {
652 if data[i] != '\\' || data[i+1] != 'u' {
653 continue
654 }
655 // \uXXXX is 6 bytes. Cut somewhere in [i+2, i+6).
656 lo := i + 2
657 hi := i + 6
658 if hi > len(data) {
659 hi = len(data)
660 }
661 if hi > lo {
662 cuts = append(cuts, lo+r.Intn(hi-lo))
663 }
664 }
665 if len(cuts) == 0 {
666 // No \u escape present; inject a truncated one and exercise the path.
667 out := make([]byte, 0, len(data)+5)
668 out = append(out, data...)
669 out = append(out, '"', '\\', 'u', '3', '0')
670 return out
671 }
672 cut := cuts[r.Intn(len(cuts))]
673 out := make([]byte, cut)
674 copy(out, data[:cut])
675 return out
676}
677
678// truncateMidElement cuts inside an array element, after a '[' or ','. The
679// truncated_mid_element class: the parser must not read past the cut when

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected