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

Function genValue

json_fuzz_test.go:165–200  ·  view source on GitHub ↗

genValue appends one JSON value to buf, chosen from a weighted distribution that favors containers at shallow depth (to exercise nesting) and scalars at deep depth (to bound recursion).

(r *rand.Rand, depth int, buf *[]byte)

Source from the content-addressed store, hash-verified

163// distribution that favors containers at shallow depth (to exercise
164// nesting) and scalars at deep depth (to bound recursion).
165func genValue(r *rand.Rand, depth int, buf *[]byte) {
166 if depth >= maxGenDepth {
167 // Scalars only at deep recursion.
168 switch r.Intn(4) {
169 case 0:
170 genString(r, buf)
171 case 1:
172 genNumber(r, buf)
173 case 2:
174 *buf = append(*buf, 't', 'r', 'u', 'e')
175 default:
176 *buf = append(*buf, 'n', 'u', 'l', 'l')
177 }
178 return
179 }
180
181 roll := r.Intn(20)
182 switch {
183 case roll < 6: // 30% — object
184 genObject(r, depth, buf)
185 case roll < 11: // 25% — array
186 genArray(r, depth, buf)
187 case roll < 14: // 15% — string
188 genString(r, buf)
189 case roll < 18: // 20% — number
190 genNumber(r, buf)
191 case roll == 18: // 5% — bool
192 if r.Intn(2) == 0 {
193 *buf = append(*buf, 't', 'r', 'u', 'e')
194 } else {
195 *buf = append(*buf, 'f', 'a', 'l', 's', 'e')
196 }
197 default: // 5% — null
198 *buf = append(*buf, 'n', 'u', 'l', 'l')
199 }
200}
201
202// genObject emits an object with 0–5 entries. Keys are drawn from a set
203// that includes the dangerous cases: empty-string "", unicode (\uXXXX),

Callers 3

genJSONFunction · 0.85
genObjectFunction · 0.85
genArrayFunction · 0.85

Calls 4

genStringFunction · 0.85
genNumberFunction · 0.85
genObjectFunction · 0.85
genArrayFunction · 0.85

Tested by

no test coverage detected