@@ -179,6 +179,8 @@ struct Buffer {
179179 bytes : Box < [ u8 ] > ,
180180 read_mark : usize ,
181181 write_mark : usize ,
182+ /// Co-operatively used to track if decoding progress was written to the buffer, not `out`.
183+ pub ( crate ) reconstructed_another_code : bool ,
182184}
183185
184186/// Mask for indexing into fixed-size arrays. Since MAX_ENTRIES = 4096 = 2^12,
@@ -795,6 +797,9 @@ impl<C: CodeBuffer, CgC: CodegenConstants> Stateful for DecodeState<C, CgC> {
795797 // The status, which is written to on an invalid code.
796798 let mut status = Ok ( LzwStatus :: Ok ) ;
797799
800+ // Reset if a value was restored into the buffer.
801+ self . buffer . reconstructed_another_code = false ;
802+
798803 match self . last . take ( ) {
799804 // No last state? This is the first code after a reset?
800805 None => {
@@ -1161,8 +1166,11 @@ impl<C: CodeBuffer, CgC: CodegenConstants> Stateful for DecodeState<C, CgC> {
11611166 }
11621167
11631168 // Ensure we don't indicate that no progress was made if we read some bytes from the input
1164- // (which is progress).
1165- if o_in > inp. len ( ) {
1169+ // (which is progress). The field `new_value` will have been written if any reconstruction
1170+ // was buffered. This is mostly a fail-safe in case the above loop does nothing else but
1171+ // buffer the value, without consuming any of the bytes. (It really should right now but
1172+ // that may change and it's not structurally required to).
1173+ if o_in > inp. len ( ) || self . buffer . reconstructed_another_code {
11661174 if let Ok ( LzwStatus :: NoProgress ) = status {
11671175 status = Ok ( LzwStatus :: Ok ) ;
11681176 }
@@ -1403,6 +1411,7 @@ impl Buffer {
14031411 bytes : vec ! [ 0 ; MAX_ENTRIES ] . into_boxed_slice ( ) ,
14041412 read_mark : 0 ,
14051413 write_mark : 0 ,
1414+ reconstructed_another_code : false ,
14061415 }
14071416 }
14081417
@@ -1412,6 +1421,7 @@ impl Buffer {
14121421 /// with the reconstruction of `B`.
14131422 fn fill_cscsc ( & mut self ) -> u8 {
14141423 self . bytes [ self . write_mark ] = self . bytes [ 0 ] ;
1424+ self . reconstructed_another_code = true ;
14151425 self . write_mark += 1 ;
14161426 self . read_mark = 0 ;
14171427 self . bytes [ 0 ]
@@ -1425,6 +1435,7 @@ impl Buffer {
14251435 let mut memory = core:: mem:: replace ( & mut self . bytes , Box :: default ( ) ) ;
14261436
14271437 let out = & mut memory[ ..usize:: from ( depth) ] ;
1438+ self . reconstructed_another_code = true ;
14281439 let last = table. reconstruct ( code, out) ;
14291440
14301441 self . bytes = memory;
0 commit comments