Skip to contentSkip to conversation

Makefile: Globally enable fall-through warning

PATCH In review [email protected] 2 participants 2 messages Jan 31 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 Makefile: Globally enable fall-through warning

From: "Gustavo A. R. Silva" <[email protected]>

Now that all the fall-through warnings have been addressed in the kernel, enable the fall-through warning globally.

Also, update the deprecated.rst file to include implicit fall-through as 'deprecated' so people can be pointed to a single location for justification.

Cc Masahiro Yamada <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Michal Marek <[email protected]>
Cc Kees Cook <[email protected]>
---
 Documentation/process/deprecated.rst | 14 ++++++++++++++
 Makefile                             |  3 +++
 2 files changed, 17 insertions(+)
Documentation/process/deprecated.rst +14 -0
index 49e0f64a3427..053b24a6dd38 100644
@@ -119,3 +119,17 @@ array may exceed the remaining memory in the stack segment. This could
 lead to a crash, possible overwriting sensitive contents at the end of the
 stack (when built without `CONFIG_THREAD_INFO_IN_TASK=y`), or overwriting
 memory adjacent to the stack (when built without `CONFIG_VMAP_STACK=y`)
+
+Implicit switch case fall-through
+---------------------------------
+The C language allows switch cases to "fall through" when
+a "break" statement is missing at the end of a case. This,
+however, introduces ambiguity in the code, as it's not always
+clear if the missing break is intentional or a bug. As there
+have been a long list of flaws `due to missing "break" statements
+<https://cwe.mitre.org/data/definitions/484.html>`_, we no longer allow
+"implicit fall-through". In order to identify an intentional fall-through
+case, we have adopted the marking used by static analyzers: a comment
+saying `/* Fall through */`. Once the C++17 `__attribute__((fallthrough))`
+is more widely handled by C compilers, static analyzers, and IDEs, we can
+switch to using that instead.
Makefile +3 -0
index 9be5834073f8..bdf8eac51b07 100644
@@ -843,6 +843,9 @@ NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
 # warn about C99 declaration after statement
 KBUILD_CFLAGS += -Wdeclaration-after-statement
 
+# Warn about unmarked fall-throughs in switch statement.
+KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=3,)
+
 # Variable Length Arrays (VLAs) should not be used anywhere in the kernel
 KBUILD_CFLAGS += -Wvla
 
Signature · 1 line
2.34.1
#2 lore raw

Shuah,

People taking this[1] LF training have been wrongly submitting this patch since 2022 [2].

Why?

-Gustavo

[1] https://training.linuxfoundation.org/training/a-beginners-guide-to-linux-kernel-development-lfd103/
[2] https://lore.kernel.org/all/20220517195912.GA10952@embeddedor/

Quoted 56 lines · On 1/31/26 14:40, [email protected] wrote:

From: "Gustavo A. R. Silva" <[email protected]>

Now that all the fall-through warnings have been addressed in the kernel, enable the fall-through warning globally.

Also, update the deprecated.rst file to include implicit fall-through as 'deprecated' so people can be pointed to a single location for justification.

Cc Masahiro Yamada <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Michal Marek <[email protected]>
Cc Kees Cook <[email protected]>
---
  Documentation/process/deprecated.rst | 14 ++++++++++++++
  Makefile                             |  3 +++
  2 files changed, 17 insertions(+)
Documentation/process/deprecated.rst +14 -0
index 49e0f64a3427..053b24a6dd38 100644
@@ -119,3 +119,17 @@ array may exceed the remaining memory in the stack segment. This could
  lead to a crash, possible overwriting sensitive contents at the end of the
  stack (when built without `CONFIG_THREAD_INFO_IN_TASK=y`), or overwriting
  memory adjacent to the stack (when built without `CONFIG_VMAP_STACK=y`)
+
+Implicit switch case fall-through
+---------------------------------
+The C language allows switch cases to "fall through" when
+a "break" statement is missing at the end of a case. This,
+however, introduces ambiguity in the code, as it's not always
+clear if the missing break is intentional or a bug. As there
+have been a long list of flaws `due to missing "break" statements
+<https://cwe.mitre.org/data/definitions/484.html>`_, we no longer allow
+"implicit fall-through". In order to identify an intentional fall-through
+case, we have adopted the marking used by static analyzers: a comment
+saying `/* Fall through */`. Once the C++17 `__attribute__((fallthrough))`
+is more widely handled by C compilers, static analyzers, and IDEs, we can
+switch to using that instead.
Makefile +3 -0
index 9be5834073f8..bdf8eac51b07 100644
@@ -843,6 +843,9 @@ NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
  # warn about C99 declaration after statement
  KBUILD_CFLAGS += -Wdeclaration-after-statement
 
+# Warn about unmarked fall-throughs in switch statement.
+KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=3,)
+
  # Variable Length Arrays (VLAs) should not be used anywhere in the kernel
  KBUILD_CFLAGS += -Wvla