In read_header(), header length is read as an unsigned, little-endian short int, which is currently done by reading two char and concatenating them into an unsigned int.
However the sign-ness of char in C++ is not specified (implementation defined). When char is treated as signed char, the code immediately overflows for byte in range [128, 255], and causing all latter buffer allocating go wrong.
uint32_t header_length;
if (version == version_t{1, 0}) {
char header_len_le16[2];
istream.read(header_len_le16, 2);
header_length = (header_len_le16[0] << 0) | (header_len_le16[1] << 8); // header_length incorrect for signed char!
.......
In
read_header()
, header length is read as an unsigned, little-endian short int, which is currently done by reading two char and concatenating them into an unsigned int.However the sign-ness of char in C++ is not specified (implementation defined). When char is treated as signed char, the code immediately overflows for byte in range [128, 255], and causing all latter buffer allocating go wrong.