Remove implicit fallthroughs; Add warning
authorCharles Giessen <charles@lunarg.com>
Tue, 1 Nov 2022 04:37:31 +0000 (22:37 -0600)
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>
Tue, 1 Nov 2022 21:05:40 +0000 (15:05 -0600)
Previously, cJSON used implicit fallthrough in a switch statement. Naked
fallthroughs are a common bug source, and while this use case was well
behaved, it did require disabling the compiler warnings for it. Thus,
converting it to an explicit for loop prevents unnecessary compiler
warning flags being disabled.

BUILD.gn
CMakeLists.txt
build-qnx/common.mk
loader/cJSON.c

index 8753755166ed2b092da71ab618940578e753d946..77239176ab527fa3b4516198bff4f186cd4fad8e 100644 (file)
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -43,7 +43,6 @@ config("vulkan_internal_config") {
     cflags = [
       "-Wno-conversion",
       "-Wno-extra-semi",
-      "-Wno-implicit-fallthrough",
       "-Wno-sign-compare",
       "-Wno-unreachable-code",
       "-Wno-unused-function",
index 1028518491cdbd5658ec62ed10f2d4b59c9d8265..088ed209b1fe901670dbd468c80d723804e3aa8b 100644 (file)
@@ -295,13 +295,8 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang
         target_compile_options(loader_common_options INTERFACE -fno-strict-aliasing -fno-builtin-memcmp)
     endif()
 
-    # For GCC version 7.1 or greater, we need to disable the implicit fallthrough warning since there's no consistent way to satisfy
-    # all compilers until they all accept the C++17 standard
     if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
         target_compile_options(loader_common_options INTERFACE -Wno-stringop-truncation -Wno-stringop-overflow)
-        if(CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL 7.1)
-            target_compile_options(loader_common_options INTERFACE -Wimplicit-fallthrough=0)
-        endif()
     endif()
 
     if(UNIX)
index d851f7f5ee8998b3fba67bd2b75c8c6dc312aa6e..a0d2a7352a5055fd42c3989bffacd88b46d5befd 100644 (file)
@@ -31,7 +31,7 @@ include $(MKFILES_ROOT)/qtargets.mk
 CCFLAGS += -DVK_USE_PLATFORM_SCREEN_QNX=1 -Dvulkan_EXPORTS
 CCFLAGS += -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
 CCFLAGS += -fno-strict-aliasing -fno-builtin-memcmp -Wno-stringop-truncation
-CCFLAGS += -Wno-stringop-overflow -Wimplicit-fallthrough=0 -fvisibility=hidden
+CCFLAGS += -Wno-stringop-overflow -fvisibility=hidden
 CCFLAGS += -Wpointer-arith -fPIC
 
 # Enable this if required
index 40adcb464d06dfb8bd3084e940fbc1b5a07f09ed..be6f2655e16c790ee40b44f10e6ead0074af9d91 100644 (file)
@@ -304,21 +304,13 @@ const char *parse_string(cJSON *item, const char *str) {
                         len = 3;
                     ptr2 += len;
 
-                    switch (len) {
-                        case 4:
-                            *--ptr2 = ((uc | 0x80) & 0xBF);
-                            uc >>= 6;
-                            // fall through
-                        case 3:
-                            *--ptr2 = ((uc | 0x80) & 0xBF);
-                            uc >>= 6;
-                            // fall through
-                        case 2:
+                    for (size_t i = len; i > 0; i--) {
+                        if (i == 1) {
+                            *--ptr2 = ((unsigned char)uc | firstByteMark[len]);
+                        } else if (i >= 2) {
                             *--ptr2 = ((uc | 0x80) & 0xBF);
                             uc >>= 6;
-                            // fall through
-                        case 1:
-                            *--ptr2 = ((unsigned char)uc | firstByteMark[len]);
+                        }
                     }
                     ptr2 += len;
                     break;