From 9ca48a55bc0e7983cd8dc8b665e0db07a394f522 Mon Sep 17 00:00:00 2001 From: dzaima Date: Thu, 26 Sep 2024 03:09:12 +0300 Subject: [PATCH] better range checking --- src/ffi.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/ffi.c b/src/ffi.c index c24cdf20..bffeb9db 100644 --- a/src/ffi.c +++ b/src/ffi.c @@ -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 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? "*" : ":"; 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 (min==max) return; - u8 xe = TI(x,elType); + incG(x); if (!elNum(xe)) { x = taga(cpyF64Arr(x)); - 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) goto ok; - default:; // fallthrough + xe = el_f64; } 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 (buf[0]max) thrF("FFI: Array provided for %S%S contained %l", ref, desc, buf[1]); - - ok:; decG(x); } static bool elChrOk(B x, u64 max) {