Skip to content

Commit 673079f

Browse files
authored
Fix incorrect bitmask in Pointer#createConstant(int) (#1472)
* Fix incorrect bitmask in Pointer#createConstant(int) * Update tests to match expectations
1 parent 28ddc33 commit 673079f

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Bug Fixes
1515
---------
1616
* [#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).
1717
* [#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).
18+
* [#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).
1819

1920
Release 5.12.1
2021
==============

contrib/platform/src/com/sun/jna/platform/win32/COM/util/ProxyObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public boolean equals(Object arg) {
189189
@Override
190190
public int hashCode() {
191191
long id = this.getUnknownId();
192-
return (int) ((id >>> 32) & 0xFFFFFFFF) + (int) (id & 0xFFFFFFFF);
192+
return (int) ((id >>> 32) + (id & 0xFFFFFFFFL));
193193
}
194194

195195
@Override

src/com/sun/jna/Pointer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static final Pointer createConstant(long peer) {
6060
systems.
6161
*/
6262
public static final Pointer createConstant(int peer) {
63-
return new Opaque((long)peer & 0xFFFFFFFF);
63+
return new Opaque(peer & 0xFFFFFFFFL);
6464
}
6565

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

113113
@Override
114114
public int hashCode() {
115-
return (int)((peer >>> 32) + (peer & 0xFFFFFFFF));
115+
return (int) ((peer >>> 32) + (peer & 0xFFFFFFFFL));
116116
}
117117

118118

test/com/sun/jna/PointerTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626
package com.sun.jna;
2727

28+
import java.lang.reflect.InvocationTargetException;
2829
import java.lang.reflect.Method;
2930
import java.lang.reflect.Modifier;
30-
import java.lang.reflect.InvocationTargetException;
3131
import java.util.Arrays;
32+
3233
import junit.framework.TestCase;
3334

3435

@@ -174,10 +175,13 @@ public void testReadPointerArray() {
174175

175176
public void testCreateConstantPointer() {
176177
Pointer p = Pointer.createConstant(0xFFFFFFFF);
177-
assertEquals("Wrong peer value", p.peer, 0xFFFFFFFF);
178+
assertEquals("Wrong peer value", p.peer, 0xFFFFFFFFL);
178179

179-
p = Pointer.createConstant(-1);
180+
p = Pointer.createConstant(-1L);
180181
assertEquals("Wrong peer value", p.peer, -1);
182+
183+
p = Pointer.createConstant(0x80000000);
184+
assertEquals("createConstant(int) should avoid setting any high bits", 0, Pointer.nativeValue(p) >>> 32);
181185
}
182186

183187
public void testReadStringArrayNULLElement() {

0 commit comments

Comments
 (0)