From 038b04d80a5d5667bd689f51132abc1a02fd9e19 Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 28 Nov 2016 09:11:14 +0100 Subject: [PATCH 1/2] Take out len from condition check. Otherwise, the check is just undefined behaviour. gcc even takes out this check because len can never be zero if len does not wrap around. Found with -Wstrict-overflow=2 --- cJSON.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index 766e558..ded3c88 100644 --- a/cJSON.c +++ b/cJSON.c @@ -712,8 +712,9 @@ static char *print_string_ptr(const char *str, printbuffer *p) ptr = str; /* calculate additional space that is needed for escaping */ - while ((token = *ptr) && ++len) + while ((token = *ptr)) { + ++len; if (strchr("\"\\\b\f\n\r\t", token)) { len++; /* +1 for the backslash */ From 5cfda2292f52091a3234584fd2192af3bcf5e706 Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 28 Nov 2016 09:12:17 +0100 Subject: [PATCH 2/2] Warn if compiler optimizes based on asumption signed overflow does not occur. This switch is only active when making a release build. --- CMakeLists.txt | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bf7cf8..b41211d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON) if (ENABLE_CUSTOM_COMPILER_FLAGS) if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2") endif() endif() diff --git a/Makefile b/Makefile index 9b1c63e..244e48f 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) INSTALL ?= cp -a -R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes $(CFLAGS) +R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 $(CFLAGS) uname := $(shell sh -c 'uname -s 2>/dev/null || echo false')