From 31634fc05cece96ef2ac4254eef8de62ca0248c9 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Thu, 20 Apr 2017 04:08:26 +0000 Subject: [PATCH] Fixed potential overflow in C and C++ code, in worst case when int type is int16. --- c/qrcodegen.c | 2 +- cpp/QrCode.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 4f52163..3c3a8c0 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -361,7 +361,7 @@ static long getPenaltyScore(const uint8_t qrcode[], int size) { } int total = size * size; // Find smallest k such that (45-5k)% <= dark/total <= (55+5k)% - for (int k = 0; black*20 < (9-k)*total || black*20 > (11+k)*total; k++) + for (int k = 0; black*20L < (9L-k)*total || black*20L > (11L+k)*total; k++) result += PENALTY_N4; return result; } diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index ff30088..7db7941 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -483,7 +483,7 @@ long qrcodegen::QrCode::getPenaltyScore() const { } int total = size * size; // Find smallest k such that (45-5k)% <= dark/total <= (55+5k)% - for (int k = 0; black*20 < (9-k)*total || black*20 > (11+k)*total; k++) + for (int k = 0; black*20L < (9L-k)*total || black*20L > (11L+k)*total; k++) result += PENALTY_N4; return result; }