Skip to content

One-liners and the REPL have no bounds checks: with -e 'print(f"{[1, 2][5]}")' reads past the end and exits 0 #2268

Closed

Description

@joshhickson

In with -e/-n/-p (and the REPL), indexing or slicing past the end does not panic. It reads out of bounds and the program exits 0. The same code in a file panics with "index out of bounds" under with run or with build (exit 134), as D71 says. The spec allows unchecked indexing only in release builds (types.md §4.3a: "Bounds checking in debug mode, unchecked in release"), and a one-liner is not a release build.

$ with -e 'let a = [1, 2]' -e 'print(f"{a[5]}")'
0                                   # exit 0
$ printf 'one\n' | with -n 'print(line.split(" ")[1])'
                                    # an empty line, exit 0
$ printf 'let a = [1, 2]\nprint(f"{a[5]}")\n' > t.w && with run t.w
panic: index out of bounds          # exit 134

-O0 doesn't change the one-liner result. panic("x") in -e does panic, so the runtime panic path works there.

Cause. The checks are emitted only when debug info is on. mir_emit_debug_index_bounds_check and mir_emit_debug_slice_bounds_check return at once when self.debug_info == 0 (src/CodegenDispatch.w:7999, :8018). run_one_liner_command calls comp.set_debug_info(false) (src/main.w:843), and run_repl_line does the same (:5528). So the safety check depends on a setting that is about debugging output. This matters most for one-liners: awk-style line.fields()[1] on a short line should panic (the cheat sheet in #2127 says so), and instead it silently prints "".

Fix direction (your call). Gate the bounds checks on the release setting, not on debug info, so a build without debug info still checks. Or turn debug info on for one-liners and the REPL. The first follows the spec's wording.

Checked on Linux x86_64 (WSL2) with the seed nightly-20261005-local-1-3207aacb2c74 and with stage1 of main 1f946c14; the two lines cited are unchanged on main 1917e058.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced.

    $ with -e 'let a = [1, 2]' -e 'print(f"{a[5]}")'
    232804278132
    rc=0
    

    pre-release: a one-liner reads out of bounds silently where a file panics (D71); one-liners are a stated priority.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Fixed on main by e3b0ad0 (PR #2664, stack #2668; battery green on the stack top 9f92dbb). The commit message names the root-cause line and the regression test.

  3. joshhickson commented on Oct 28, 2026

    @joshhickson
    ContributorAuthor

    Thanks! Confirmed on main 8b297e36 (Linux x86_64): every shape from the report now panics with "index out of bounds" (exit 134): with -e, with -n, with -p, the REPL, and a slice past the end ("slice index out of bounds"). --release stays unchecked, as §4.3a says.

  4. ehartford commented on Oct 28, 2026

    @ehartford
    Contributor

    I apprecite your contributions Josh.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions