diff --git a/cpp/BitBuffer.cpp b/cpp/BitBuffer.cpp index 3ed13a4..d8a8357 100644 --- a/cpp/BitBuffer.cpp +++ b/cpp/BitBuffer.cpp @@ -32,7 +32,7 @@ BitBuffer::BitBuffer() std::vector BitBuffer::getBytes() const { - std::vector result((size() + 7) / 8); + std::vector 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; diff --git a/cpp/QrSegment.cpp b/cpp/QrSegment.cpp index c17294d..f180518 100644 --- a/cpp/QrSegment.cpp +++ b/cpp/QrSegment.cpp @@ -57,8 +57,6 @@ const QrSegment::Mode QrSegment::Mode::ECI (0x7, 0, 0, 0); QrSegment QrSegment::makeBytes(const vector &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 &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(INT_MAX - result)) + return -1; + result += static_cast(seg.data.size()); } return result; }