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

<system_error>: passes a temporary string to runtime_error #3304

Closed
mihaisebea opened this issue Dec 22, 2022 · 1 comment
Closed

<system_error>: passes a temporary string to runtime_error #3304

mihaisebea opened this issue Dec 22, 2022 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@mihaisebea
Copy link

system_error class gives runtime error a temporary std string
https://github.com/microsoft/STL/blob/main/stl/inc/system_error#L477

and runtime_error seems to pass a temporary string to exception

explicit runtime_error(const string& _Message) : _Mybase(_Message.c_str()) {}

if _HAS_EXCEPTIONS is defined to 1 it seems exception class does copy the string

 __std_exception_copy(&_InitData, &_Data);

but if _HAS_EXCEPTIONS = 0 the exception class only stores a pointer

const char* _Ptr; // the message pointer

Command-line test case

C:\Temp>type repro.cpp
#include <iostream>

int main() {
  
    std::string error_what("test system_error");
    std::error_code error_code(0, std::system_category());				
    auto se = std::system_error(error_code, error_what);    
    std::puts(se.what());

}
C:\Test>cl /W4 /WX /D "_HAS_EXCEPTIONS=0" /MDd .\test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.34.31935 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 14.34.31935.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

C:\Test>test.exe
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦

Expected behavior
For the a proper message to be printed instead of garbage

C:\Test>cl /EHsc /W4 /WX /MDd .\test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.34.31935 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 14.34.31935.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

C:\Test>test.exe
test system_error: La operaci¾n se complet¾ correctamente.

STL version
Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.4.2
Additional context
At this point it's hard for me to assume that I found a legit bug and nobody is using _HAS_EXCEPTIONS=0
but all my c++ experts are in vacation already :) 🤷‍♂️

@AlexGuteniev
Copy link
Contributor

Duplicate of #2114

@CaseyCarter CaseyCarter added the duplicate This issue or pull request already exists label Dec 23, 2022
@CaseyCarter CaseyCarter closed this as not planned Won't fix, can't repro, duplicate, stale Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants