From 6fe2741f2dadb165e0361eb171cdd85bad6a6dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ni=C3=B1o=20D=C3=ADaz?= Date: Sat, 28 Apr 2018 00:57:20 +0100 Subject: [PATCH] Enable GCC options to detect undefined behaviour MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GCC has an Undefined Behavior Sanitizer (ubsan), which enables run-time checks of undefined behaviour. It has been enabled for the `develop` build target. A small bug detected with it has been fixed. Signed-off-by: Antonio Niño Díaz --- Makefile | 7 ++++++- src/link/object.c | 12 ++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 58a51b6e..aa8d1ba9 100644 --- a/Makefile +++ b/Makefile @@ -207,7 +207,12 @@ develop: -Wstringop-overflow=4 -Walloc-zero -Wduplicated-cond \ -Wfloat-equal -Wshadow -Wcast-qual -Wcast-align -Wlogical-op \ -Wnested-externs -Wno-aggressive-loop-optimizations -Winline \ - -Wundef -Wstrict-prototypes -Wold-style-definition" + -Wundef -Wstrict-prototypes -Wold-style-definition \ + -fsanitize=shift -fsanitize=integer-divide-by-zero \ + -fsanitize=unreachable -fsanitize=vla-bound \ + -fsanitize=signed-integer-overflow -fsanitize=bounds \ + -fsanitize=object-size -fsanitize=bool -fsanitize=enum \ + -fsanitize=alignment -fsanitize=null" # Targets for the project maintainer to easily create Windows exes. # This is not for Windows users! diff --git a/src/link/object.c b/src/link/object.c index 9fd50471..023d562e 100644 --- a/src/link/object.c +++ b/src/link/object.c @@ -36,14 +36,14 @@ uint8_t oReadLib; */ static int32_t readlong(FILE *f) { - int32_t r; + uint32_t r; - r = fgetc(f); - r |= fgetc(f) << 8; - r |= fgetc(f) << 16; - r |= fgetc(f) << 24; + r = ((uint32_t)(uint8_t)fgetc(f)); + r |= ((uint32_t)(uint8_t)fgetc(f)) << 8; + r |= ((uint32_t)(uint8_t)fgetc(f)) << 16; + r |= ((uint32_t)(uint8_t)fgetc(f)) << 24; - return r; + return (int32_t)r; } /*