git: 03b4f232354c - stable/14 - uncompress: Avoid reading an extra byte

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 11 Oct 2024 22:11:32 UTC
The branch stable/14 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=03b4f232354ce68fe1cb462038654f6527f63579

commit 03b4f232354ce68fe1cb462038654f6527f63579
Author:     David Jones <drj@ravenbrook.com>
AuthorDate: 2024-10-11 15:49:17 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-10-11 22:11:02 +0000

    uncompress: Avoid reading an extra byte
    
    When reading the next code in a stream, avoid reading an extra byte if
    we're going to throw it away. When there's no more bits to extract from
    the stream, bits will be 0 and we'll mask the read byte with 0 anyway.
    At worst, this will avoid reading one past the end of gbuf array (which
    is not possible in well formed streams).
    
    PR: 127912
    Reviewed by:    emaste
    Differential Revision:  https://reviews.freebsd.org/D47041
    
    (cherry picked from commit 818c7b769a4f7d3c8fecc4cf491f4e22ef816eba)
---
 usr.bin/compress/zopen.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/usr.bin/compress/zopen.c b/usr.bin/compress/zopen.c
index a5b34ef83ecb..0d0900cabfa1 100644
--- a/usr.bin/compress/zopen.c
+++ b/usr.bin/compress/zopen.c
@@ -623,7 +623,8 @@ getcode(struct s_zstate *zs)
 	}
 
 	/* High order bits. */
-	gcode |= (*bp & rmask[bits]) << r_off;
+	if (bits > 0)
+		gcode |= (*bp & rmask[bits]) << r_off;
 	roffset += n_bits;
 
 	return (gcode);