Skip to content

s = s.trim() stores freed bytes: the assignment drops the old s before reading the view of it #2366

Closed

Description

@joshhickson

Since D111 (str is a value), assigning a view of a str back to the same variable reads freed memory. No error, no warning, and exit 0.

fn main:
    var s = "  alpha  ".to_owned()
    s = s.trim()
    print(f"[{s}]")
  • Main 7d729c2c printed [alpha].
  • Main 7ecc120a prints [ ]. With WITH_ALLOC_NO_REUSE=1 WITH_DEBUG_ALLOC_SCRIBBLE=1 --debug-alloc it prints scribble bytes and "leak count=0", with no error.

The pinned seed nightly-20261009-local-2-1140e66035cc has the same bug, and so do these:

  • trim_start() and trim_end();
  • let v = u.trim(); u = v;
  • every one-liner of the form with -p 'line=line.trim()', which prints NUL bytes for each line.

s = s.trim().to_owned() is correct.

Exact line. MIR of main (with-stage1 check --dump-mir, main 7ecc120a), with _1 = s:

bb1: {
    _1 = move _4;
    drop(_2) @ drop#4 scope-exit _2;
    _4 = const zst(ty16);
    _5 = ref(shared, _1);
    call const fn sym115(copy _5) -> [return: _6, next: bb2];   // s.trim()
  }
bb2: {
    _7 = copy _6;                       // the &str view into s's buffer
    drop(_1) @ drop#8 scope-exit _1;    // the old s is freed here
    _1 = copy+retain _7.*;              // ...then the new s is read through the dangling view

The old value of the destination is dropped before the right-hand side, which is a view of that same value, becomes an owned str. The new value should be made first (copy or retain into a temporary), then the old one dropped, then the store done.

Not #2307. #2307 (closed, fixed by 1140e660) is about a str made from an interior view of a temporary. This case is a view of the destination itself, and it still fails on 1140e660 and on e4861e1d.

Silent tools. with analyze t.w 'explain:origin:main:s' reports "no view origins recorded". The checker accepts both s = s.trim() and u = v while v is a live view of u. The debug allocator shows it only with SCRIBBLE.

Checked on Linux x86_64 (WSL2) on the newest pinned seed nightly-20261011-local-2-e4861e1d6997 (main e4861e1d, it prints [ plus five NUL bytes plus ]), builds of e8641d89 and 8399773e, a release build and stage1 of 7ecc120a, and a release build of 7d729c2c for the old behaviour. Main is now 2d281239. Of the five commits after e4861e1d, two touch src/MirLower.w, both for List (c5a0209a, a match that consumes a List; c97a0085, the List header) and not str assignment. Not bisected. The 25 commits between 7d729c2c and 1140e660 include 9a8b9781 (D111), b4b21a79 (D120) and 1140e660.

Activity

  1. added a commit that references this issue on Oct 14, 2026
  2. ehartford commented on Oct 14, 2026

    @ehartford
    Contributor

    Fixed by #2402: the assignment makes the new value (with its hold) before the overwrite drop; fixture behav_2366_assign_view_of_self.

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