-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
type_error factory creates a dangling pointer (in VisualStudio 2019) #2535
Comments
@nlohmann |
Update: Under the hood we are using
To me this seems a library bug in MSVC, but I'm not sure the wording is enforcing the compiler vendors to make a copy of |
|
@evolv-bricerive What is exact version of Visual Studio/MSVC that you are using? I tested the following code on and it worked as expected. I tried it on Visual Studio 2019 ver. 16.6.2 which ships msvc 19.26.28806. #include <iostream>
#include "json.hpp"
int main()
{
nlohmann::json j = "Hello";
try
{
j.at(0);
}
catch (std::exception& exp)
{
std::cout << "Caught the exception: " << exp.what() << std::endl;
}
} Output:
P.S. I would be nice if you send a minimal code snippet that reproduces this issue. |
@nickaein It is hard to reproduce because the actual issue is a dangling pointer to freed memory. |
@gregmarr
It also assumes the string will be there during exception processing. |
@evolv-bricerive Can you try the sample code above and see whay happens? It should trigger the same exception-generation path, I guess. I doubt this is a bug in library as it is continuously under test and this bug is trivial to catch for current tests. This could be either caused by unexpected behaviour of MSCV library (not making a copy of error message in |
@evolv-bricerive I don't believe that an expectation that the |
I don't know how MSVC handles this, but the implementation makes the assumptions that are described here: https://stackoverflow.com/a/38482856. |
@gregmarr @nickaein Not trying to make a point, here; Just letting you guys know about the story. Feel free to ignore it. |
@evolv-bricerive You can't technically have something that lives as long as something else but doesn't outlive it because there is an order to the destruction of the two objects, so there is no real difference between "lives at least as long as" and "outlives". Having said that, I still don't believe that this is required by the standard. Do you have a reference that says otherwise? All the references that we've found say that the Even MSVC's documentation says that the value returned by |
@gregmarr I see your point about "outlives". Having said that, If I understand you correctly, you believe the library is correct according to the standard, so the crash is MS's problem. Fine with me. |
What is the issue you have?
I get a non-catchable C00000409 exception from the error handling code.
I have a piece of code which mistakenly tries to call at() on a string.
This triggers this error handling code:
Which in turn, calls the type_error factory:
Which calls the type_error constructor:
Which calls the exception contructor:
Which (in VisualStudio 2019) calls the std::runtime_error constructor:
Which then calls (in VisualStudio 2019) the std::exception constructor:
Which finally stores the exception message in this struct:
The issue is that the message is stored as a pointer, which in this case is the c_str() of a string which will be destroyed as soon as we exit the type_error factory.
When we then throw the exception object, the vcruntime kills the program.
Which compiler and operating system are you using?
Which version of the library did you use?
develop
branchThe text was updated successfully, but these errors were encountered: