Skip to content

runtime str.byte_at silently returns 0 on an out-of-bounds index #996

Closed

Description

@migueldecimal128

Summary

str.byte_at silently returns 0 for an out-of-bounds index at runtime. An
out-of-range index is a logic bug in the caller; returning a plausible in-band
value hides it.

fn main:
    let s = "abc"
    print(f"{s.byte_at(99)}")   // 0
    print(f"{s.byte_at(-1)}")   // 0

rt/rt_core.w:2086:

fn str_byte_at_ref(s: &str, idx: i64) -> i32:
    let slen = str_length(s)
    if idx < 0 or idx >= slen:
        return 0
    let p = str_data(s)
    (unsafe p[idx]) as i32

The comptime evaluator does the right thing on the same input:

comptime fn ct -> i32: "abc".byte_at(99)
const X: i32 = comptime ct()
error: str.byte_at() index out of bounds in comptime

The comptime side is correct here; the runtime side is the defect.

Why 0 is the wrong sentinel

0 is a legitimate in-band value — a NUL byte in the string. The two cases are
indistinguishable to a caller, and the codebase already tests for the in-band
meaning. lib/std/build.w:671, inside a bounded loop:

let ch = path.byte_at(i as i64)
if ch == 0 or ch == 9 or ch == 10 or ch == 13:
    return false

That call is in-bounds and correct, but it shows 0 carrying real meaning at a
byte_at result. Any caller doing the same with an unguarded index silently
conflates "NUL byte" with "read past the end."

What is not wrong

UTF-8 handling is correct, and comptime and runtime agree exactly:

comptime: len=6 b0=104 b1=195 b2=169     // "héllo"
runtime : len=6 b0=104 b1=195 b2=169

byte_at is byte-indexed as named; é occupies indices 1-2 and yields its raw
0xC3 0xA9 pair. len() is byte length in both. Recorded here so this does not
get re-investigated.

Fix direction and cost

The direction is settled — the runtime should not silently swallow an
out-of-range index. The form is still open: panic (matching indexed access
elsewhere), or return Option[u8], or a checked/unchecked pair. That choice is a
§12 contract decision.

Migration is the real cost. There are 1336 byte_at call sites across
lib/, src/, and rt/, with the compiler itself the largest consumer. Every
sampled site in lib/std/build.w is explicitly guarded — if path.len() == 0
before byte_at(0) at :663-665 and :694-696, loops bounded by len() — so
the guarded pattern is already the house style and those sites would not break.
But 1336 is well past what can be verified by inspection.

Expect a strict byte_at to surface latent out-of-bounds reads rather than
land silently
. That is the point of the change, but it should be planned as a
bug-finding exercise with a full battery run, not as a mechanical swap.

Relationship to #943

Same class as #943 — a comptime/runtime disagreement about what one expression
means — reached from an unrelated direction. #943 is an arithmetic-width defect
where the comptime side is wrong; this one is a bounds-contract defect where the
runtime side is wrong. Two instances found independently is evidence the class
is broader than either bug, and strengthens the case for stating the
comptime/runtime agreement requirement in §17.1 rather than leaving it implicit.

Filed separately because the fix touches the runtime string contract and shares
no code with #943.

Provenance

Measured with the released v0.15.1.7 binary (04ca1a74); code read at
22e534c6. The two are equivalent for this bug:

  • rt/rt_core.w differs between the revisions by a single hunk at :2511
    (vec_grow) — str_byte_at_ref at :2086 is untouched.
  • The byte_at arm in src/ComptimeEval.w is unchanged.

Investigated and drafted jointly by @migueldecimal128 and Claude (Claude Code).

Activity

  1. added
    bug
    on Sep 3, 2026
  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

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

    $ cat main.w
    let s = "abc"
    print(f"{s.byte_at(99)}")
    print(f"{s.byte_at(-1)}")
    $ with run main.w
    0
    0
    rc=0
    

    pre-release: an out-of-range positional access returns an in-band value, where D71 says it panics; the fix is one branch in str_byte_at_ref.

  3. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Fixed on main by eff60eb (#2687), merged through stack 5 whose seed-driven battery was green on its top (gate, uat and survey all green). Regression test: test/behavior/behav_996_str_byte_at_out_of_range_panics.w.

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

    bugdo-before-releasereproduced

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions