Skip to content

Commit

Permalink
Merge pull request #2140 from nlohmann/issue1715
Browse files Browse the repository at this point in the history
Respect allow_exceptions=false for binary formats
  • Loading branch information
nlohmann committed May 28, 2020
2 parents a01759a + 225fa58 commit 9ec0e4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 3 additions & 2 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,14 @@ inline input_stream_adapter input_adapter(std::istream&& stream)
return input_stream_adapter(stream);
}

template<typename CharT,
template<typename CharT, typename SizeT,
typename std::enable_if<
std::is_pointer<CharT>::value and
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
not std::is_same<SizeT, bool>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
input_buffer_adapter input_adapter(CharT b, std::size_t l)
input_buffer_adapter input_adapter(CharT b, SizeT l)
{
return input_buffer_adapter(reinterpret_cast<const char*>(b), l);
}
Expand Down
5 changes: 3 additions & 2 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4753,13 +4753,14 @@ inline input_stream_adapter input_adapter(std::istream&& stream)
return input_stream_adapter(stream);
}

template<typename CharT,
template<typename CharT, typename SizeT,
typename std::enable_if<
std::is_pointer<CharT>::value and
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
not std::is_same<SizeT, bool>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
input_buffer_adapter input_adapter(CharT b, std::size_t l)
input_buffer_adapter input_adapter(CharT b, SizeT l)
{
return input_buffer_adapter(reinterpret_cast<const char*>(b), l);
}
Expand Down
22 changes: 22 additions & 0 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,28 @@ TEST_CASE("regression tests")
json j = val;
}

SECTION("issue #1715 - json::from_cbor does not respect allow_exceptions = false when input is string literal")
{
SECTION("string literal")
{
json cbor = json::from_cbor("B", true, false);
CHECK(cbor.is_discarded());
}

SECTION("string array")
{
const char input[] = { 'B', 0x00 };
json cbor = json::from_cbor(input, true, false);
CHECK(cbor.is_discarded());
}

SECTION("std::string")
{
json cbor = json::from_cbor(std::string("B"), true, false);
CHECK(cbor.is_discarded());
}
}

SECTION("issue #1805 - A pair<T1, T2> is json constructible only if T1 and T2 are json constructible")
{
static_assert(!std::is_constructible<json, std::pair<std::string, NotSerializableData>>::value, "");
Expand Down

0 comments on commit 9ec0e4c

Please sign in to comment.