mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 00:47:28 +01:00
Bounds check "x >> y".
This commit is contained in:
1 parent
51da37c661
commit
5a0aea66a3
4 files changed
+29
No files matched your search
@@ -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:
|
||||
|
||||
|
||||
@@ -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},
|
||||
|
||||
Reference in new issue
Block a user