more fixes for overflow checking

This commit is contained in:
dzaima 2023-04-17 13:07:50 +03:00
parent c2754e0fff
commit c57e0ba135
3 changed files with 22 additions and 17 deletions

View File

@ -73,24 +73,26 @@ B ud_c1(B t, B x) {
usz sh[xia]; // stack allocation of rank items
i32 pos[xia];
usz ria = 1;
bool bad=false, good=false;
for (usz i = xia; i--; ) {
usz c = o2s(GetU(x, i));
if (c > I32_MAX) thrM("↕: Result too large");
good = c==0;
bad|= c > I32_MAX;
sh[i] = c;
if (mulOn(ria, c)) thrM("↕: Result too large");
bad|= mulOn(ria, c);
}
if (bad && !good) thrM("↕: Result too large");
decG(x);
Arr* r = m_fillarr0p(ria);
B* rp = fillarr_ptr(r);
ud_rec(rp, 0, xia, pos, sh);
usz* rsh = arr_shAlloc(r, xia);
if (rsh) shcpy(rsh, sh, xia);
if (ria) fillarr_setFill(r, inc(rp[0]));
else {
if (ria) {
B* rp = fillarr_ptr(r);
ud_rec(rp, 0, xia, pos, sh);
fillarr_setFill(r, incG(rp[0]));
} else {
i32* fp;
fillarr_setFill(r, m_i32arrv(&fp, xia));
for (usz i = 0; i < xia; i++) fp[i] = 0;

View File

@ -495,8 +495,7 @@ NOINLINE B takedrop_highrank(bool take, B w, B x) {
usz ria = 1;
bool anyFill = false;
i64 cellStart = -1; // axis from which whole cells can be copied
bool overflow = false;
bool anyZero = false;
bool bad=false, good=false;
for (usz i = 0; i < rr; i++) {
i64 cw = i<wia? o2i64(GetU(w, i)) : take? xsh[i] : 0;
u64 cwa = cw<0? -cw : cw;
@ -507,13 +506,11 @@ NOINLINE B takedrop_highrank(bool take, B w, B x) {
if (c!=xshc) cellStart = i;
anyFill|= c>xshc;
rsh->a[i] = c;
if (c==0) anyZero = true;
if (mulOn(ria, c)) overflow = true;
}
if (overflow) {
if (!anyZero) thrOOM();
ria = 0;
good|= c==0;
bad|= mulOn(ria, c);
}
if (bad && !good) thrOOM();
CHECK_IA(ria, 8);
if (cellStart<=0) {

View File

@ -353,7 +353,13 @@ B rand_range_c2(B t, B w, B x) {
if (RNK(w) > 1) thrM("(rand).Range: 𝕨 must be a valid shape");
SGetU(w);
usz wia = IA(w);
for (u64 i = 0; i < wia; i++) mulOn(am, o2s(GetU(w, i)));
bool bad=false, good=false;
for (u64 i = 0; i < wia; i++) {
usz c = o2s(GetU(w, i));
bad|= mulOn(am, c);
good|= c==0;
}
if (bad && !good) thrOOM();
} else {
am = o2s(w);
}