Skip to content

Commit 8c50ed1

Browse files
committed
Fix header length error due to signed->unsigned conversion
1 parent 18f6524 commit 8c50ed1

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

include/xtensor/io/xnpy.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,9 @@ namespace xt
482482
char header_len_le16[2];
483483
istream.read(header_len_le16, 2);
484484

485-
uint16_t header_length = uint16_t(header_len_le16[0] << 0) | uint16_t(header_len_le16[1] << 8);
485+
uint16_t header_b0 = static_cast<uint16_t>(static_cast<uint8_t>(header_len_le16[0]));
486+
uint16_t header_b1 = static_cast<uint16_t>(static_cast<uint8_t>(header_len_le16[1])) << 8;
487+
uint16_t header_length = header_b0 | header_b1;
486488

487489
if ((magic_string_length + 2 + 2 + header_length) % 16 != 0)
488490
{
@@ -502,8 +504,11 @@ namespace xt
502504
char header_len_le32[4];
503505
istream.read(header_len_le32, 4);
504506

505-
uint32_t header_length = uint32_t(header_len_le32[0] << 0) | uint32_t(header_len_le32[1] << 8)
506-
| uint32_t(header_len_le32[2] << 16) | uint32_t(header_len_le32[3] << 24);
507+
uint32_t header_b0 = static_cast<uint32_t>(static_cast<uint8_t>(header_len_le32[0]));
508+
uint32_t header_b1 = static_cast<uint32_t>(static_cast<uint8_t>(header_len_le32[1])) << 8;
509+
uint32_t header_b2 = static_cast<uint32_t>(static_cast<uint8_t>(header_len_le32[2])) << 16;
510+
uint32_t header_b3 = static_cast<uint32_t>(static_cast<uint8_t>(header_len_le32[3])) << 24;
511+
uint32_t header_length = header_b0 | header_b1 | header_b2 | header_b3;
507512

508513
if ((magic_string_length + 2 + 4 + header_length) % 16 != 0)
509514
{

0 commit comments

Comments
 (0)