mirror of
https://github.com/gbdev/rgbds.git
synced 2026-08-29 18:15:16 +00:00
Fix the same shifting UB for SDAS objects as for RGBDS ones
This commit is contained in:
+2
-7
@@ -57,13 +57,8 @@ static int64_t readLong(FILE *file) {
|
|||||||
if (byte == EOF) {
|
if (byte == EOF) {
|
||||||
return INT64_MAX;
|
return INT64_MAX;
|
||||||
}
|
}
|
||||||
// This must be casted to `unsigned`, not `uint8_t`. Rationale:
|
// Cast to `uint32_t` to avoid UB when shifting a byte >= 128 by a count >= 24.
|
||||||
// the type of the shift is the type of `byte` after undergoing
|
value |= static_cast<uint32_t>(byte) << shift;
|
||||||
// integer promotion, which would be `int` if this was casted to
|
|
||||||
// `uint8_t`, because int is large enough to hold a byte. This
|
|
||||||
// however causes values larger than 127 to be too large when
|
|
||||||
// shifted, potentially triggering undefined behavior.
|
|
||||||
value |= static_cast<unsigned int>(byte) << shift;
|
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -622,7 +622,8 @@ void sdobj_ReadFile(FileStackNode const &src, FILE *file, std::vector<Symbol> &f
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
for (uint8_t i = 0; i < nbBaseBytes; ++i) {
|
for (uint8_t i = 0; i < nbBaseBytes; ++i) {
|
||||||
baseValue = baseValue | data[offset + i] << (8 * i);
|
// Cast to `uint32_t` to avoid UB when shifting a byte >= 128 by a count >= 24.
|
||||||
|
baseValue = baseValue | static_cast<uint32_t>(data[offset + i]) << (8 * i);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bit 4 specifies signedness, but I don't think that matters?
|
// Bit 4 specifies signedness, but I don't think that matters?
|
||||||
|
|||||||
Reference in New Issue
Block a user