Skip to content

[BUG] [ / test panics when a binary operator is the last argument ([ x = ]) #321

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell (built from main @ 9851f51)
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Binding

Rust

Strands Shell Version

main @ 9851f51 (--version prints 0.0.0)

Language Runtime Version

Rust 1.100.0

Operating System

Linux 6.18 (aarch64)

Installation Method

built from source

Steps to Reproduce

strands-shell -c '[ x = ]; echo "rc=$?"'
strands-shell -c 'e=; [ "$HOME" = $e ]; echo "rc=$?"'
strands-shell -c 'test x -eq; echo "rc=$?"'

The second form is the realistic one: an unquoted variable that is empty leaves the operator as the last argument.

Expected Behavior

Same as bash: an error on stderr, status 2, and the script continues.

bash: line 1: [: x: unary operator expected
rc=2

Actual Behavior

The process panics and exits 101. Nothing after the [ runs.

thread 'main' panicked at src/builtins/test.rs:97:22:
index out of bounds: the len is 2 but the index is 2

Additional Context

Found while reviewing #320. It is not caused by that PR: all three commands panic on main @ 9851f51. I ran debug builds only; I did not test the Python, Node.js or MCP entry points.

Possible Solution

The bounds check in src/builtins/test.rs:91 admits two remaining arguments, but the branch reads three (args[*pos + 2] at line 97):

if *pos + 2 <= args.len()
    && let Some(next) = args.get(*pos + 1)
    && is_binary_op(next)
{
    let left = &args[*pos];
    let op = &args[*pos + 1];
    let right = &args[*pos + 2];

Changing the check to *pos + 3 <= args.len() stops the panic. I have not built or tested that change, and it does not by itself give bash's status 2 for the malformed expression.

Related Issues

#320, #299

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions