Severity: Medium
Expected real-world likelihood: Medium to high
Problem
Querying index 0 of an empty array returns status 2, while other out-of-range indexes return the documented missing-index status 1.
Empty lists are common in API and provisioning data. Iteration still stops because both statuses are nonzero, but callers that distinguish missing data from malformed/unsupported input receive the wrong result.
Reproduction
./bj.sh '[]' 0
printf 'status=%s\n' "$?"
./bj.sh '[]' 1
printf 'status=%s\n' "$?"
Actual:
index 0: status=2
index 1: status=1
Nested form:
./bj.sh '{"items":[]}' items 0
# Actual: empty output, status 2
# Expected: empty output, status 1
Suspected cause
Index 0 uses a shortcut:
[) [[ $q = 0 ]] || co 1 ;;
For an empty array, this bypasses container scanning. The following final-output iteration then encounters ] as an unexpected token and returns status 2.
Possible fixes (suggestions only)
These are possible approaches, not prescribed implementations:
- Have the index-zero path detect an immediate closing bracket and return missing-index status 1.
- Route index zero through list scanning while preserving the fast path for nonempty arrays.
- Refactor list selection so every index, including zero, shares one exhaustion-status path.
Suggested regression coverage
Against the readable source and generated forms:
- root and nested empty arrays queried at index 0;
- indexes 1 and larger on empty arrays;
- index 0 on single-element arrays;
- iteration over an empty array terminates immediately with exact status 1;
- whitespace inside the empty array, such as
[ ].
Severity: Medium
Expected real-world likelihood: Medium to high
Problem
Querying index 0 of an empty array returns status 2, while other out-of-range indexes return the documented missing-index status 1.
Empty lists are common in API and provisioning data. Iteration still stops because both statuses are nonzero, but callers that distinguish missing data from malformed/unsupported input receive the wrong result.
Reproduction
Actual:
Nested form:
Suspected cause
Index 0 uses a shortcut:
For an empty array, this bypasses container scanning. The following final-output iteration then encounters
]as an unexpected token and returns status 2.Possible fixes (suggestions only)
These are possible approaches, not prescribed implementations:
Suggested regression coverage
Against the readable source and generated forms:
[ ].