fix: getRemainingSize() underflowed when readPos exceeded data size

Both operands are size_t (unsigned), so if readPos > data.size() the
subtraction wrapped to ~0 instead of returning 0. This could happen
via setReadPos() which has no bounds check. Downstream hasRemaining()
was already safe but getRemainingSize() callers (e.g. hasFullPackedGuid)
would see billions of bytes available.
This commit is contained in:
Kelsi 2026-03-29 19:36:41 -07:00
parent 9da97e5e88
commit ed63b029cd

View file

@ -35,7 +35,9 @@ public:
const std::vector<uint8_t>& getData() const { return data; }
size_t getReadPos() const { return readPos; }
size_t getSize() const { return data.size(); }
size_t getRemainingSize() const { return data.size() - readPos; }
// Clamp to 0 instead of wrapping to ~(size_t)0 when readPos overshoots
// (can happen via setReadPos with an unchecked offset).
size_t getRemainingSize() const { return (readPos <= data.size()) ? (data.size() - readPos) : 0; }
bool hasRemaining(size_t need) const { return readPos <= data.size() && need <= (data.size() - readPos); }
bool hasFullPackedGuid() const {
if (readPos >= data.size()) return false;