better range checking

This commit is contained in:
dzaima 2024-09-26 03:09:12 +03:00
parent 00a9dc03be
commit 9ca48a55bc

View File

@ -286,33 +286,33 @@ static u32 styG(B x) {
static NOINLINE void ffi_numRange(B x, i32 mode, char* desc, i64 min, i64 max) { // doesn't consume; assumes non-array has already been checked for; if min==max, doesn't check range static NOINLINE void ffi_numRange(B x, i32 mode, char* desc, i64 min, i64 max) { // doesn't consume; assumes non-array has already been checked for; if min==max, doesn't check range
if (IA(x)==0) return; if (IA(x)==0) return;
u8 xe = TI(x,elType);
i64 emax;
switch (xe) {
case el_i8: emax=I8_MAX; goto echk;
case el_i16: emax=I16_MAX; goto echk;
case el_i32: emax=I32_MAX; goto echk;
echk:;
i64 emin = -emax-1;
if (emin>=min && emax<=max) return;
// fallthrough, incl. default
}
char* ref = mode==1? "&" : mode==0? "*" : ":"; char* ref = mode==1? "&" : mode==0? "*" : ":";
B nonNum = nonNumber(x); B nonNum = nonNumber(x);
if (nonNum.u!=m_f64(0).u) thrF("FFI: Array provided for %S%S contained %S", ref, desc, genericDesc(nonNum)); if (nonNum.u!=m_f64(0).u) thrF("FFI: Array provided for %S%S contained %S", ref, desc, genericDesc(nonNum));
if (min==max) return; if (min==max) return;
u8 xe = TI(x,elType);
incG(x); incG(x);
if (!elNum(xe)) { if (!elNum(xe)) {
x = taga(cpyF64Arr(x)); x = taga(cpyF64Arr(x));
xe = TI(x,elType); xe = el_f64;
}
i64 emax;
switch (xe) {
case el_i8: emax=I8_MAX; goto echk;
case el_i16: emax=I16_MAX; goto echk;
case el_i32: emax=I32_MAX; goto echk;
echk:;
i64 emin = -emax-1;
if (emin>=min && emax<=max) goto ok;
default:; // fallthrough
} }
i64 buf[2]; i64 buf[2];
if (!getRange_fns[xe](tyany_ptr(x), buf, IA(x))) thrF("FFI: Array provided for %S%S contained non-integer", ref, desc); if (!getRange_fns[xe](tyany_ptr(x), buf, IA(x))) thrF("FFI: Array provided for %S%S contained non-integer", ref, desc);
if (buf[0]<min) thrF("FFI: Array provided for %S%S contained %l", ref, desc, buf[0]); if (buf[0]<min) thrF("FFI: Array provided for %S%S contained %l", ref, desc, buf[0]);
if (buf[1]>max) thrF("FFI: Array provided for %S%S contained %l", ref, desc, buf[1]); if (buf[1]>max) thrF("FFI: Array provided for %S%S contained %l", ref, desc, buf[1]);
ok:;
decG(x); decG(x);
} }
static bool elChrOk(B x, u64 max) { static bool elChrOk(B x, u64 max) {