From 9312480978d70003a2051e766ee2208cd00eac69 Mon Sep 17 00:00:00 2001
From: Project Nayuki <me@nayuki.io>
Date: Mon, 22 Jul 2019 16:31:22 +0000
Subject: [PATCH] In C version, added many explicit integer type casts for
 signedness, with the help of GCC's "-Wsign-conversion" and "-Wconversion".

---
 c/qrcodegen-demo.c   |  2 +-
 c/qrcodegen-test.c   | 34 +++++++++++++++++-----------------
 c/qrcodegen-worker.c | 10 +++++-----
 c/qrcodegen.c        | 34 ++++++++++++++++++----------------
 4 files changed, 41 insertions(+), 39 deletions(-)

diff --git a/c/qrcodegen-demo.c b/c/qrcodegen-demo.c
index ef44108..872f48b 100644
--- a/c/qrcodegen-demo.c
+++ b/c/qrcodegen-demo.c
@@ -224,7 +224,7 @@ static void doSegmentDemo(void) {
 			uint8_t *segBuf = calloc(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_KANJI, len), sizeof(uint8_t));
 			struct qrcodegen_Segment seg;
 			seg.mode = qrcodegen_Mode_KANJI;
-			seg.numChars = len;
+			seg.numChars = (int)len;
 			seg.bitLength = 0;
 			for (size_t i = 0; i < len; i++) {
 				for (int j = 12; j >= 0; j--, seg.bitLength++)
diff --git a/c/qrcodegen-test.c b/c/qrcodegen-test.c
index db48b11..30bb318 100644
--- a/c/qrcodegen-test.c
+++ b/c/qrcodegen-test.c
@@ -114,13 +114,13 @@ static uint8_t *addEccAndInterleaveReference(const uint8_t *data, int version, e
 	int shortBlockLen = rawCodewords / numBlocks;
 	
 	// Split data into blocks and append ECC to each block
-	uint8_t **blocks = malloc(numBlocks * sizeof(uint8_t*));
-	uint8_t *generator = malloc(blockEccLen * sizeof(uint8_t));
+	uint8_t **blocks = malloc((size_t)numBlocks * sizeof(uint8_t*));
+	uint8_t *generator = malloc((size_t)blockEccLen * sizeof(uint8_t));
 	reedSolomonComputeDivisor(blockEccLen, generator);
 	for (int i = 0, k = 0; i < numBlocks; i++) {
-		uint8_t *block = malloc((shortBlockLen + 1) * sizeof(uint8_t));
+		uint8_t *block = malloc((size_t)(shortBlockLen + 1) * sizeof(uint8_t));
 		int datLen = shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1);
-		memcpy(block, &data[k], datLen * sizeof(uint8_t));
+		memcpy(block, &data[k], (size_t)datLen * sizeof(uint8_t));
 		reedSolomonComputeRemainder(&data[k], datLen, generator, blockEccLen, &block[shortBlockLen + 1 - blockEccLen]);
 		k += datLen;
 		blocks[i] = block;
@@ -128,7 +128,7 @@ static uint8_t *addEccAndInterleaveReference(const uint8_t *data, int version, e
 	free(generator);
 	
 	// Interleave (not concatenate) the bytes from every block into a single sequence
-	uint8_t *result = malloc(rawCodewords * sizeof(uint8_t));
+	uint8_t *result = malloc((size_t)rawCodewords * sizeof(uint8_t));
 	for (int i = 0, k = 0; i < shortBlockLen + 1; i++) {
 		for (int j = 0; j < numBlocks; j++) {
 			// Skip the padding byte in short blocks
@@ -149,18 +149,18 @@ static void testAddEccAndInterleave(void) {
 	for (int version = 1; version <= 40; version++) {
 		for (int ecl = 0; ecl < 4; ecl++) {
 			int dataLen = getNumDataCodewords(version, (enum qrcodegen_Ecc)ecl);
-			uint8_t *pureData = malloc(dataLen * sizeof(uint8_t));
+			uint8_t *pureData = malloc((size_t)dataLen * sizeof(uint8_t));
 			for (int i = 0; i < dataLen; i++)
-				pureData[i] = rand() % 256;
+				pureData[i] = (uint8_t)(rand() % 256);
 			uint8_t *expectOutput = addEccAndInterleaveReference(pureData, version, (enum qrcodegen_Ecc)ecl);
 			
 			int dataAndEccLen = getNumRawDataModules(version) / 8;
-			uint8_t *paddedData = malloc(dataAndEccLen * sizeof(uint8_t));
-			memcpy(paddedData, pureData, dataLen * sizeof(uint8_t));
-			uint8_t *actualOutput = malloc(dataAndEccLen * sizeof(uint8_t));
+			uint8_t *paddedData = malloc((size_t)dataAndEccLen * sizeof(uint8_t));
+			memcpy(paddedData, pureData, (size_t)dataLen * sizeof(uint8_t));
+			uint8_t *actualOutput = malloc((size_t)dataAndEccLen * sizeof(uint8_t));
 			addEccAndInterleave(paddedData, version, (enum qrcodegen_Ecc)ecl, actualOutput);
 			
-			assert(memcmp(actualOutput, expectOutput, dataAndEccLen * sizeof(uint8_t)) == 0);
+			assert(memcmp(actualOutput, expectOutput, (size_t)dataAndEccLen * sizeof(uint8_t)) == 0);
 			free(pureData);
 			free(expectOutput);
 			free(paddedData);
@@ -362,7 +362,7 @@ static void testReedSolomonMultiply(void) {
 
 static void testInitializeFunctionModulesEtc(void) {
 	for (int ver = 1; ver <= 40; ver++) {
-		uint8_t *qrcode = malloc(qrcodegen_BUFFER_LEN_FOR_VERSION(ver) * sizeof(uint8_t));
+		uint8_t *qrcode = malloc((size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(ver) * sizeof(uint8_t));
 		assert(qrcode != NULL);
 		initializeFunctionModules(ver, qrcode);
 		
@@ -718,7 +718,7 @@ static void testCalcSegmentBitLength(void) {
 			{INT_MAX / 1, -1},
 		};
 		for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
-			assert(calcSegmentBitLength(qrcodegen_Mode_NUMERIC, cases[i][0]) == cases[i][1]);
+			assert(calcSegmentBitLength(qrcodegen_Mode_NUMERIC, (size_t)cases[i][0]) == cases[i][1]);
 			numTestCases++;
 		}
 	}
@@ -748,7 +748,7 @@ static void testCalcSegmentBitLength(void) {
 			{INT_MAX / 1, -1},
 		};
 		for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
-			assert(calcSegmentBitLength(qrcodegen_Mode_ALPHANUMERIC, cases[i][0]) == cases[i][1]);
+			assert(calcSegmentBitLength(qrcodegen_Mode_ALPHANUMERIC, (size_t)cases[i][0]) == cases[i][1]);
 			numTestCases++;
 		}
 	}
@@ -777,7 +777,7 @@ static void testCalcSegmentBitLength(void) {
 			{INT_MAX / 1, -1},
 		};
 		for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
-			assert(calcSegmentBitLength(qrcodegen_Mode_BYTE, cases[i][0]) == cases[i][1]);
+			assert(calcSegmentBitLength(qrcodegen_Mode_BYTE, (size_t)cases[i][0]) == cases[i][1]);
 			numTestCases++;
 		}
 	}
@@ -805,7 +805,7 @@ static void testCalcSegmentBitLength(void) {
 			{INT_MAX / 1, -1},
 		};
 		for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
-			assert(calcSegmentBitLength(qrcodegen_Mode_KANJI, cases[i][0]) == cases[i][1]);
+			assert(calcSegmentBitLength(qrcodegen_Mode_KANJI, (size_t)cases[i][0]) == cases[i][1]);
 			numTestCases++;
 		}
 	}
@@ -1049,7 +1049,7 @@ static void testGetTotalBits(void) {
 /*---- Main runner ----*/
 
 int main(void) {
-	srand(time(NULL));
+	srand((unsigned int)time(NULL));
 	testAppendBitsToBuffer();
 	testAddEccAndInterleave();
 	testGetNumDataCodewords();
diff --git a/c/qrcodegen-worker.c b/c/qrcodegen-worker.c
index b57d30e..b41a037 100644
--- a/c/qrcodegen-worker.c
+++ b/c/qrcodegen-worker.c
@@ -47,7 +47,7 @@ int main(void) {
 		
 		// Read data bytes
 		bool isAscii = true;
-		uint8_t *data = malloc(length * sizeof(uint8_t));
+		uint8_t *data = malloc((size_t)length * sizeof(uint8_t));
 		if (data == NULL) {
 			perror("malloc");
 			return EXIT_FAILURE;
@@ -67,8 +67,8 @@ int main(void) {
 		
 		// Allocate memory for QR Code
 		int bufferLen = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
-		uint8_t *qrcode     = malloc(bufferLen * sizeof(uint8_t));
-		uint8_t *tempBuffer = malloc(bufferLen * sizeof(uint8_t));
+		uint8_t *qrcode     = malloc((size_t)bufferLen * sizeof(uint8_t));
+		uint8_t *tempBuffer = malloc((size_t)bufferLen * sizeof(uint8_t));
 		if (qrcode == NULL || tempBuffer == NULL) {
 			perror("malloc");
 			return EXIT_FAILURE;
@@ -77,7 +77,7 @@ int main(void) {
 		// Try to make QR Code symbol
 		bool ok;
 		if (isAscii) {
-			char *text = malloc((length + 1) * sizeof(char));
+			char *text = malloc((size_t)(length + 1) * sizeof(char));
 			if (text == NULL) {
 				perror("malloc");
 				return EXIT_FAILURE;
@@ -89,7 +89,7 @@ int main(void) {
 				minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
 			free(text);
 		} else if (length <= bufferLen) {
-			memcpy(tempBuffer, data, length * sizeof(data[0]));
+			memcpy(tempBuffer, data, (size_t)length * sizeof(data[0]));
 			ok = qrcodegen_encodeBinary(tempBuffer, (size_t)length, qrcode, (enum qrcodegen_Ecc)errCorLvl,
 				minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
 		} else
diff --git a/c/qrcodegen.c b/c/qrcodegen.c
index d899803..612409a 100644
--- a/c/qrcodegen.c
+++ b/c/qrcodegen.c
@@ -132,7 +132,7 @@ bool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode
 	size_t textLen = strlen(text);
 	if (textLen == 0)
 		return qrcodegen_encodeSegmentsAdvanced(NULL, 0, ecl, minVersion, maxVersion, mask, boostEcl, tempBuffer, qrcode);
-	size_t bufLen = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
+	size_t bufLen = (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
 	
 	struct qrcodegen_Segment seg;
 	if (qrcodegen_isNumeric(text)) {
@@ -228,14 +228,16 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz
 	}
 	
 	// Concatenate all segments to create the data bit string
-	memset(qrcode, 0, qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0]));
+	memset(qrcode, 0, (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0]));
 	int bitLen = 0;
 	for (size_t i = 0; i < len; i++) {
 		const struct qrcodegen_Segment *seg = &segs[i];
-		appendBitsToBuffer((int)seg->mode, 4, qrcode, &bitLen);
-		appendBitsToBuffer(seg->numChars, numCharCountBits(seg->mode, version), qrcode, &bitLen);
-		for (int j = 0; j < seg->bitLength; j++)
-			appendBitsToBuffer((seg->data[j >> 3] >> (7 - (j & 7))) & 1, 1, qrcode, &bitLen);
+		appendBitsToBuffer((unsigned int)seg->mode, 4, qrcode, &bitLen);
+		appendBitsToBuffer((unsigned int)seg->numChars, numCharCountBits(seg->mode, version), qrcode, &bitLen);
+		for (int j = 0; j < seg->bitLength; j++) {
+			int bit = (seg->data[j >> 3] >> (7 - (j & 7))) & 1;
+			appendBitsToBuffer((unsigned int)bit, 1, qrcode, &bitLen);
+		}
 	}
 	assert(bitLen == dataUsedBits);
 	
@@ -356,7 +358,7 @@ testable void reedSolomonComputeDivisor(int degree, uint8_t result[]) {
 	assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);
 	// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
 	// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
-	memset(result, 0, degree * sizeof(result[0]));
+	memset(result, 0, (size_t)degree * sizeof(result[0]));
 	result[degree - 1] = 1;  // Start off with the monomial x^0
 	
 	// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
@@ -381,10 +383,10 @@ testable void reedSolomonComputeDivisor(int degree, uint8_t result[]) {
 testable void reedSolomonComputeRemainder(const uint8_t data[], int dataLen,
 		const uint8_t generator[], int degree, uint8_t result[]) {
 	assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);
-	memset(result, 0, degree * sizeof(result[0]));
+	memset(result, 0, (size_t)degree * sizeof(result[0]));
 	for (int i = 0; i < dataLen; i++) {  // Polynomial division
 		uint8_t factor = data[i] ^ result[0];
-		memmove(&result[0], &result[1], (degree - 1) * sizeof(result[0]));
+		memmove(&result[0], &result[1], (size_t)(degree - 1) * sizeof(result[0]));
 		result[degree - 1] = 0;
 		for (int j = 0; j < degree; j++)
 			result[j] ^= reedSolomonMultiply(generator[j], factor);
@@ -400,7 +402,7 @@ testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y) {
 	// Russian peasant multiplication
 	uint8_t z = 0;
 	for (int i = 7; i >= 0; i--) {
-		z = (z << 1) ^ ((z >> 7) * 0x11D);
+		z = (uint8_t)((z << 1) ^ ((z >> 7) * 0x11D));
 		z ^= ((y >> i) & 1) * x;
 	}
 	return z;
@@ -415,7 +417,7 @@ testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y) {
 testable void initializeFunctionModules(int version, uint8_t qrcode[]) {
 	// Initialize QR Code
 	int qrsize = version * 4 + 17;
-	memset(qrcode, 0, ((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0]));
+	memset(qrcode, 0, (size_t)((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0]));
 	qrcode[0] = (uint8_t)qrsize;
 	
 	// Fill horizontal and vertical timing patterns
@@ -551,7 +553,7 @@ testable int getAlignmentPatternPositions(int version, uint8_t result[7]) {
 	int step = (version == 32) ? 26 :
 		(version*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2;
 	for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step)
-		result[i] = pos;
+		result[i] = (uint8_t)pos;
 	result[0] = 6;
 	return numAlign;
 }
@@ -962,16 +964,16 @@ struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) {
 		assert(false);
 	else if (assignVal < (1 << 7)) {
 		memset(buf, 0, 1 * sizeof(buf[0]));
-		appendBitsToBuffer(assignVal, 8, buf, &result.bitLength);
+		appendBitsToBuffer((unsigned int)assignVal, 8, buf, &result.bitLength);
 	} else if (assignVal < (1 << 14)) {
 		memset(buf, 0, 2 * sizeof(buf[0]));
 		appendBitsToBuffer(2, 2, buf, &result.bitLength);
-		appendBitsToBuffer(assignVal, 14, buf, &result.bitLength);
+		appendBitsToBuffer((unsigned int)assignVal, 14, buf, &result.bitLength);
 	} else if (assignVal < 1000000L) {
 		memset(buf, 0, 3 * sizeof(buf[0]));
 		appendBitsToBuffer(6, 3, buf, &result.bitLength);
-		appendBitsToBuffer(assignVal >> 10, 11, buf, &result.bitLength);
-		appendBitsToBuffer(assignVal & 0x3FF, 10, buf, &result.bitLength);
+		appendBitsToBuffer((unsigned int)(assignVal >> 10), 11, buf, &result.bitLength);
+		appendBitsToBuffer((unsigned int)(assignVal & 0x3FF), 10, buf, &result.bitLength);
 	} else
 		assert(false);
 	result.data = buf;