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

Cast unsigned values explicitly to avoid signed extension #5966

Closed
wants to merge 1 commit into from
Closed

Cast unsigned values explicitly to avoid signed extension #5966

wants to merge 1 commit into from

Conversation

koutheir
Copy link
Contributor

Cast unsigned values, stored in smaller types, to long using zero-extension instead of Java's default sign extension.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Feb 13, 2023
@@ -112,6 +112,6 @@ private long encode(ReferenceMapEncoder.OffsetIterator offsets) {
private void encodeRun(int offset, int refsCount) {
assert offset >= 0 && refsCount >= 0;
writeBuffer.putS4(offset);
writeBuffer.putU4(refsCount);
writeBuffer.putU4(Integer.toUnsignedLong(refsCount));

Choose a reason for hiding this comment

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

I don's see how refsCount can ever get to anything remotely close to MAX_INT. What problem is this solving?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If refsCount is an unsigned number, and it must be converted to a long for the function call, then the conversion needs to be a zero extension, not a signed extension.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe the assertion that refsCount >= 0 above is enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wouldn't call the assertion enough, but it surely will detect cases where refsCount is negative or huge, as long as assertions are explicitly enabled when running the code, and that is not the default. What I'm fixing here is the extension from int to long necessary at the call site to putU4(): that has to be a zero-extension, not a sign-extension.

@mdcssw mdcssw closed this by deleting the head repository May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants