Skip to content

truncation of constant value in to_cbor() #1286

@oktonion

Description

@oktonion

Although documentation for static void to_cbor(const basic_json& j, detail::output_adapter<char> o) says nothing I assumed that it has some purpose but f.e. following sample:

	  ::nlohmann::json jj;
	  std::vector<char> test_cbor_data;
	  ::nlohmann::json::to_cbor(jj, test_cbor_data);

generates lots of warnings like

Warning C4309 'static_cast': truncation of constant value testapp json.hpp 7731

in class binary_writer
at lines like:

oa->write_character(static_cast<CharType>(0xF6));

or

oa->write_character(j.m_value.boolean
? static_cast<CharType>(0xF5)
: static_cast<CharType>(0xF4));
break;

Basically the cause for that is static_cast<CharType>(/*some integer value > 127*/) with CharType = char leads to loss of integer value (sizeof(char) is 1 byte and it is signed, so value range is [-128;127] and it can't fit integer number > 127).

I don't know if it is an issue but that code smells.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions