From 7a0875f4f3adacdcedd472742ad827b9f49e1c4d Mon Sep 17 00:00:00 2001 From: jsalling Date: Sat, 27 Feb 2016 10:26:04 -0600 Subject: [PATCH 1/2] Fix unreachable code in Unity on fully covered switch statements The clang compiler warns here with -Wunreachable-code The enum's switch statement covers all cases, so default is unused Leave the break in the code as a comment, to be more clear The history is this default case was added in f6bb7162 - compiler warning. Then the break was added in c6dc96f3. --- src/unity.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/unity.c b/src/unity.c index 05fa6ea..91edec8 100644 --- a/src/unity.c +++ b/src/unity.c @@ -775,7 +775,7 @@ void UnityAssertFloatSpecial(const _UF actual, break; default: - break; + /*break // unreachable code */; } if (is_trait != should_be_trait) @@ -938,7 +938,7 @@ void UnityAssertDoubleSpecial(const _UD actual, break; default: - break; + /*break // unreachable code*/; } if (is_trait != should_be_trait) From 13c99601a798516175624f2f60dc038674b03a5f Mon Sep 17 00:00:00 2001 From: jsalling Date: Tue, 1 Mar 2016 23:35:55 -0600 Subject: [PATCH 2/2] Add a value to FLOAT_TRAIT enum to enforce use of default case in switches Revert the previous commit. Add tests for extended enum cases. Fix crash due to accessing 'trait_names' array out of bounds. Adding an extra invalid value to the end of an enum causes '-Wswitch' flag to warn unless there is a switch default case - also enabled by '-Wall'. --- src/unity.c | 11 ++++++++--- src/unity_internals.h | 3 ++- test/Makefile | 3 +++ test/tests/testunity.c | 23 +++++++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/unity.c b/src/unity.c index 91edec8..e281d6a 100644 --- a/src/unity.c +++ b/src/unity.c @@ -35,6 +35,7 @@ const char UnityStrInf[] = "Infinity"; const char UnityStrNegInf[] = "Negative Infinity"; const char UnityStrNaN[] = "NaN"; const char UnityStrDet[] = "Determinate"; +const char UnityStrInvalidFloatTrait[] = "Invalid Float Trait"; const char UnityStrErrFloat[] = "Unity Floating Point Disabled"; const char UnityStrErrDouble[] = "Unity Double Precision Disabled"; const char UnityStrErr64[] = "Unity 64-bit Support Disabled"; @@ -775,7 +776,9 @@ void UnityAssertFloatSpecial(const _UF actual, break; default: - /*break // unreachable code */; + trait_index = 0; + trait_names[0] = UnityStrInvalidFloatTrait; + break; } if (is_trait != should_be_trait) @@ -905,7 +908,7 @@ void UnityAssertDoubleSpecial(const _UD actual, const char* trait_names[] = { UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet }; _U_SINT should_be_trait = ((_U_SINT)style & 1); _U_SINT is_trait = !should_be_trait; - _U_SINT trait_index = style >> 1; + _U_SINT trait_index = (_U_SINT)(style >> 1); UNITY_SKIP_EXECUTION; @@ -938,7 +941,9 @@ void UnityAssertDoubleSpecial(const _UD actual, break; default: - /*break // unreachable code*/; + trait_index = 0; + trait_names[0] = UnityStrInvalidFloatTrait; + break; } if (is_trait != should_be_trait) diff --git a/src/unity_internals.h b/src/unity_internals.h index b6f1061..6549656 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -396,7 +396,8 @@ typedef enum _UNITY_FLOAT_TRAIT_T UNITY_FLOAT_IS_NOT_NAN, UNITY_FLOAT_IS_NAN, UNITY_FLOAT_IS_NOT_DET, - UNITY_FLOAT_IS_DET + UNITY_FLOAT_IS_DET, + UNITY_FLOAT_INVALID_TRAIT } UNITY_FLOAT_TRAIT_T; #endif diff --git a/test/Makefile b/test/Makefile index 5a65a62..ef44540 100644 --- a/test/Makefile +++ b/test/Makefile @@ -28,6 +28,9 @@ coverage: $(BUILD_DIR)/testunityRunner.c uncovered: grep '###' $(BUILD_DIR)/unity.c.gcov -C2 || true +test: CFLAGS += -Wbad-function-cast -Wcast-qual -Wconversion -Wformat=2 -Wold-style-definition \ +-Wpointer-arith -Wshadow -Wstrict-overflow=5 -Wstrict-prototypes -Wswitch-default -Wundef \ +-Wunreachable-code -Wunused -fstrict-aliasing test: $(BUILD_DIR)/testunityRunner.c $(CC) $(CFLAGS) $(DEFINES) $(INC_DIR) $(SRC) -o $(TARGET) ./$(TARGET) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 9e81c61..9fae64e 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -2978,6 +2978,18 @@ void testFloatIsNotDeterminate2(void) #endif } +void testFloatTraitFailsOnInvalidTrait(void) +{ +#ifdef UNITY_EXCLUDE_FLOAT + TEST_IGNORE(); +#else + EXPECT_ABORT_BEGIN + UnityAssertFloatSpecial(1.0f, NULL, __LINE__, UNITY_FLOAT_INVALID_TRAIT); + VERIFY_FAILS_END +#endif +} + + void testEqualFloatArrays(void) { #ifdef UNITY_EXCLUDE_FLOAT @@ -3483,6 +3495,17 @@ void testDoubleIsNotDeterminate2(void) #endif } +void testDoubleTraitFailsOnInvalidTrait(void) +{ +#ifdef UNITY_EXCLUDE_DOUBLE + TEST_IGNORE(); +#else + EXPECT_ABORT_BEGIN + UnityAssertDoubleSpecial(1.0, NULL, __LINE__, UNITY_FLOAT_INVALID_TRAIT); + VERIFY_FAILS_END +#endif +} + void testEqualDoubleArrays(void) { #ifdef UNITY_EXCLUDE_DOUBLE