From 33924733e1a6973bdb91c68a9c29ca08bd3966dd Mon Sep 17 00:00:00 2001 From: "Nathaniel J. McClatchey, PhD" Date: Sun, 16 Aug 2020 21:21:40 -0700 Subject: [PATCH 1/2] Fix a mis-parse of NPY v2.0 and 3.0 headers CNPY originally only supported NPY 1.0 headers, with a 2-byte length field. This commit causes parse_npy_header to respond to NPY 2.0 and 3.0 by reading the header length as 4 bytes. --- cnpy.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cnpy.cpp b/cnpy.cpp index 2d28578..4c78a8a 100644 --- a/cnpy.cpp +++ b/cnpy.cpp @@ -63,8 +63,13 @@ void cnpy::parse_npy_header(unsigned char* buffer,size_t& word_size, std::vector //std::string magic_string(buffer,6); uint8_t major_version = *reinterpret_cast(buffer+6); uint8_t minor_version = *reinterpret_cast(buffer+7); - uint16_t header_len = *reinterpret_cast(buffer+8); - std::string header(reinterpret_cast(buffer+9),header_len); + bool extended_header = (major_version > 1); + uint32_t header_len; + if (extended_header) + header_len = *reinterpret_cast(buffer+8); + else + header_len = *reinterpret_cast(buffer+8); + std::string header(reinterpret_cast(buffer+(extended_header ? 11 : 9)),header_len); size_t loc1, loc2; From 4f8c6e40c6ccfeeac62377aad291cb212fca6a9d Mon Sep 17 00:00:00 2001 From: "Nathaniel J. McClatchey, PhD" Date: Tue, 2 Sep 2025 20:00:04 -0500 Subject: [PATCH 2/2] Bugfix: `header` string was oversized The `header` string was started too early, and had included the final byte of the length field (and omitting the final byte). This commit adjusts where it starts to fix the issue. Note: No ill effects occurred earlier because each parsed field is located using `find`, and the final byte is typically `}` and irrelevant to the parsed fields. --- cnpy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cnpy.cpp b/cnpy.cpp index 4c78a8a..0a7402c 100644 --- a/cnpy.cpp +++ b/cnpy.cpp @@ -69,7 +69,7 @@ void cnpy::parse_npy_header(unsigned char* buffer,size_t& word_size, std::vector header_len = *reinterpret_cast(buffer+8); else header_len = *reinterpret_cast(buffer+8); - std::string header(reinterpret_cast(buffer+(extended_header ? 11 : 9)),header_len); + std::string header(reinterpret_cast(buffer+(extended_header ? 12 : 10)),header_len); size_t loc1, loc2;