Add assertion if nullptr is passed to parse function (#3593)

Addresses #3584
This commit is contained in:
Niels Lohmann
2022-07-23 01:26:51 +02:00
committed by GitHub
parent b185c5bc9d
commit dbfd33a70a
5 changed files with 52 additions and 4 deletions

View File

@@ -102,3 +102,30 @@ behavior and yields a runtime assertion.
```
Assertion failed: (m_object != nullptr), function operator++, file iter_impl.hpp, line 368.
```
### Reading from a null `FILE` pointer
Reading from a null `#!cpp FILE` pointer is undefined behavior and yields a runtime assertion. This can happen when
calling `#!cpp std::fopen` on a nonexistent file.
??? example "Example 4: Uninitialized iterator"
The following code will trigger an assertion at runtime:
```cpp
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
std::FILE* f = std::fopen("nonexistent_file.json", "r");
json j = json::parse(f);
}
```
Output:
```
Assertion failed: (m_file != nullptr), function file_input_adapter, file input_adapters.hpp, line 55.
```