[Scummvm-git-logs] scummvm master -> 5f720aba7c716daa8947e84a766f310536cb6886

bluegr noreply at scummvm.org
Fri Dec 27 20:16:38 UTC 2024


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
5f720aba7c COMMON: Remove undefined behaviours from RNC decoder


Commit: 5f720aba7c716daa8947e84a766f310536cb6886
    https://github.com/scummvm/scummvm/commit/5f720aba7c716daa8947e84a766f310536cb6886
Author: Le Philousophe (lephilousophe at users.noreply.github.com)
Date: 2024-12-27T22:16:35+02:00

Commit Message:
COMMON: Remove undefined behaviours from RNC decoder

Shifting an amount of 16 on a 16 bits variable is UB.
Make the variables larger to not fall inside it and avoid branches.

Changed paths:
    common/compression/rnc_deco.cpp


diff --git a/common/compression/rnc_deco.cpp b/common/compression/rnc_deco.cpp
index 9f3151f92c5..103425d20de 100644
--- a/common/compression/rnc_deco.cpp
+++ b/common/compression/rnc_deco.cpp
@@ -86,17 +86,20 @@ uint16 RncDecoder::crcBlock(const uint8 *block, uint32 size) {
 }
 
 uint16 RncDecoder::inputBits(uint8 amount) {
-	uint16 newBitBuffh = _bitBuffh;
-	uint16 newBitBuffl = _bitBuffl;
+	// Although we are manipulating 16 bits values, use 32 bits variables
+	// This avoids triggering UB when shifting an amount of 0 or 16.
+	uint32 newBitBuffh = _bitBuffh;
+	uint32 newBitBuffl = _bitBuffl;
 	int16 newBitCount = _bitCount;
-	uint16 remBits, returnVal;
+	uint32 remBits;
+	uint16 returnVal;
 
 	returnVal = ((1 << amount) - 1) & newBitBuffl;
 	newBitCount -= amount;
 
 	if (newBitCount < 0) {
 		newBitCount += amount;
-		remBits = (newBitBuffh << (16 - newBitCount));
+		remBits = (newBitBuffh << (16 - newBitCount)) & 0xffff;
 		newBitBuffh >>= newBitCount;
 		newBitBuffl >>= newBitCount;
 		newBitBuffl |= remBits;
@@ -114,7 +117,7 @@ uint16 RncDecoder::inputBits(uint8 amount) {
 		amount -= newBitCount;
 		newBitCount = 16 - amount;
 	}
-	remBits = (newBitBuffh << (16 - amount));
+	remBits = (newBitBuffh << (16 - amount)) & 0xffff;
 	_bitBuffh = newBitBuffh >> amount;
 	_bitBuffl = (newBitBuffl >> amount) | remBits;
 	_bitCount = (uint8)newBitCount;




More information about the Scummvm-git-logs mailing list