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

+12 -6
View File
@@ -1245,11 +1245,13 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_uncompressed(
switch (coro_susp_point) {
PUFFS_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)) {
status = PUFFS_FLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS;
goto exit;
}
self->private_impl.f_n_bits = 0;
self->private_impl.f_bits = 0;
{
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
uint32_t t_1;
@@ -2013,7 +2015,8 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
b_rend_src = b_rptr_src + len;
}
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)) {
status = PUFFS_FLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS;
goto exit;
}
@@ -2274,9 +2277,10 @@ label_0_break:;
}
b_rptr_src--;
}
self->private_impl.f_bits = v_bits;
self->private_impl.f_bits = (v_bits & ((((uint32_t)(1)) << v_n_bits) - 1));
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)) {
status = PUFFS_FLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS;
goto exit;
}
@@ -2384,7 +2388,8 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_slow(
switch (coro_susp_point) {
PUFFS_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)) {
status = PUFFS_FLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS;
goto exit;
}
@@ -2684,7 +2689,8 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_slow(
label_0_break:;
self->private_impl.f_bits = v_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)) {
status = PUFFS_FLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS;
goto exit;
}
+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"
}
}