duplicateKey injects a duplicate object key, turning {"k":1} into {"k":1,"k":1}. This exercises duplicate-key handling: the parser should return the first value for Get without structural corruption.
(r *rand.Rand, data []byte)
| 699 | // {"k":1,"k":1}. This exercises duplicate-key handling: the parser should |
| 700 | // return the first value for Get without structural corruption. |
| 701 | func duplicateKey(r *rand.Rand, data []byte) []byte { |
| 702 | for i := 0; i+2 < len(data); i++ { |
| 703 | if data[i] != '"' { |
| 704 | continue |
| 705 | } |
| 706 | // Find the closing quote of this key. |
| 707 | keyEnd := -1 |
| 708 | for k := i + 1; k < len(data); k++ { |
| 709 | if data[k] == '"' && data[k-1] != '\\' { |
| 710 | keyEnd = k |
| 711 | break |
| 712 | } |
| 713 | } |
| 714 | if keyEnd == -1 || keyEnd+1 >= len(data) || data[keyEnd+1] != ':' { |
| 715 | continue |
| 716 | } |
| 717 | // Find the end of the value (next ',' or '}' or ']' at depth 0). |
| 718 | valEnd := keyEnd + 2 |
| 719 | depth := 0 |
| 720 | inStr := false |
| 721 | done: |
| 722 | for valEnd < len(data) { |
| 723 | b := data[valEnd] |
| 724 | if inStr { |
| 725 | if b == '"' && data[valEnd-1] != '\\' { |
| 726 | inStr = false |
| 727 | } |
| 728 | } else { |
| 729 | switch b { |
| 730 | case '"': |
| 731 | inStr = true |
| 732 | case '{', '[': |
| 733 | depth++ |
| 734 | case '}', ']': |
| 735 | if depth == 0 { |
| 736 | break done |
| 737 | } |
| 738 | depth-- |
| 739 | case ',': |
| 740 | if depth == 0 { |
| 741 | break done |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | valEnd++ |
| 746 | } |
| 747 | if valEnd <= keyEnd+2 || valEnd > len(data) { |
| 748 | continue |
| 749 | } |
| 750 | pair := data[i:valEnd] |
| 751 | out := make([]byte, 0, len(data)+len(pair)+1) |
| 752 | out = append(out, data[:valEnd]...) |
| 753 | out = append(out, ',') |
| 754 | out = append(out, pair...) |
| 755 | out = append(out, data[valEnd:]...) |
| 756 | return out |
| 757 | } |
| 758 | return data |
nothing calls this directly
no outgoing calls
no test coverage detected