Skip to content

Commit

Permalink
Fix incorrect bitmask in Pointer#createConstant(int) (#1472)
Browse files Browse the repository at this point in the history
* Fix incorrect bitmask in Pointer#createConstant(int)

* Update tests to match expectations
  • Loading branch information
dbwiddis committed Oct 9, 2022
1 parent 28ddc33 commit 673079f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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

0 comments on commit 673079f

Please sign in to comment.