Tighten the Huffman n_bits invariant.

This commit is contained in:
Nigel Tao committed 2017-11-07 09:30:16 +11:00
1 parent 30ce73ad26
commit 848d66ffdf
4 files changed
+23 -14

No files matched your search

+2 -1
View File
@@ -218,10 +218,11 @@ pri func flate_decoder.decode_uncompressed?(dst writer1, src reader1)() {
// TODO: make this "if" into a function invariant?
//
// Ditto for decode_huffman_slow and decode_huffman_fast.
if this.n_bits >= 8 {
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
return error "internal error: inconsistent n_bits"
}
this.n_bits = 0
this.bits = 0
var length u32 = in.src.read_u32le?()
if (length.low_bits(n:16) + length.high_bits(n:16)) != 0xFFFF {
+6 -4
View File
@@ -20,7 +20,7 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() {
// decode_huffman_*.puffs files as small as possible, while retaining both
// correctness and performance.
if this.n_bits >= 8 {
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
return error "internal error: inconsistent n_bits"
}
@@ -321,15 +321,17 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() {
//
// TODO: have the compiler enforce (via some explicit syntax) that this
// function is ProvenNotToSuspend?
while n_bits >= 8 {
while n_bits >= 8,
post n_bits < 8,
{
n_bits -= 8
in.src.unread_u8?()
}
this.bits = bits
this.bits = bits & (((1 as u32) << n_bits) - 1)
this.n_bits = n_bits
if this.n_bits >= 8 {
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
return error "internal error: inconsistent n_bits"
}
}
+3 -3
View File
@@ -18,7 +18,7 @@ pri func flate_decoder.decode_huffman_slow?(dst writer1, src reader1)() {
// decode_huffman_*.puffs files as small as possible, while retaining both
// correctness and performance.
if this.n_bits >= 8 {
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
return error "internal error: inconsistent n_bits"
}
@@ -254,12 +254,12 @@ pri func flate_decoder.decode_huffman_slow?(dst writer1, src reader1)() {
}
}
// TODO: "assert n_bits < 8"?
// TODO: "assert n_bits < 8"? What about (bits >> n_bits)?
this.bits = bits
this.n_bits = n_bits
if this.n_bits >= 8 {
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) != 0) {
return error "internal error: inconsistent n_bits"
}
}