Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash if custom std::streambuf implementation throws #1105

Closed
laudrup opened this issue Jul 29, 2020 · 1 comment · Fixed by #1358
Closed

Crash if custom std::streambuf implementation throws #1105

laudrup opened this issue Jul 29, 2020 · 1 comment · Fixed by #1358
Labels
bug Something isn't working fixed Something works now, yay!

Comments

@laudrup
Copy link

laudrup commented Jul 29, 2020

Describe the bug
Constructing an std::istream with a custom std::streambuf that throws on construction causes an access violation.

Command-line test case

C:\Temp>type foo.cpp

#include <istream>
#include <string>
#include <stdexcept>

class foo_buf : public std::streambuf {
public:
    foo_buf() {
        throw std::runtime_error("Whoops");
    }
};

class foo_stream : public std::istream {
public:
    foo_stream() : std::istream(new foo_buf) {
    }
};

int main(int, char**) {
    try {
        foo_stream f;
    } catch (const std::exception&) {
        return 1;
    }
    return 0;
}

CL.exe /c /Zi /nologo /W3 /WX- /diagnostics:column /Od /Ob0 /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /EHsc /
  RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"foo.dir\Debug\\" /Fd"foo.dir\Debug\vc142.pdb" /Gd /TP /errorReport:queue C:\Temp\foo.cpp  foo.cp

link.exe /ERRORREPORT:QUEUE /OUT:"C:\Temp\Debug\foo.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib wins
  pool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:/Temp/Debug/foo.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /D
  YNAMICBASE /NXCOMPAT /IMPLIB:"C:/Temp/Debug/foo.lib" /MACHINE:X64  /machine:x64 foo.dir\Debug\foo.obj

Debug\foo.exe

Exception thrown at 0x00007FFE0C6E6E85 (msvcp140d.dll) in foo.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Expected behavior

The std::runtime_error thrown from the foo_buf constructor should be caught and main should return 1.

STL version

  • Visual Studio version
Microsoft Visual Studio Enterprise 2019
Version 16.6.5

I don't see any reason why an std::streambuf implementation shouldn't be allowed to throw and this works fine with other compilers and library implementations (Gnu and LLVM), so I assume this could be a bug in this STL implementation.

@laudrup laudrup changed the title <header>: Crash if custom std::streambuf implementation throws Crash if custom std::streambuf implementation throws Jul 29, 2020
@CaseyCarter CaseyCarter added the bug Something isn't working label Jul 29, 2020
@crackedmind
Copy link
Contributor

I did some debug, and found that crash happens at this line

https://github.com/microsoft/STL/blob/master/stl/src/ios.cpp#L29

because _Stdstr is not initialized at this moment and we have array out of boundary error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants