Skip to content

Result[str, E].unwrap_or returns a str with the payload's length and a bad data pointer (segfault / garbage) #1363

Closed

Description

@ehartford

Result[str, E].unwrap_or(default) yields a str whose length is the Ok payload's but whose bytes are not: reading it faults or returns garbage.

Spec: §10 (Option/Result eliminators) and D22 (unwrap_or is a contextual join; the result must be the payload or the default, never a mix).

Repro (a stage1 built from origin/main 5fd93bc; the installed v0.15.2.1 behaves the same):

use std.fs
fn main:
    let r = read_file("/tmp/p70k.txt")   // any file of ~70 KB (a 92-byte file prints garbage instead of faulting)
    let t = r.unwrap_or("")
    print(t.slice(0, 8))

Actual: SIGSEGV. lldb:

stop reason = EXC_BAD_ACCESS (code=1, address=0x100098010)
frame #0 rt_memcpy at rt_core.w:324:18 [inlined]
frame #1 alloc_str at rt_core.w:1643:25
frame #2 main at repro.w:5:22

t.len() prints the file's length (399682 for src/Parser.w); t.slice(...) / .replace(...) / print(t) read from a bad data pointer. With a 92-byte file the same program prints a line of spaces. match read_file(p): Ok(t) => ... works, so the defect is in the unwrap_or path (MirLower lower_unwrap_or_method, src/MirLower.w ~12485, or the D22 join feeding it).

Expected: t is the file's text.

Workaround used: match read_file(p): Ok(t) => t, Err(_) => "".

Found while building the #1354 matrix: with run of a one-off rewrite script that did read_file(path).unwrap_or("") on src/Parser.w segfaulted.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions