From 21212ba61711f7b96cf80d4b8728df3b117984d6 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Thu, 20 Apr 2017 04:10:53 +0000 Subject: [PATCH] Fixed integer overflow checks in C code. --- c/qrcodegen.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 3c3a8c0..ee785c9 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -113,14 +113,14 @@ int qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[ if (textLen > INT_MAX / 3) return 0; textBits = textLen * 3; - if (textLen > INT_MAX - 2 || textLen > INT_MAX - textBits) + if (textLen > INT_MAX - 2 || (textLen + 2) / 3 > INT_MAX - textBits) return 0; textBits += (textLen + 2) / 3; } else if (isAlphanumeric) { // textBits = textLen * 5 + ceil(textLen / 2) if (textLen > INT_MAX / 5) return 0; textBits = textLen * 5; - if (textLen > INT_MAX - 1 || textLen > INT_MAX - textBits) + if (textLen > INT_MAX - 1 || (textLen + 1) / 2 > INT_MAX - textBits) return 0; textBits += (textLen + 1) / 2; } else { // Use binary mode