💥 throw exception if nullptr is passed to parse function
Addresses #3584
This commit is contained in:
parent
feef0eb595
commit
b661022ccd
@ -28,7 +28,7 @@ static basic_json parse(IteratorType first, IteratorType last,
|
|||||||
: A compatible input, for instance:
|
: A compatible input, for instance:
|
||||||
|
|
||||||
- an `std::istream` object
|
- an `std::istream` object
|
||||||
- a `FILE` pointer
|
- a `FILE` pointer (will throw [json.exception.parse_error.116](../../home/exceptions.md#jsonexceptionparse_error116) if passed pointer is `#!cpp nullptr`)
|
||||||
- a C-style array of characters
|
- a C-style array of characters
|
||||||
- a pointer to a null-terminated string of single byte characters
|
- a pointer to a null-terminated string of single byte characters
|
||||||
- a `std::string`
|
- a `std::string`
|
||||||
|
|||||||
@ -354,6 +354,16 @@ A UBJSON high-precision number could not be parsed.
|
|||||||
[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A
|
[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### json.exception.parse_error.116
|
||||||
|
|
||||||
|
A `#!cpp FILE*` pointer passed to the [parse](../api/basic_json/parse.md) function is `#!cpp nullptr`; that is, a previous call to `#!cpp std::fopen` failed.
|
||||||
|
|
||||||
|
!!! failure "Example message"
|
||||||
|
|
||||||
|
```
|
||||||
|
[json.exception.parse_error.116] parse error: input file is invalid: No such file or directory
|
||||||
|
```
|
||||||
|
|
||||||
## Iterator errors
|
## Iterator errors
|
||||||
|
|
||||||
This exception is thrown if iterators passed to a library function do not match
|
This exception is thrown if iterators passed to a library function do not match
|
||||||
|
|||||||
@ -9,8 +9,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <array> // array
|
#include <array> // array
|
||||||
|
#include <cerrno> // errno
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <cstring> // strlen
|
#include <cstring> // strlen, strerror
|
||||||
#include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
|
#include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
|
||||||
#include <memory> // shared_ptr, make_shared, addressof
|
#include <memory> // shared_ptr, make_shared, addressof
|
||||||
#include <numeric> // accumulate
|
#include <numeric> // accumulate
|
||||||
@ -48,9 +49,14 @@ class file_input_adapter
|
|||||||
using char_type = char;
|
using char_type = char;
|
||||||
|
|
||||||
JSON_HEDLEY_NON_NULL(2)
|
JSON_HEDLEY_NON_NULL(2)
|
||||||
explicit file_input_adapter(std::FILE* f) noexcept
|
explicit file_input_adapter(std::FILE* f)
|
||||||
: m_file(f)
|
: m_file(f)
|
||||||
{}
|
{
|
||||||
|
if (m_file == nullptr)
|
||||||
|
{
|
||||||
|
JSON_THROW(parse_error::create(116, 0, detail::concat("input file is invalid: ", reinterpret_cast<const char*>(std::strerror(errno))), nullptr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// make class move-only
|
// make class move-only
|
||||||
file_input_adapter(const file_input_adapter&) = delete;
|
file_input_adapter(const file_input_adapter&) = delete;
|
||||||
|
|||||||
@ -5893,8 +5893,9 @@ std::size_t hash(const BasicJsonType& j)
|
|||||||
|
|
||||||
|
|
||||||
#include <array> // array
|
#include <array> // array
|
||||||
|
#include <cerrno> // errno
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <cstring> // strlen
|
#include <cstring> // strlen, strerror
|
||||||
#include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
|
#include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
|
||||||
#include <memory> // shared_ptr, make_shared, addressof
|
#include <memory> // shared_ptr, make_shared, addressof
|
||||||
#include <numeric> // accumulate
|
#include <numeric> // accumulate
|
||||||
@ -5934,9 +5935,14 @@ class file_input_adapter
|
|||||||
using char_type = char;
|
using char_type = char;
|
||||||
|
|
||||||
JSON_HEDLEY_NON_NULL(2)
|
JSON_HEDLEY_NON_NULL(2)
|
||||||
explicit file_input_adapter(std::FILE* f) noexcept
|
explicit file_input_adapter(std::FILE* f)
|
||||||
: m_file(f)
|
: m_file(f)
|
||||||
{}
|
{
|
||||||
|
if (m_file == nullptr)
|
||||||
|
{
|
||||||
|
JSON_THROW(parse_error::create(116, 0, detail::concat("input file is invalid: ", reinterpret_cast<const char*>(std::strerror(errno))), nullptr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// make class move-only
|
// make class move-only
|
||||||
file_input_adapter(const file_input_adapter&) = delete;
|
file_input_adapter(const file_input_adapter&) = delete;
|
||||||
|
|||||||
@ -332,6 +332,12 @@ TEST_CASE("deserialization")
|
|||||||
{
|
{
|
||||||
CHECK_THROWS_WITH_AS("[\"foo\",1,2,3,false,{\"one\":1}"_json, "[json.exception.parse_error.101] parse error at line 1, column 29: syntax error while parsing array - unexpected end of input; expected ']'", json::parse_error&);
|
CHECK_THROWS_WITH_AS("[\"foo\",1,2,3,false,{\"one\":1}"_json, "[json.exception.parse_error.101] parse error at line 1, column 29: syntax error while parsing array - unexpected end of input; expected ']'", json::parse_error&);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("FILE*")
|
||||||
|
{
|
||||||
|
std::FILE* f = std::fopen("nonexisting_file", "r"); // NOTLINT(cppcoreguidelines-owning-memory)
|
||||||
|
CHECK_THROWS_WITH_AS(json::parse(f), "[json.exception.parse_error.116] parse error: input file is invalid: No such file or directory", json::parse_error&);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("contiguous containers")
|
SECTION("contiguous containers")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user