From 55b0056f38b8199398de80e8e99c00f533aba31b Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Sat, 9 Sep 2017 05:36:08 +0000 Subject: [PATCH] Added some assertions to new C functions. --- c/qrcodegen.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index bfae0e8..618404f 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -970,7 +970,12 @@ testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX); int result = 0; for (size_t i = 0; i < len; i++) { + int numChars = segs[i].numChars; + int bitLength = segs[i].bitLength; + assert(0 <= numChars && numChars <= INT16_MAX); + assert(0 <= bitLength && bitLength <= INT16_MAX); int ccbits = numCharCountBits(segs[i].mode, version); + assert(0 <= ccbits && ccbits <= 16); // Fail if segment length value doesn't fit in the length field's bit-width if (segs[i].numChars >= (1L << ccbits)) return -1; @@ -979,11 +984,13 @@ testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int return -1; result += temp; } + assert(0 <= result && result <= INT16_MAX); return result; } static int numCharCountBits(enum qrcodegen_Mode mode, int version) { + assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX); int i; if ( 1 <= version && version <= 9) i = 0; else if (10 <= version && version <= 26) i = 1;