Skip to content

Index 0 of an empty array returns parse-error status instead of missing-index status #23

Description

@memotype

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 [ ].

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmediumMedium priorityparser-followupCoverage exposes parser behavior needing separate work

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions