mirror of
https://github.com/f4exb/sdrangel.git
synced 2026-08-07 11:26:09 -04:00
Fix undefined shift in SimpleDeserializer::readS32
The signed integer deserialization code used a signed temporary value while
assembling bytes:
tmp = (tmp << 8) | byte;
For negative values, tmp was initialized to -1 for sign extension, causing a
left shift of a negative value, which is undefined behavior in C++.
Use an unsigned temporary while constructing the 32-bit representation and
convert to qint32 only after all bytes have been assembled. This preserves the
existing two's complement sign handling while avoiding undefined signed shifts.
Fixes static analysis (cppcheck) warning about shifting a negative value.
Signed-off-by: Robin Getz <rgetz503@gmail.com>
This commit is contained in:
@@ -340,7 +340,7 @@ setInvalid:
|
||||
bool SimpleDeserializer::readS32(quint32 id, qint32* result, qint32 def) const
|
||||
{
|
||||
uint readOfs;
|
||||
qint32 tmp;
|
||||
quint32 tmp;
|
||||
Elements::const_iterator it = m_elements.constFind(id);
|
||||
if(it == m_elements.constEnd())
|
||||
goto returnDefault;
|
||||
@@ -354,10 +354,10 @@ bool SimpleDeserializer::readS32(quint32 id, qint32* result, qint32 def) const
|
||||
for(uint i = 0; i < it->length; i++) {
|
||||
quint8 byte = readByte(&readOfs);
|
||||
if((i == 0) && (byte & 0x80))
|
||||
tmp = -1;
|
||||
tmp = 0xFFFFFFFF;
|
||||
tmp = (tmp << 8) | byte;
|
||||
}
|
||||
*result = tmp;
|
||||
*result = static_cast<qint32>(tmp);
|
||||
return true;
|
||||
|
||||
returnDefault:
|
||||
|
||||
Reference in New Issue
Block a user