Skip to content

Segfault with impl &mut ~str: Pushable #4559

@SimonSapin

Description

@SimonSapin

Hi,

In an attempt to have string.push_char(c) be a sweet and short form for str::push_char(&mut string, c), I wrote this:

trait Pushable {
    fn push_char(self, ch: char);
}

impl &mut ~str: Pushable {
    fn push_char(self, ch: char) { str::push_char(self, ch) }
}

fn main() {
    let mut string: ~str = ~"";
    string.push_char('z')
}

This is probably wrong in many interesting ways, and I expected a compiler error. Instead, the program above compiles fine but segfaults at runtime.

Valdgrind output:

==23276== Memcheck, a memory error detector
==23276== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==23276== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==23276== Command: ./push
==23276== 
==23276== Thread 3:
==23276== Invalid read of size 8
==23276==    at 0x4E86697: str::push_char::_46c858744a40b9d5::_06 (in /home/simon/non-packages/lib/rustc/x86_64-unknown-linux-gnu/lib/libcore-c3ca5d77d81b46c1-0.6.so)
==23276==    by 0x400CFB: __extensions__::meth_1803::push_char::_e72f466627d24520::_00 (in /home/simon/dev/specs/rust-cssparser/push)
==23276==    by 0x400D91: main::_6d2ee9f4a6e27ad::_00 (in /home/simon/dev/specs/rust-cssparser/push)
==23276==    by 0x400E2D: _rust_main (in /home/simon/dev/specs/rust-cssparser/push)
==23276==    by 0x52335A3: task_start_wrapper(spawn_args*) (rust_task.cpp:160)
==23276==  Address 0x1f is not stack'd, malloc'd or (recently) free'd
==23276== 
==23276== 
==23276== Process terminating with default action of signal 11 (SIGSEGV)
==23276==  Access not within mapped region at address 0x1F
==23276==    at 0x4E86697: str::push_char::_46c858744a40b9d5::_06 (in /home/simon/non-packages/lib/rustc/x86_64-unknown-linux-gnu/lib/libcore-c3ca5d77d81b46c1-0.6.so)
==23276==    by 0x400CFB: __extensions__::meth_1803::push_char::_e72f466627d24520::_00 (in /home/simon/dev/specs/rust-cssparser/push)
==23276==    by 0x400D91: main::_6d2ee9f4a6e27ad::_00 (in /home/simon/dev/specs/rust-cssparser/push)
==23276==    by 0x400E2D: _rust_main (in /home/simon/dev/specs/rust-cssparser/push)
==23276==    by 0x52335A3: task_start_wrapper(spawn_args*) (rust_task.cpp:160)
==23276==  If you believe this happened as a result of a stack
==23276==  overflow in your program's main thread (unlikely but
==23276==  possible), you can try to increase the size of the
==23276==  main thread stack using the --main-stacksize= flag.
==23276==  The main thread stack size used in this run was 8388608.
==23276== 
==23276== HEAP SUMMARY:
==23276==     in use at exit: 1,064,779 bytes in 30 blocks
==23276==   total heap usage: 30 allocs, 0 frees, 1,064,779 bytes allocated
==23276== 
==23276== LEAK SUMMARY:
==23276==    definitely lost: 0 bytes in 0 blocks
==23276==    indirectly lost: 0 bytes in 0 blocks
==23276==      possibly lost: 576 bytes in 2 blocks
==23276==    still reachable: 1,064,203 bytes in 28 blocks
==23276==         suppressed: 0 bytes in 0 blocks
==23276== Rerun with --leak-check=full to see details of leaked memory
==23276== 
==23276== For counts of detected and suppressed errors, rerun with: -v
==23276== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-trait-systemArea: Trait systemI-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions