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

[std][hl] Fix array pos check, force UInt #11810

Open
wants to merge 4 commits into
base: development
Choose a base branch
from

Conversation

yuxiaomao
Copy link
Contributor

Closes HaxeFoundation/hashlink#723

Seems to be some missing cast from #8408

@Simn
Copy link
Member

Simn commented Oct 31, 2024

This entire UInt business here looks pretty wonky, can't we just do a pos < 0 || pos >= length check like normal humans?

@yuxiaomao
Copy link
Contributor Author

yuxiaomao commented Oct 31, 2024

I searched UInt in std and got for example

public function charCodeAt(index:Int):Null<Int> {
var idx:UInt = index;
if (idx >= (length : UInt))
return null;
return bytes.getUI16(index << 1);
}

So I thought it's the "normal" usage. Can change to pos < 0 || pos >= length too if you prefer.

UInt seems to save a comparison (using OJULt)

@Simn
Copy link
Member

Simn commented Oct 31, 2024

Doesn't this just work incidentally because something like -1 becomes a large number that then exceeds length? I don't see how this is sound because with a sufficiently small number you could end up with something that's < length after casting.

@yuxiaomao
Copy link
Contributor Author

Negative Int has the most significant bit at 1, with INT_MIN it's 0x80000000 and is always bigger (unsigned operation) than a normal length ranged from 0 to INT_MAX (0x7FFFFFFF)

@Simn
Copy link
Member

Simn commented Nov 1, 2024

Ah right, because the length is Int, not UInt... Well, if this is how you want to handle this then I'm fine with it.

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.

[NIGHTLY?] Array getDyn broken
2 participants