Fixed and updated C++ code regarding integer overflow checks.

pull/16/head
Project Nayuki 7 years ago
parent 2794dbc179
commit 777a9365f1

@ -32,7 +32,7 @@ BitBuffer::BitBuffer()
std::vector<std::uint8_t> BitBuffer::getBytes() const {
std::vector<std::uint8_t> result((size() + 7) / 8);
std::vector<std::uint8_t> result(size() / 8 + (size() % 8 == 0 ? 0 : 1));
for (std::size_t i = 0; i < size(); i++)
result[i >> 3] |= (*this)[i] ? 1 << (7 - (i & 7)) : 0;
return result;

@ -57,8 +57,6 @@ const QrSegment::Mode QrSegment::Mode::ECI (0x7, 0, 0, 0);
QrSegment QrSegment::makeBytes(const vector<uint8_t> &data) {
if (data.size() >= (unsigned int)INT_MAX / 8)
throw "Buffer too long";
BitBuffer bb;
for (uint8_t b : data)
bb.appendBits(b, 8);
@ -162,12 +160,14 @@ int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {
for (const QrSegment &seg : segs) {
int ccbits = seg.mode.numCharCountBits(version);
// Fail if segment length value doesn't fit in the length field's bit-width
if (seg.numChars >= (1L << ccbits) || seg.data.size() > INT16_MAX)
if (seg.numChars >= (1L << ccbits))
return -1;
long temp = (long)result + 4 + ccbits + seg.data.size();
if (temp > INT_MAX)
if (4 + ccbits > INT_MAX - result)
return -1;
result = temp;
result += 4 + ccbits;
if (seg.data.size() > static_cast<unsigned int>(INT_MAX - result))
return -1;
result += static_cast<int>(seg.data.size());
}
return result;
}

Loading…
Cancel
Save