Skip to content

Fix NPY header length error due to signed->unsigned conversion#2889

Merged
JohanMabille merged 1 commit intoxtensor-stack:masterfrom
Kwonunn:npy-header-fix
Apr 7, 2026
Merged

Fix NPY header length error due to signed->unsigned conversion#2889
JohanMabille merged 1 commit intoxtensor-stack:masterfrom
Kwonunn:npy-header-fix

Conversation

@Kwonunn
Copy link
Copy Markdown
Contributor

@Kwonunn Kwonunn commented Apr 3, 2026

Checklist

  • The title and commit message(s) are descriptive.
  • Small commits made to fix your PR have been squashed to avoid history pollution.
  • Tests have been added for new features or bug fixes.
  • API of new functions and classes are documented.

Description

This PR fixes an error pertaining to reading the length of the header of an npy file. The header is read as two chars and then combined into a uint16_t. Previously due to char being a signed value, any value with the MSB set would get converted as if it were a negative number and thus misinterpreted.

For example, the bytes F6 00 which means a header of 246 bytes would be interpreted as a header of 65526 bytes.

I think the code could be shortened a little, but I think when messing with bits like this it's best to be explicit. Also, the compiler will likely optimize it anyway.

@Kwonunn Kwonunn changed the title Fix header length error due to signed->unsigned conversion Fix NPY header length error due to signed->unsigned conversion Apr 3, 2026
@JohanMabille
Copy link
Copy Markdown
Member

The linter error is unrelated and has been fixed on the master branch.

@JohanMabille
Copy link
Copy Markdown
Member

Thanks for the fix!

@JohanMabille JohanMabille merged commit f21347a into xtensor-stack:master Apr 7, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants