diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 423860df..48399997 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -1210,17 +1210,17 @@ func (q *checker) canUndoByte(recv *a.Expr) error { func (q *checker) canLimitedCopyU32FromHistoryFast(recv *a.Expr, args []*a.Node) error { // As per cgen's io-private.h, there are three pre-conditions: - // - n <= this.length() + // - upTo <= this.length() // - distance > 0 // - distance <= this.history_length() if len(args) != 2 { - return fmt.Errorf("check: internal error: inconsistent copy_n_from_history_fast arguments") + return fmt.Errorf("check: internal error: inconsistent limited_copy_u32_from_history_fast arguments") } - n := args[0].AsArg().Value() + upTo := args[0].AsArg().Value() distance := args[1].AsArg().Value() - // Check "n <= this.length()". + // Check "upTo <= this.length()". check0: for { for _, x := range q.facts { @@ -1228,13 +1228,13 @@ check0: continue } - // Check that the LHS is "n as base.u64". + // Check that the LHS is "upTo as base.u64". lhs := x.LHS().AsExpr() if lhs.Operator() != t.IDXBinaryAs { continue } llhs, lrhs := lhs.LHS().AsExpr(), lhs.RHS().AsTypeExpr() - if !llhs.Eq(n) || !lrhs.Eq(typeExprU64) { + if !llhs.Eq(upTo) || !lrhs.Eq(typeExprU64) { continue } @@ -1249,7 +1249,7 @@ check0: break check0 } - return fmt.Errorf("check: could not prove n <= %s.length()", recv.Str(q.tm)) + return fmt.Errorf("check: could not prove (up_to as base.u64) <= %s.length()", recv.Str(q.tm)) } // Check "distance > 0". diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index f53bafe6..3616613f 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -23030,10 +23030,11 @@ wuffs_deflate__decoder__decode_huffman_fast( if (v_length == 0) { goto label__loop__continue; } - if (((uint64_t)((v_dist_minus_1 + 1))) > ((uint64_t)(iop_a_dst - io0_a_dst))) { + if ((((uint64_t)((v_dist_minus_1 + 1))) > ((uint64_t)(iop_a_dst - io0_a_dst))) || (((uint64_t)(v_length)) > ((uint64_t)(io2_a_dst - iop_a_dst)))) { status = wuffs_base__make_status(wuffs_deflate__error__internal_error_inconsistent_distance); goto exit; } + } else { } wuffs_base__io_writer__limited_copy_u32_from_history_fast( &iop_a_dst, io0_a_dst, io2_a_dst, v_length, (v_dist_minus_1 + 1)); diff --git a/std/deflate/decode_huffman_fast.wuffs b/std/deflate/decode_huffman_fast.wuffs index a3d439b7..99f47436 100644 --- a/std/deflate/decode_huffman_fast.wuffs +++ b/std/deflate/decode_huffman_fast.wuffs @@ -216,18 +216,20 @@ pri func decoder.decode_huffman_fast!(dst: base.io_writer, src: base.io_reader) continue.loop } - if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() { + if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or + ((length as base.u64) > args.dst.length()) { return "#internal error: inconsistent distance" } + } else { + assert (length as base.u64) <= 258 + assert args.dst.length() >= 258 + // We can therefore prove: + assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 258) } // Once again, redundant but explicit assertions. - assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length() - assert args.dst.length() >= 258 - - // We can therefore prove: assert (dist_minus_1 + 1) > 0 - assert (length as base.u64) <= 258 - assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 258) + assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length() + assert (length as base.u64) <= args.dst.length() // Copy from args.dst. args.dst.limited_copy_u32_from_history_fast!(