mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 00:52:18 +01:00
For "x shift y", check that y < (x's bit-size)
This commit is contained in:
1 parent
65c2f4971f
commit
705a84723a
6 files changed
+65
-36
No files matched your search
+34
-24
@@ -27,6 +27,13 @@ import (
|
|||||||
|
|
||||||
type bounds = interval.IntRange
|
type bounds = interval.IntRange
|
||||||
|
|
||||||
|
var numShiftBounds = [...]bounds{
|
||||||
|
t.IDU8: {zero, big.NewInt(7)},
|
||||||
|
t.IDU16: {zero, big.NewInt(15)},
|
||||||
|
t.IDU32: {zero, big.NewInt(31)},
|
||||||
|
t.IDU64: {zero, big.NewInt(63)},
|
||||||
|
}
|
||||||
|
|
||||||
var numTypeBounds = [...]bounds{
|
var numTypeBounds = [...]bounds{
|
||||||
t.IDI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
|
t.IDI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
|
||||||
t.IDI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
|
t.IDI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
|
||||||
@@ -1391,34 +1398,37 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lb bounds, rhs *a.Ex
|
|||||||
big.NewInt(0).Sub(rb[1], one),
|
big.NewInt(0).Sub(rb[1], one),
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
case t.IDXBinaryShiftL, t.IDXBinaryTildeModShiftL:
|
case t.IDXBinaryShiftL, t.IDXBinaryTildeModShiftL, t.IDXBinaryShiftR:
|
||||||
if lb[0].Sign() < 0 {
|
shiftBounds := bounds{}
|
||||||
return bounds{}, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm))
|
typeBounds := bounds{}
|
||||||
}
|
if lTyp := lhs.MType(); lTyp.IsNumType() {
|
||||||
if rb[0].Sign() < 0 {
|
id := int(lTyp.QID()[1])
|
||||||
return bounds{}, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm))
|
if id < len(numShiftBounds) {
|
||||||
}
|
shiftBounds = numShiftBounds[id]
|
||||||
if rb[1].Cmp(ffff) > 0 {
|
}
|
||||||
return bounds{}, fmt.Errorf("check: shift %q out of range", rhs.Str(q.tm))
|
if id < len(numTypeBounds) {
|
||||||
}
|
typeBounds = numTypeBounds[id]
|
||||||
nb, _ := lb.Lsh(rb)
|
|
||||||
if op == t.IDXBinaryTildeModShiftL {
|
|
||||||
if qid := lhs.MType().QID(); qid[0] == t.IDBase {
|
|
||||||
b := numTypeBounds[qid[1]]
|
|
||||||
nb[1] = min(nb[1], b[1])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nb, nil
|
if shiftBounds[0] == nil {
|
||||||
|
return bounds{}, fmt.Errorf("check: shift op argument %q of type %q does not have unsigned integer type",
|
||||||
|
lhs.Str(q.tm), lhs.MType().Str(q.tm))
|
||||||
|
} else if !shiftBounds.ContainsIntRange(rb) {
|
||||||
|
return bounds{}, fmt.Errorf("check: shift op argument %q is outside the range %s", rhs.Str(q.tm), shiftBounds)
|
||||||
|
}
|
||||||
|
|
||||||
case t.IDXBinaryShiftR:
|
switch op {
|
||||||
if lb[0].Sign() < 0 {
|
case t.IDXBinaryShiftL:
|
||||||
return bounds{}, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm))
|
nb, _ := lb.Lsh(rb)
|
||||||
|
return nb, nil
|
||||||
|
case t.IDXBinaryTildeModShiftL:
|
||||||
|
nb, _ := lb.Lsh(rb)
|
||||||
|
nb[1] = min(nb[1], typeBounds[1])
|
||||||
|
return nb, nil
|
||||||
|
case t.IDXBinaryShiftR:
|
||||||
|
nb, _ := lb.Rsh(rb)
|
||||||
|
return nb, nil
|
||||||
}
|
}
|
||||||
if rb[0].Sign() < 0 {
|
|
||||||
return bounds{}, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm))
|
|
||||||
}
|
|
||||||
nb, _ := lb.Rsh(rb)
|
|
||||||
return nb, nil
|
|
||||||
|
|
||||||
case t.IDXBinaryAmp, t.IDXBinaryPipe, t.IDXBinaryHat:
|
case t.IDXBinaryAmp, t.IDXBinaryPipe, t.IDXBinaryHat:
|
||||||
// TODO: should type-checking ensure that bitwise ops only apply to
|
// TODO: should type-checking ensure that bitwise ops only apply to
|
||||||
|
|||||||
@@ -196,12 +196,28 @@ func (x IntRange) ContainsZero() bool {
|
|||||||
(x[1] == nil || x[1].Sign() >= 0)
|
(x[1] == nil || x[1].Sign() >= 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contains returns whether x contains i.
|
// ContainsInt returns whether x contains i.
|
||||||
func (x IntRange) Contains(i *big.Int) bool {
|
func (x IntRange) ContainsInt(i *big.Int) bool {
|
||||||
return (x[0] == nil || x[0].Cmp(i) <= 0) &&
|
return (x[0] == nil || x[0].Cmp(i) <= 0) &&
|
||||||
(x[1] == nil || x[1].Cmp(i) >= 0)
|
(x[1] == nil || x[1].Cmp(i) >= 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ContainsIntRange returns whether x contains every element of y.
|
||||||
|
//
|
||||||
|
// It returns true if y is empty.
|
||||||
|
func (x IntRange) ContainsIntRange(y IntRange) bool {
|
||||||
|
if y.Empty() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (x[0] != nil) && (y[0] == nil || x[0].Cmp(y[0]) > 0) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (x[1] != nil) && (y[1] == nil || x[1].Cmp(y[1]) < 0) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// Eq returns whether x equals y.
|
// Eq returns whether x equals y.
|
||||||
func (x IntRange) Eq(y IntRange) bool {
|
func (x IntRange) Eq(y IntRange) bool {
|
||||||
if xe, ye := x.Empty(), y.Empty(); xe || ye {
|
if xe, ye := x.Empty(), y.Empty(); xe || ye {
|
||||||
@@ -602,10 +618,10 @@ func (x IntRange) Or(y IntRange) (z IntRange, ok bool) {
|
|||||||
// The smaller of those is also a sufficient bound if that smaller
|
// The smaller of those is also a sufficient bound if that smaller
|
||||||
// value is contained in the other interval. For example, if both xx
|
// value is contained in the other interval. For example, if both xx
|
||||||
// and yy can be x[0], then (x[0] | x[0]) is simply x[0].
|
// and yy can be x[0], then (x[0] | x[0]) is simply x[0].
|
||||||
if x.Contains(y[0]) {
|
if x.ContainsInt(y[0]) {
|
||||||
return IntRange{y[0], nil}, true
|
return IntRange{y[0], nil}, true
|
||||||
}
|
}
|
||||||
if y.Contains(x[0]) {
|
if y.ContainsInt(x[0]) {
|
||||||
return IntRange{x[0], nil}, true
|
return IntRange{x[0], nil}, true
|
||||||
}
|
}
|
||||||
if x[1] == nil && y[1] == nil {
|
if x[1] == nil && y[1] == nil {
|
||||||
|
|||||||
@@ -5872,7 +5872,8 @@ wuffs_deflate__decoder__decode_uncompressed(wuffs_deflate__decoder* self,
|
|||||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||||
|
|
||||||
if ((self->private_impl.f_n_bits >= 8) ||
|
if ((self->private_impl.f_n_bits >= 8) ||
|
||||||
((self->private_impl.f_bits >> self->private_impl.f_n_bits) != 0)) {
|
((self->private_impl.f_bits >> (self->private_impl.f_n_bits & 7)) !=
|
||||||
|
0)) {
|
||||||
status = wuffs_deflate__error__internal_error_inconsistent_n_bits;
|
status = wuffs_deflate__error__internal_error_inconsistent_n_bits;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
@@ -6545,7 +6546,7 @@ wuffs_deflate__decoder__decode_huffman_fast(wuffs_deflate__decoder* self,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((self->private_impl.f_n_bits >= 8) ||
|
if ((self->private_impl.f_n_bits >= 8) ||
|
||||||
((self->private_impl.f_bits >> self->private_impl.f_n_bits) != 0)) {
|
((self->private_impl.f_bits >> (self->private_impl.f_n_bits & 7)) != 0)) {
|
||||||
status = wuffs_deflate__error__internal_error_inconsistent_n_bits;
|
status = wuffs_deflate__error__internal_error_inconsistent_n_bits;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
@@ -6886,7 +6887,8 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self,
|
|||||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||||
|
|
||||||
if ((self->private_impl.f_n_bits >= 8) ||
|
if ((self->private_impl.f_n_bits >= 8) ||
|
||||||
((self->private_impl.f_bits >> self->private_impl.f_n_bits) != 0)) {
|
((self->private_impl.f_bits >> (self->private_impl.f_n_bits & 7)) !=
|
||||||
|
0)) {
|
||||||
status = wuffs_deflate__error__internal_error_inconsistent_n_bits;
|
status = wuffs_deflate__error__internal_error_inconsistent_n_bits;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
@@ -7186,7 +7188,8 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self,
|
|||||||
self->private_impl.f_bits = v_bits;
|
self->private_impl.f_bits = v_bits;
|
||||||
self->private_impl.f_n_bits = v_n_bits;
|
self->private_impl.f_n_bits = v_n_bits;
|
||||||
if ((self->private_impl.f_n_bits >= 8) ||
|
if ((self->private_impl.f_n_bits >= 8) ||
|
||||||
((self->private_impl.f_bits >> self->private_impl.f_n_bits) != 0)) {
|
((self->private_impl.f_bits >> (self->private_impl.f_n_bits & 7)) !=
|
||||||
|
0)) {
|
||||||
status = wuffs_deflate__error__internal_error_inconsistent_n_bits;
|
status = wuffs_deflate__error__internal_error_inconsistent_n_bits;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -242,7 +242,7 @@ pri func decoder.decode_uncompressed?(dst base.io_writer, src base.io_reader) {
|
|||||||
// TODO: make this "if" into a function invariant?
|
// TODO: make this "if" into a function invariant?
|
||||||
//
|
//
|
||||||
// Ditto for decode_huffman_slow and decode_huffman_fast.
|
// Ditto for decode_huffman_slow and decode_huffman_fast.
|
||||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
|
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) != 0) {
|
||||||
return "?internal error: inconsistent n_bits"
|
return "?internal error: inconsistent n_bits"
|
||||||
}
|
}
|
||||||
this.n_bits = 0
|
this.n_bits = 0
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ pri func decoder.decode_huffman_fast!(dst base.io_writer, src base.io_reader) ba
|
|||||||
var hlen base.u32[..0x7FFF]
|
var hlen base.u32[..0x7FFF]
|
||||||
var hdist base.u32
|
var hdist base.u32
|
||||||
|
|
||||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
|
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) != 0) {
|
||||||
return "?internal error: inconsistent n_bits"
|
return "?internal error: inconsistent n_bits"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) {
|
|||||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||||
// correctness and performance.
|
// correctness and performance.
|
||||||
|
|
||||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
|
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) != 0) {
|
||||||
return "?internal error: inconsistent n_bits"
|
return "?internal error: inconsistent n_bits"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +285,7 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) {
|
|||||||
this.bits = bits
|
this.bits = bits
|
||||||
this.n_bits = n_bits
|
this.n_bits = n_bits
|
||||||
|
|
||||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
|
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) != 0) {
|
||||||
return "?internal error: inconsistent n_bits"
|
return "?internal error: inconsistent n_bits"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user