shithub: mc

Download patch

ref: ce56b7eacc56b427ba673829b0b35b933e3f2cda
parent: 359f5e7e727145aeebae62563afbbe031257ea21
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Jul 28 22:05:12 EDT 2016

Fix a bit of undefined behavior.

--- a/mk/c.mk
+++ b/mk/c.mk
@@ -9,8 +9,9 @@
 _LIBPATHS=$(addprefix -l, $(patsubst lib%.a,%,$(notdir $(DEPS)))) $(_PCLIBS)
 
 # yeah, I should probably remove -Werror, but it's nice for developing alone.
-CFLAGS += -Wall -Wextra -Werror -Wno-unused-parameter -Wno-missing-field-initializers -Wno-sign-compare -g -O0
+CFLAGS += -Wall -Wextra -Werror -Wno-unused-parameter -Wno-missing-field-initializers -Wno-sign-compare -g -O0 -fsanitize=undefined
 CFLAGS += -MMD -MP -MF .deps/$(subst /,-,$*).d
+LDFLAGS += -fsanitize=undefined
 
 LIB ?= $(INSTLIB)
 BIN ?= $(INSTBIN)
--- a/parse/fold.c
+++ b/parse/fold.c
@@ -153,7 +153,7 @@
 		if (isintval(args[1], 0))
 			r = args[0];
 		if (getintlit(args[0], &a) && getintlit(args[1], &b))
-			r = val(n->loc, a - b, exprtype(n));
+			r = val(n->loc, (uint64_t)a - b, exprtype(n));
 		break;
 	case Omul:
 		/* 1 * x = x */
@@ -198,7 +198,7 @@
 		break;
 	case Obsl:
 		if (getintlit(args[0], &a) && getintlit(args[1], &b))
-			r = val(n->loc, a << b, exprtype(n));
+			r = val(n->loc, (uint64_t)a << b, exprtype(n));
 		break;
 	case Obsr:
 		if (getintlit(args[0], &a) && getintlit(args[1], &b))
--- a/util/htab.c
+++ b/util/htab.c
@@ -277,7 +277,7 @@
 	uint32_t r = 24;
 	uint32_t h, k;
 	uint32_t *data;
-	char *buf;
+	uint8_t *buf;
 	
 	buf = ptr;
 	data = (uint32_t*)buf;