diff --git a/gen/c/gif.c b/gen/c/gif.c index 69ce5a1b..69d6ef2d 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -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; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 9036b910..349362c7 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -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 { diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index 365625ba..71af4e07 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -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)