mcc

mcc is a machine code compiler.

Log | Files | << Repositories


tree 62c92e6aeafd3085a0bb399380bbe21a404cfa2c
parent 99e5669f15d33482fb30c9cb05be369c8f3e04b2
author esote <esote.net@gmail.com> 1558295610 -0500
committer esote <esote.net@gmail.com> 1558295610 -0500
gpgsig -----BEGIN PGP SIGNATURE-----
 
 iHUEABYIAB0WIQTXAxYDuIzimYoNSPuhTmRAjzzC8gUCXSArIgAKCRChTmRAjzzC
 8n38AQCmeB31URxq1Rp/WzbdlD6OqlUHiBaFi7EumJfZCDA1VgD/fqVJ4BXKxGJb
 syWp8Gwu5z7oURCQ9ey9yUptLws3pAU=
 =OBj8
 -----END PGP SIGNATURE-----

Fix possible overflow on large byte lengths

 mcc.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/mcc.c b/mcc.c
index 4e526d3..436ab49 100644
--- a/mcc.c
+++ b/mcc.c
@@ -41,7 +41,7 @@ main(int argc, char *argv[])
 	FILE *out;
 	struct mcc_opts opts;
 	char *end;
-	char *iname;
+	char const *iname;
 	char const *oname;
 	uint64_t len;
 	uint64_t mem;
@@ -177,12 +177,13 @@ main(int argc, char *argv[])
 uint64_t
 byte_len(FILE *const in)
 {
-	unsigned char buf;
+	uint64_t i;
 	uint64_t len;
+	unsigned char buf;
 
 	len = 0;
 
-	while (fread(&buf, 1, 1, in) == 1) {
+	for (i = 0; fread(&buf, 1, 1, in) == 1; ) {
 		switch (buf) {
 		case COMMENT:
 			/* gulp until newline or EOF */
@@ -196,13 +197,17 @@ byte_len(FILE *const in)
 			break;
 		case '0':
 		case '1':
-			len++;
+			if (++i == 8) {
+				i = 0;
+				len++;
+			}
+
 			break;
 		}
 	}
 
 out:
-	if (len % 8 != 0) {
+	if (i != 0) {
 		errx(1, "weird bytes");
 	}
 
@@ -210,7 +215,7 @@ out:
 		err(1, "fseek");
 	}
 
-	return len / 8;
+	return len;
 }
 
 void