Skip to content

Commit 6f84fb2

Browse files
author
Patrick Soquet
committed
XS: secaudit issue #17
1 parent 65f1bdf commit 6f84fb2

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

xs/sources/xsMath.c

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,44 +295,66 @@ void fx_Math_hypot(txMachine* the)
295295

296296
void fx_Math_idiv(txMachine* the)
297297
{
298-
txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
299-
txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
298+
txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
299+
txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
300300
if (y == 0) {
301301
mxResult->kind = XS_NUMBER_KIND;
302302
mxResult->value.number = C_NAN;
303303
}
304304
else {
305-
mxResult->kind = XS_INTEGER_KIND;
306-
mxResult->value.integer = x / y;
305+
txS8 r = x / y;
306+
if ((-2147483648LL <= r) && (r <= 2147483647LL)) {
307+
mxResult->kind = XS_INTEGER_KIND;
308+
mxResult->value.integer = (txInteger)r;
309+
}
310+
else {
311+
mxResult->kind = XS_NUMBER_KIND;
312+
mxResult->value.number = (txNumber)r;
313+
}
307314
}
308315
}
309316

310317
void fx_Math_idivmod(txMachine* the)
311318
{
312-
txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
313-
txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
319+
txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
320+
txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
314321
if (y == 0) {
315322
mxPushNumber(C_NAN);
316323
mxPushNumber(C_NAN);
317324
}
318325
else {
319-
mxPushInteger(x / y);
320-
mxPushInteger((x % y + y) % y);
326+
txS8 r = x / y;
327+
if ((-2147483648LL <= r) && (r <= 2147483647LL))
328+
mxPushInteger((txInteger)r);
329+
else
330+
mxPushNumber((txNumber)r);
331+
r = (x % y + y) % y;
332+
if ((-2147483648LL <= r) && (r <= 2147483647LL))
333+
mxPushInteger((txInteger)r);
334+
else
335+
mxPushNumber((txNumber)r);
321336
}
322337
fxConstructArrayEntry(the, mxResult);
323338
}
324339

325340
void fx_Math_imod(txMachine* the)
326341
{
327-
txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
328-
txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
342+
txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
343+
txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
329344
if (y == 0) {
330345
mxResult->kind = XS_NUMBER_KIND;
331346
mxResult->value.number = C_NAN;
332347
}
333348
else {
334-
mxResult->kind = XS_INTEGER_KIND;
335-
mxResult->value.integer = (x % y + y) % y;
349+
txS8 r = (x % y + y) % y;
350+
if ((-2147483648LL <= r) && (r <= 2147483647LL)) {
351+
mxResult->kind = XS_INTEGER_KIND;
352+
mxResult->value.integer = (txInteger)r;
353+
}
354+
else {
355+
mxResult->kind = XS_NUMBER_KIND;
356+
mxResult->value.number = (txNumber)r;
357+
}
336358
}
337359
}
338360

@@ -368,15 +390,22 @@ void fx_Math_imuldiv(txMachine* the)
368390

369391
void fx_Math_irem(txMachine* the)
370392
{
371-
txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
372-
txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
393+
txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
394+
txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
373395
if (y == 0) {
374396
mxResult->kind = XS_NUMBER_KIND;
375397
mxResult->value.number = C_NAN;
376398
}
377399
else {
378-
mxResult->kind = XS_INTEGER_KIND;
379-
mxResult->value.integer = x % y;
400+
txS8 r = x % y;
401+
if ((-2147483648LL <= r) && (r <= 2147483647LL)) {
402+
mxResult->kind = XS_INTEGER_KIND;
403+
mxResult->value.integer = (txInteger)r;
404+
}
405+
else {
406+
mxResult->kind = XS_NUMBER_KIND;
407+
mxResult->value.number = (txNumber)r;
408+
}
380409
}
381410
}
382411

0 commit comments

Comments
 (0)