Skip to content

Crash during read()/xsgetn() on default constructed ifstream #168628

@rupprecht

Description

@rupprecht

After #165223, and including the fix in #167779, the following crashes during the read() call:

#include <cassert>
#include <fstream>

int main() {
    char buf[10];
    std::ifstream ifs;

    assert(!ifs.is_open());
    assert(!ifs.bad());
    assert(!ifs.fail());
    assert(!ifs.eof());
    assert(ifs.good());

    ifs.read(buf, sizeof(buf));  // Fails here

    assert(!ifs.is_open());
    assert(!ifs.bad());
    assert(ifs.fail());
    assert(ifs.eof());
    assert(!ifs.good());
}

I couldn't get a stack trace w/o using asan, so here's the asan stack trace:

==1==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7d6b3b67fb52 bp 0x7ffde2c5ccc0 sp 0x7ffde2c5c430 T0)
==1==The signal is caused by a READ memory access.
==1==Hint: address points to the zero page.
    #0 0x7d6b3b67fb52 in _IO_fread (/lib/x86_64-linux-gnu/libc.so.6+0x7fb52) (BuildId: d5197096f709801829b118af1b7cf6631efa2dcd)
    #1 0x5f78b3e77a19 in fread /root/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc
    #2 0x7d6b3bbe6cd6 in std::__1::basic_filebuf<char, std::__1::char_traits<char>>::xsgetn(char*, long) (/opt/compiler-explorer/clang-assertions-trunk/lib/x86_64-unknown-linux-gnu/libc++.so.1+0x77cd6)
    #3 0x7d6b3bbdcd6f in std::__1::basic_istream<char, std::__1::char_traits<char>>::read(char*, long) (/opt/compiler-explorer/clang-assertions-trunk/lib/x86_64-unknown-linux-gnu/libc++.so.1+0x6dd6f)
    #4 0x5f78b3f410ad in main /app/example.cpp:14:9
    #5 0x7d6b3b629d8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: d5197096f709801829b118af1b7cf6631efa2dcd)
    #6 0x7d6b3b629e3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e3f) (BuildId: d5197096f709801829b118af1b7cf6631efa2dcd)
    #7 0x5f78b3e573e4 in _start (/app/output.s+0x2e3e4)

Live repro: https://compiler-explorer.com/z/3vxqM7ssz

Metadata

Metadata

Assignees

Labels

libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions