Debian Codegraph @

dead_code_audit_oob_test.go

raw symbols SCIP
1 package jsonparser
2
3 import (
4 "testing"
5 )
6
7 // Test that ObjectEach doesn't panic with out-of-bounds access
8 // after removing the `offset < len(data)` loop guard.
9
10 // Verifies: SYS-REQ-007 [boundary]
11 func TestObjectEach_OOB_TruncatedAfterComma(t *testing.T) {
12 // {"a":1, — truncated right after comma, no more data
13 // After parsing "a":1, finds comma at step 4, increments offset past comma.
14 // Then step "skip to next token after comma" calls nextToken on remaining data.
15 // If remaining is empty → nextToken returns -1 → returns MalformedArrayError.
16 // No panic.
17 err := ObjectEach([]byte(`{"a":1,`), func(key []byte, value []byte, dataType ValueType, offset int) error {
18 return nil
19 })
20 if err == nil {
21 t.Fatal("expected error for truncated object after comma")
22 }
23 t.Logf("Correctly got error: %v", err)
24 }
25
26 // Verifies: SYS-REQ-007 [boundary]
27 func TestObjectEach_OOB_TruncatedAfterColon(t *testing.T) {
28 // {"a": — truncated after colon
29 err := ObjectEach([]byte(`{"a":`), func(key []byte, value []byte, dataType ValueType, offset int) error {
30 return nil
31 })
32 if err == nil {
33 t.Fatal("expected error for truncated object after colon")
34 }
35 t.Logf("Correctly got error: %v", err)
36 }
37
38 // Verifies: SYS-REQ-007 [boundary]
39 func TestObjectEach_OOB_TruncatedAfterKey(t *testing.T) {
40 // {"a" — truncated after key string
41 err := ObjectEach([]byte(`{"a"`), func(key []byte, value []byte, dataType ValueType, offset int) error {
42 return nil
43 })
44 if err == nil {
45 t.Fatal("expected error for truncated object after key")
46 }
47 t.Logf("Correctly got error: %v", err)
48 }
49
50 // Verifies: SYS-REQ-007 [boundary]
51 func TestObjectEach_OOB_TruncatedMidKey(t *testing.T) {
52 // {"a — unterminated string
53 err := ObjectEach([]byte(`{"a`), func(key []byte, value []byte, dataType ValueType, offset int) error {
54 return nil
55 })
56 if err == nil {
57 t.Fatal("expected error for unterminated key string")
58 }
59 t.Logf("Correctly got error: %v", err)
60 }
61
62 // Verifies: SYS-REQ-007 [boundary]
63 func TestObjectEach_OOB_JustOpenBrace(t *testing.T) {
64 // { — only opening brace, then nothing
65 err := ObjectEach([]byte(`{`), func(key []byte, value []byte, dataType ValueType, offset int) error {
66 return nil
67 })
68 if err == nil {
69 t.Fatal("expected error for just opening brace")
70 }
71 t.Logf("Correctly got error: %v", err)
72 }
73
74 // Verifies: SYS-REQ-007 [boundary]
75 func TestObjectEach_OOB_BraceAndWhitespace(t *testing.T) {
76 // { — opening brace then only whitespace
77 err := ObjectEach([]byte(`{ `), func(key []byte, value []byte, dataType ValueType, offset int) error {
78 return nil
79 })
80 if err == nil {
81 t.Fatal("expected error for brace+whitespace")
82 }
83 t.Logf("Correctly got error: %v", err)
84 }
85
86 // ArrayEach infinite loop guard: verify o==0 catches all no-progress cases
87 // Verifies: SYS-REQ-006 [boundary]
88 func TestArrayEach_OOB_MalformedElements(t *testing.T) {
89 tests := []struct {
90 name string
91 json string
92 }{
93 {"bare_comma", `[,]`},
94 {"double_comma", `[1,,2]`},
95 {"just_bracket", `[`},
96 {"bracket_space", `[ `},
97 {"unclosed_string", `["abc`},
98 }
99
100 for _, tt := range tests {
101 t.Run(tt.name, func(t *testing.T) {
102 count := 0
103 _, err := ArrayEach([]byte(tt.json), func(value []byte, dataType ValueType, offset int, err error) {
104 count++
105 if count > 100 {
106 t.Fatal("possible infinite loop detected")
107 }
108 })
109 // We don't care whether it errors; we care that it terminates
110 _ = err
111 t.Logf("Terminated with count=%d, err=%v", count, err)
112 })
113 }
114 }