Updated C++ QrCode private methods to replace some int variables with long to prevent overflow.

This commit is contained in:
Project Nayuki 2017-04-17 16:41:20 +00:00
parent 873652a82f
commit ceb51372d3
2 changed files with 5 additions and 5 deletions

View File

@ -393,11 +393,11 @@ void qrcodegen::QrCode::applyMask(int mask) {
int qrcodegen::QrCode::handleConstructorMasking(int mask) { int qrcodegen::QrCode::handleConstructorMasking(int mask) {
if (mask == -1) { // Automatically choose best mask if (mask == -1) { // Automatically choose best mask
int32_t minPenalty = INT32_MAX; long minPenalty = LONG_MAX;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
drawFormatBits(i); drawFormatBits(i);
applyMask(i); applyMask(i);
int penalty = getPenaltyScore(); long penalty = getPenaltyScore();
if (penalty < minPenalty) { if (penalty < minPenalty) {
mask = i; mask = i;
minPenalty = penalty; minPenalty = penalty;
@ -413,8 +413,8 @@ int qrcodegen::QrCode::handleConstructorMasking(int mask) {
} }
int qrcodegen::QrCode::getPenaltyScore() const { long qrcodegen::QrCode::getPenaltyScore() const {
int result = 0; long result = 0;
// Adjacent modules in row having same color // Adjacent modules in row having same color
for (int y = 0; y < size; y++) { for (int y = 0; y < size; y++) {

View File

@ -211,7 +211,7 @@ class QrCode final {
// Calculates and returns the penalty score based on state of this QR Code's current modules. // Calculates and returns the penalty score based on state of this QR Code's current modules.
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score. // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
private: int getPenaltyScore() const; private: long getPenaltyScore() const;