mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 16:52:18 +01:00
Bounds check "x | y".
This commit is contained in:
1 parent
ca78c343cb
commit
509e6b48a4
3 files changed
+84
-3
No files matched your search
+45
-2
@@ -48,6 +48,29 @@ func neg(i *big.Int) *big.Int {
|
|||||||
return big.NewInt(0).Neg(i)
|
return big.NewInt(0).Neg(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func roundUpToPowerOf2Minus1(i *big.Int) *big.Int {
|
||||||
|
if i.Cmp(zero) < 0 {
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
||||||
|
bitLen := i.BitLen()
|
||||||
|
switch bitLen {
|
||||||
|
case 0:
|
||||||
|
return zero
|
||||||
|
case 1:
|
||||||
|
return one
|
||||||
|
case 8:
|
||||||
|
return numTypeBounds[t.KeyU8][1]
|
||||||
|
case 16:
|
||||||
|
return numTypeBounds[t.KeyU16][1]
|
||||||
|
case 32:
|
||||||
|
return numTypeBounds[t.KeyU32][1]
|
||||||
|
case 64:
|
||||||
|
return numTypeBounds[t.KeyU64][1]
|
||||||
|
}
|
||||||
|
z := big.NewInt(0).Lsh(one, uint(bitLen))
|
||||||
|
return z.Sub(z, one)
|
||||||
|
}
|
||||||
|
|
||||||
func invert(m *t.IDMap, n *a.Expr) (*a.Expr, error) {
|
func invert(m *t.IDMap, n *a.Expr) (*a.Expr, error) {
|
||||||
if !n.MType().IsBool() {
|
if !n.MType().IsBool() {
|
||||||
return nil, fmt.Errorf("check: invert(%q) called on non-bool-typed expression", n.String(m))
|
return nil, fmt.Errorf("check: invert(%q) called on non-bool-typed expression", n.String(m))
|
||||||
@@ -423,9 +446,29 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
|
|||||||
case t.KeyXBinarySlash:
|
case t.KeyXBinarySlash:
|
||||||
case t.KeyXBinaryShiftL:
|
case t.KeyXBinaryShiftL:
|
||||||
case t.KeyXBinaryShiftR:
|
case t.KeyXBinaryShiftR:
|
||||||
case t.KeyXBinaryAmp:
|
case t.KeyXBinaryAmp, t.KeyXBinaryPipe:
|
||||||
|
// TODO: should type-checking ensure that bitwise ops only apply to
|
||||||
|
// *unsigned* integer types?
|
||||||
|
if lMin.Cmp(zero) < 0 {
|
||||||
|
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.String(q.idMap))
|
||||||
|
}
|
||||||
|
if lMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
|
||||||
|
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.String(q.idMap))
|
||||||
|
}
|
||||||
|
if rMin.Cmp(zero) < 0 {
|
||||||
|
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.String(q.idMap))
|
||||||
|
}
|
||||||
|
if rMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
|
||||||
|
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", rhs.String(q.idMap))
|
||||||
|
}
|
||||||
|
max := lMax
|
||||||
|
if max.Cmp(rMax) < 0 {
|
||||||
|
max = rMax
|
||||||
|
}
|
||||||
|
// Round up max to the next-power-of-2-minus-1, and return [0, max].
|
||||||
|
// This is conservative, but works fine in practice.
|
||||||
|
return zero, roundUpToPowerOf2Minus1(max), nil
|
||||||
case t.KeyXBinaryAmpHat:
|
case t.KeyXBinaryAmpHat:
|
||||||
case t.KeyXBinaryPipe:
|
|
||||||
case t.KeyXBinaryHat:
|
case t.KeyXBinaryHat:
|
||||||
|
|
||||||
case t.KeyXBinaryNotEq, t.KeyXBinaryLessThan, t.KeyXBinaryLessEq, t.KeyXBinaryEqEq,
|
case t.KeyXBinaryNotEq, t.KeyXBinaryLessThan, t.KeyXBinaryLessEq, t.KeyXBinaryEqEq,
|
||||||
|
|||||||
@@ -224,3 +224,41 @@ func TestConstValues(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRoundUpToPowerOf2Minus1(t *testing.T) {
|
||||||
|
testCases := [][2]uint64{
|
||||||
|
{0, 0},
|
||||||
|
{1, 1},
|
||||||
|
{2, 3},
|
||||||
|
{3, 3},
|
||||||
|
{4, 7},
|
||||||
|
{5, 7},
|
||||||
|
{50, 63},
|
||||||
|
{500, 511},
|
||||||
|
{5000, 8191},
|
||||||
|
{50000, 65535},
|
||||||
|
{500000, 524287},
|
||||||
|
{1<<7 - 2, 1<<7 - 1},
|
||||||
|
{1<<7 - 1, 1<<7 - 1},
|
||||||
|
{1<<7 + 0, 1<<8 - 1},
|
||||||
|
{1<<7 + 1, 1<<8 - 1},
|
||||||
|
{1<<8 - 2, 1<<8 - 1},
|
||||||
|
{1<<8 - 1, 1<<8 - 1},
|
||||||
|
{1<<8 + 0, 1<<9 - 1},
|
||||||
|
{1<<8 + 1, 1<<9 - 1},
|
||||||
|
{1<<32 - 2, 1<<32 - 1},
|
||||||
|
{1<<32 - 1, 1<<32 - 1},
|
||||||
|
{1<<32 + 0, 1<<33 - 1},
|
||||||
|
{1<<32 + 1, 1<<33 - 1},
|
||||||
|
{1<<64 - 2, 1<<64 - 1},
|
||||||
|
{1<<64 - 1, 1<<64 - 1},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
got := roundUpToPowerOf2Minus1(big.NewInt(0).SetUint64(tc[0]))
|
||||||
|
want := big.NewInt(0).SetUint64(tc[1])
|
||||||
|
if got.Cmp(want) != 0 {
|
||||||
|
t.Errorf("roundUpToPowerOf2Minus1(%v): got %v, want %v", tc[0], got, tc[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,7 +21,7 @@ pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)()
|
|||||||
post n_bits >= width,
|
post n_bits >= width,
|
||||||
{
|
{
|
||||||
assert n_bits < 12 via "a < b: a < c <= b"(c:width)
|
assert n_bits < 12 via "a < b: a < c <= b"(c:width)
|
||||||
// TODO.
|
bits |= 0 // TODO.
|
||||||
n_bits += 8
|
n_bits += 8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user