-
-
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
json::from_cbor does not respect allow_exceptions = false when input is string literal #1715
Comments
allow_exceptions = false
when input is string literal
allow_exceptions = false
when input is string literal
This is strange: First off, changing the literal to a json cbor = json::from_cbor(std::string("A"), true, false);
printf("is_discarded: %i\n", cbor.is_discarded()); output:
This code correctly selects this function: static basic_json from_cbor(detail::input_adapter&& i,
const bool strict = true,
const bool allow_exceptions = true) The original code, however, uses this function: template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
static basic_json from_cbor(A1 && a1, A2 && a2,
const bool strict = true,
const bool allow_exceptions = true) Binding That version of template<typename CharT,
typename std::enable_if<
std::is_pointer<CharT>::value and
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0>
input_adapter(CharT b, std::size_t l)
: ia(std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l)) {} Unfortunately, I do not know how to avoid the conversion from |
At least the example here works with just $git diff .
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
index 2a32a829..d2d3ccf3 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -4107,13 +4107,14 @@ class input_adapter
: ia(std::make_shared<wide_string_input_adapter<std::u32string>>(ws)) {}
/// input adapter for buffer
- 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_adapter(CharT b, std::size_t l)
+ input_adapter(CharT b, SizeT l)
: ia(std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l)) {}
// derived support
but I'm probably missing a billion things. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I debugged the issue and found some remarkable coincidence I would like to share:
In order to fix this, we need to make sure the the terminating null byte introduced by string literals is already treated as EOF and not returned as 0x00. One quick fix for this is to add an overload template<std::size_t N>
input_buffer_adapter input_adapter(const char (&array)[N])
{
return input_adapter(std::begin(array), N-1);
} that returns const char input[] = { 'A', 0x00 }; which would then silently remove the null byte and making a valid example fail. Any ideas on this? |
I think better is to have |
Function
json::from_cbor
throws exception even withallow_exceptions = false
when input is string literal.Note
json::from_cbor
:It should print
is_discarded: 1
.Throws exception:
Clang 7.
The text was updated successfully, but these errors were encountered: