diff --git a/src/core/fillarr.h b/src/core/fillarr.h index 4a6a11b8..2827b752 100644 --- a/src/core/fillarr.h +++ b/src/core/fillarr.h @@ -60,6 +60,7 @@ static B getFillE(B x) { // errors if there's no fill static Arr* m_fillarrp(usz ia) { + CHECK_IA(ia, sizeof(B)); return m_arr(fsizeof(FillArr,a,B,ia), t_fillarr, ia); } static void fillarr_setFill(Arr* x, B fill) { assert(x->type==t_fillarr); ((FillArr*)x)->fill = fill; } // consumes fill diff --git a/src/core/harr.h b/src/core/harr.h index c1fc93a0..e6248fa7 100644 --- a/src/core/harr.h +++ b/src/core/harr.h @@ -29,6 +29,7 @@ NOINLINE void harr_pfree(B x, usz am); // am - item after last written #define HARR_O(N) N##_v #define HARR_I(N) N##_i static HArr_p m_harr_impl(usz ia) { + CHECK_IA(ia, sizeof(B)); HArr* r = m_arr(fsizeof(HArr,a,B,ia), t_harrPartial, ia); r->ia = 0; // don't need to initialize r->sh or rank at all i guess @@ -75,16 +76,20 @@ void harr_abandon_impl(HArr* p); #define m_harr0c(X) ({ B x_ = (X); usz n_ = a(x_)->ia; HArr_p r_ = m_harrUc(x_); for(usz i=0;iia), t_harr, a(x)->ia); + usz ia = a(x)->ia; + CHECK_IA(ia, sizeof(B)); + HArr* r = m_arr(fsizeof(HArr,a,B,ia), t_harr, ia); arr_shCopy((Arr*)r, x); return harrP_parts(r); } static HArr_p m_harrUp(usz ia) { + CHECK_IA(ia, sizeof(B)); HArr* r = m_arr(fsizeof(HArr,a,B,ia), t_harr, ia); return harrP_parts(r); } diff --git a/src/core/tyarrTemplate.h b/src/core/tyarrTemplate.h index a5029ece..fedc3225 100644 --- a/src/core/tyarrTemplate.h +++ b/src/core/tyarrTemplate.h @@ -5,18 +5,22 @@ typedef TyArr JOIN(TU,Arr); typedef TP(,) JOIN(TU,Atom); static B TP(m_,arrv) (TEl** p, usz ia) { + CHECK_IA(ia, sizeof(TEl)); TyArr* r = m_arr(TYARR_SZ2(TU,ia), T_ARR, ia); arr_shVec((Arr*)r); *p = (TEl*)r->a; return taga(r); } static B TP(m_,arrc) (TEl** p, B x) { assert(isArr(x)); - TyArr* r = m_arr(TYARR_SZ2(TU,a(x)->ia), T_ARR, a(x)->ia); + usz ia = a(x)->ia; + CHECK_IA(ia, sizeof(TEl)); + TyArr* r = m_arr(TYARR_SZ2(TU,ia), T_ARR, a(x)->ia); *p = (TEl*)r->a; arr_shCopy((Arr*)r, x); return taga(r); } static Arr* TP(m_,arrp) (TEl** p, usz ia) { + CHECK_IA(ia, sizeof(TEl)); TyArr* r = m_arr(TYARR_SZ2(TU,ia), T_ARR, ia); *p = (TEl*)r->a; return (Arr*)r; diff --git a/src/h.h b/src/h.h index 69e93d37..2b2ff044 100644 --- a/src/h.h +++ b/src/h.h @@ -137,10 +137,19 @@ typedef double f64; #define JOIN0(A,B) A##B #define JOIN(A,B) JOIN0(A,B) -typedef u32 usz; -#define USZ_MAX ((u32)((1LL<<32)-1)) -// typedef u64 usz; -// #define USZ_MAX ((u64)(1ULL<<48)) +#if USZ_64 + typedef u64 usz; + #define USZ_MAX ((u64)(1ULL<<48)) + #define CHECK_IA(IA,W) if(IA>USZ_MAX) thrOOM() +#else + typedef u32 usz; + #define USZ_MAX ((u32)((1LL<<32)-1)) + #define CHECK_IA(IA,W) if (IA > ((1LL<<31)/W - 1000)) thrOOM() +#endif +#if UNSAFE_SIZES + #undef CHECK_IA + #define CHECK_IA(IA,W) +#endif typedef u8 ur; #define UR_MAX 255