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

Function deleteFoundConfig

parser.go:1249–1383  ·  view source on GitHub ↗

SYS-REQ-115

(config Config, data []byte, keys ...string)

Source from the content-addressed store, hash-verified

1247
1248// SYS-REQ-115
1249func deleteFoundConfig(config Config, data []byte, keys ...string) (result []byte, found bool) {
1250 lk := len(keys)
1251 if lk == 0 {
1252 // Deleting the root produces an empty document whose backing array
1253 // must not alias the caller's input.
1254 return make([]byte, 0), true
1255 }
1256
1257 array := false
1258 if len(keys[lk-1]) > 0 && string(keys[lk-1][0]) == "[" {
1259 array = true
1260 }
1261
1262 var startOffset, keyOffset int
1263 endOffset := len(data)
1264 var err error
1265 if !array {
1266 if len(keys) > 1 {
1267 _, _, startOffset, endOffset, err = internalGetConfig(config, data, keys[:lk-1]...)
1268 if err != nil {
1269 // problem parsing the data
1270 return data, false
1271 }
1272 }
1273
1274 keyOffset, err = findKeyStartConfig(config, data[startOffset:endOffset], keys[lk-1])
1275 if err == KeyPathNotFoundError {
1276 // problem parsing the data
1277 return data, false
1278 }
1279 keyOffset += startOffset
1280 var subEndOffset int
1281 _, _, _, subEndOffset, err = internalGetConfig(config, data[startOffset:endOffset], keys[lk-1])
1282 if err != nil {
1283 return data, false
1284 }
1285 endOffset = startOffset + subEndOffset
1286 tokEnd := tokenEnd(data[endOffset:])
1287 tokStart := findTokenStart(data[:keyOffset], ","[0])
1288
1289 if endOffset+tokEnd >= len(data) {
1290 // tokenEnd sentinel: no delimiter found, input is truncated
1291 return data, false
1292 }
1293
1294 // guard: tokenEnd sentinel may return -1 on truncated input; bounds-check before deref.
1295 idx := endOffset + tokEnd
1296 // Scan forward from idx through any JSON whitespace to find the next
1297 // real token. The original check only matched a single ' ' byte
1298 // before the comma, so inputs like '{"a":1,\n"b":2}' or '[0,0 ,0]'
1299 // (multiple whitespace bytes) bypassed the cleanup and left a
1300 // dangling comma sequence. Found by FuzzPathMutation.
1301 nextTokIdx := idx
1302 for nextTokIdx < len(data) && isJSONWhitespace(data[nextTokIdx]) {
1303 nextTokIdx++
1304 }
1305 if len(data) > idx && data[idx] == ',' {
1306 endOffset += tokEnd + 1

Callers 3

DeleteFunction · 0.85
DeleteFoundFunction · 0.85
DeleteMethod · 0.85

Calls 7

internalGetConfigFunction · 0.85
findKeyStartConfigFunction · 0.85
tokenEndFunction · 0.85
findTokenStartFunction · 0.85
isJSONWhitespaceFunction · 0.85
lastTokenFunction · 0.85
nextTokenConfigFunction · 0.85

Tested by

no test coverage detected