Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] edf.py: potential overflow with n_events=256 #12859

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hatute
Copy link

@hatute hatute commented Sep 18, 2024

since the n_events assigned with the UINT8 type of number "ne". So, the default type of n_events will also be UINT8. During the summation of all three byte of "ne" will potentially cause overflow. In my case, python raised overflow error when it reach 256 which exceed the upper limit of 255. Direct Bit-Wise operation can avoid this.

Reference issue (if any)

What does this implement/fix?

Additional information

since the n_events assigned with the UINT8 type of number "ne". So, the default type of n_events will also be UINT8. During the summation of all three byte of "ne" will potentially cause overflow. In my case, python raised overflow error when it reach 256 which exceed the upper limit of 255. Direct Bit-Wise operation can avoid this.
Copy link

welcome bot commented Sep 18, 2024

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴

Comment on lines 1442 to +1443
ne = np.fromfile(fid, UINT8, 3)
n_events = ne[0]
for i in range(1, len(ne)):
n_events = n_events + ne[i] * 2 ** (i * 8)
n_events = ne[0] + (ne[1] << 8) + (ne[2] << 16)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a fix on NumPy < 2 but on 2.1 I get:

>>> np.uint8(255) + np.uint8(255)
<stdin>:1: RuntimeWarning: overflow encountered in scalar add
np.uint8(254)

So I think maybe doing this would be a clean way to fix it

ne = np.fromfile(fid, UINT8, 3).astype(np.int64)

or similar

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