Skip to content

Commit 1e5d2e6

Browse files
committed
HPy: fix over/underflow in native fast-paths for HPyLong_FromXYZ
1 parent e79a70a commit 1e5d2e6

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

graalpython/com.oracle.graal.python.jni/src/hpy_jni.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,36 +296,32 @@ static double augment_Long_AsDouble(HPyContext *ctx, HPy h) {
296296
}
297297

298298
static HPy augment_Long_FromLong(HPyContext *ctx, long l) {
299-
int32_t i = (int32_t) l;
300-
if (l == i) {
301-
return toPtr(boxInt(i));
299+
if (isBoxableInt(l)) {
300+
return toPtr(boxInt((int32_t) l));
302301
} else {
303302
return original_Long_FromLong(ctx, l);
304303
}
305304
}
306305

307306
static HPy augment_Long_FromUnsignedLong(HPyContext *ctx, unsigned long l) {
308-
int32_t i = (int32_t) l;
309-
if (l == i) {
310-
return toPtr(boxInt(i));
307+
if (isBoxableUnsignedInt(l)) {
308+
return toPtr(boxInt((int32_t) l));
311309
} else {
312310
return original_Long_FromUnsignedLong(ctx, l);
313311
}
314312
}
315313

316314
static HPy augment_Long_FromLongLong(HPyContext *ctx, long long l) {
317-
int32_t i = (int32_t) l;
318-
if (l == i) {
319-
return toPtr(boxInt(i));
315+
if (isBoxableInt(l)) {
316+
return toPtr(boxInt((int32_t) l));
320317
} else {
321318
return original_Long_FromLongLong(ctx, l);
322319
}
323320
}
324321

325322
static HPy augment_Long_FromUnsignedLongLong(HPyContext *ctx, unsigned long long l) {
326-
int32_t i = (int32_t) l;
327-
if (l == i) {
328-
return toPtr(boxInt(i));
323+
if (isBoxableUnsignedInt(l)) {
324+
return toPtr(boxInt((int32_t) l));
329325
} else {
330326
return original_Long_FromUnsignedLongLong(ctx, l);
331327
}

graalpython/com.oracle.graal.python.jni/src/hpy_jni.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
#include <hpy.h>
4949
#include <jni.h>
50+
#include <stdint.h>
5051

5152
//*************************
5253
// BOXING
@@ -65,6 +66,8 @@
6566
#define unboxHandle(value) (value)
6667
#define boxHandle(handle) (handle)
6768

69+
#define isBoxableInt(value) (INT32_MIN < (value) && (value) < INT32_MAX)
70+
#define isBoxableUnsignedInt(value) ((value) < INT32_MAX)
6871
#define unboxInt(value) ((int32_t) ((value) - NAN_BOXING_INT))
6972
#define boxInt(value) ((((uint64_t) (value)) & NAN_BOXING_INT_MASK) + NAN_BOXING_INT)
7073

0 commit comments

Comments
 (0)