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 incorrect bitmask in Pointer#createConstant(int) #1472

Merged
merged 2 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Bug Fixes
---------
* [#1452](https://github.com/java-native-access/jna/issues/1452): Fix memory allocation/handling for error message generation in native library code (`dispatch.c`) - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1460](https://github.com/java-native-access/jna/issues/1460): Fix win32 variant date conversion in DST offest window and with millisecond values - [@eranl](https://github.com/eranl).
* [#1472](https://github.com/java-native-access/jna/issues/1472): Fix incorrect bitmask in `c.s.j.Pointer#createConstant(int)` - [@dbwiddis](https://github.com/dbwiddis).

Release 5.12.1
==============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public boolean equals(Object arg) {
@Override
public int hashCode() {
long id = this.getUnknownId();
return (int) ((id >>> 32) & 0xFFFFFFFF) + (int) (id & 0xFFFFFFFF);
return (int) ((id >>> 32) + (id & 0xFFFFFFFFL));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/com/sun/jna/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static final Pointer createConstant(long peer) {
systems.
*/
public static final Pointer createConstant(int peer) {
return new Opaque((long)peer & 0xFFFFFFFF);
return new Opaque(peer & 0xFFFFFFFFL);
}

/** Pointer value of the real native pointer. Use long to be 64-bit safe.
Expand Down Expand Up @@ -112,7 +112,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return (int)((peer >>> 32) + (peer & 0xFFFFFFFF));
return (int) ((peer >>> 32) + (peer & 0xFFFFFFFFL));
}


Expand Down
10 changes: 7 additions & 3 deletions test/com/sun/jna/PointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@

package com.sun.jna;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

import junit.framework.TestCase;


Expand Down Expand Up @@ -174,10 +175,13 @@ public void testReadPointerArray() {

public void testCreateConstantPointer() {
Pointer p = Pointer.createConstant(0xFFFFFFFF);
assertEquals("Wrong peer value", p.peer, 0xFFFFFFFF);
assertEquals("Wrong peer value", p.peer, 0xFFFFFFFFL);

p = Pointer.createConstant(-1);
p = Pointer.createConstant(-1L);
assertEquals("Wrong peer value", p.peer, -1);

p = Pointer.createConstant(0x80000000);
assertEquals("createConstant(int) should avoid setting any high bits", 0, Pointer.nativeValue(p) >>> 32);
}

public void testReadStringArrayNULLElement() {
Expand Down