Bounds check "x as T" and "x << y".

This commit is contained in:
Nigel Tao committed 2017-06-06 17:45:17 +10:00
1 parent 45cecca79a
commit 6e7aa31aef
3 files changed
+23 -5

No files matched your search

+1 -1
View File
@@ -221,7 +221,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
goto cleanup0;
}
uint8_t t_0 = a_src->ptr[a_src->ri++];
v_bits |= ((uint32_t)(t_0));
v_bits |= (((uint32_t)(t_0)) << v_n_bits);
v_n_bits += 8;
}
v_bits >>= v_width;
+21 -3
View File
@@ -427,9 +427,17 @@ func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error
if err != nil {
return nil, nil, err
}
// TODO.
_, _ = lMin, lMax
return zero, zero, nil
rMin, rMax, err := q.bcheckTypeExpr(n.RHS().TypeExpr())
if err != nil {
return nil, nil, err
}
nMin, nMax := max(lMin, rMin), min(lMax, rMax)
// TODO: check if nMin > nMax.
//
// TODO: should it be a compile time error if "x as T" can narrow
// the range? Do we want a separate "~as" operator that ignores
// overflow?
return nMin, nMax, nil
}
return q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth)
case t.FlagsAssociativeOp:
@@ -539,6 +547,16 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
case t.KeyXBinarySlash:
case t.KeyXBinaryShiftL:
if lMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.idMap))
}
if rMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.idMap))
}
if rMax.Cmp(ffff) > 0 {
return nil, nil, fmt.Errorf("check: shift %q out of range", rhs.String(q.idMap))
}
return big.NewInt(0).Lsh(lMin, uint(rMin.Uint64())), big.NewInt(0).Lsh(lMax, uint(rMax.Uint64())), nil
case t.KeyXBinaryShiftR:
if lMin.Cmp(zero) < 0 {
+1 -1
View File
@@ -24,7 +24,7 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
post n_bits >= width,
{
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
bits |= in.src.read_u8?() as u32 // TODO: read_u8 should take src_final.
bits |= (in.src.read_u8?() as u32) << n_bits // TODO: read_u8 should take src_final.
n_bits += 8
}
// TODO: var code u32[..4095] = bits.low_bits(width)