Bounds check "x >> y".

This commit is contained in:
Nigel Tao committed 2017-06-02 10:57:56 +10:00
1 parent 51da37c661
commit 5a0aea66a3
4 files changed
+29

No files matched your search

+1
View File
@@ -167,6 +167,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) {
v_bits |= 0;
v_n_bits += 8;
}
v_bits >>= v_width;
goto label_0_break;
v_n_bits = 0;
}
+20
View File
@@ -28,6 +28,8 @@ var (
one = big.NewInt(1)
ffff = big.NewInt(0xFFFF)
maxIntBits = big.NewInt(t.MaxIntBits)
zeroExpr = a.NewExpr(a.FlagsTypeChecked, 0, t.IDZero, nil, nil, nil, nil)
)
@@ -511,8 +513,25 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
return zero, zero, nil
}
case t.KeyXBinarySlash:
case t.KeyXBinaryShiftL:
case t.KeyXBinaryShiftR:
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 rMin.Cmp(maxIntBits) >= 0 {
return zero, zero, nil
}
nMax := big.NewInt(0).Rsh(lMax, uint(rMin.Uint64()))
if rMax.Cmp(maxIntBits) >= 0 {
return zero, nMax, nil
}
return big.NewInt(0).Rsh(lMin, uint(rMax.Uint64())), nMax, nil
case t.KeyXBinaryAmp, t.KeyXBinaryPipe:
// TODO: should type-checking ensure that bitwise ops only apply to
// *unsigned* integer types?
@@ -537,6 +556,7 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
// Return [0, z rounded up to the next power-of-2-minus-1]. This is
// conservative, but works fine in practice.
return zero, bitMask(z.BitLen()), nil
case t.KeyXBinaryAmpHat:
case t.KeyXBinaryHat:
+6
View File
@@ -3,6 +3,10 @@
package token
// MaxIntBits is the largest size (in bits) of the i8, u8, i16, u16, etc.
// integer types.
const MaxIntBits = 64
// Flags are the low 16 bits of an ID. For example, they signify whether a
// token is an operator, an identifier, etc.
//
@@ -471,6 +475,8 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyTrue: {"true", IDTrue},
KeyZero: {"0", IDZero},
// Change MaxIntBits if a future update adds an i128 or u128 type.
KeyI8: {"i8", IDI8},
KeyI16: {"i16", IDI16},
KeyI32: {"i32", IDI32},
+2
View File
@@ -29,6 +29,8 @@ pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)()
bits |= 0 // TODO.
n_bits += 8
}
// TODO: var code u32[..4095] = bits.low_bits(width)
bits >>= width
// TODO: implement.
break