Skip to contentSkip to conversation

init: fix -Wmissing-variable-declarations clang warning

PATCH Superseded Justin Stitt 2 participants 2 messages Aug 30, 2023 VFS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 init: fix -Wmissing-variable-declarations clang warning

When building x86/defconfig with Clang-18 I encounter the following warning:

| init/main.c:189:13: warning: no previous extern declaration for non-static variable 'envp_init' [-Wmissing-variable-declarations]
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
| init/main.c:189:7: note: declare 'static' if the variable is not intended to be used outside of this translation unit
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };

Move the extern declaration to a header file so that the compiler can find it when compiling init/main.c.

Add const qualifier to extern declaration so that the types match and we follow the One-Definition Rule (ODR).

---
Note: build-tested only.

There's a previous RFC [1] wherein the kernel test robot reported some build errors which lead me to creating this patch which both fixes the errors as well as the -Wmissing-variable-declarations warning.

[1]: https://lore.kernel.org/all/[email protected]/

Also, get_maintainer.pl had a hard time finding folks to Cc here, not sure why.

---
 include/linux/initrd.h  | 2 ++
 init/do_mounts_initrd.c | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)
include/linux/initrd.h +2 -0
index f1a1f4c92ded..0114f1acbb45 100644
@@ -34,4 +34,6 @@ extern unsigned long __initramfs_size;
 
 void console_on_rootfs(void);
 
+extern const char *envp_init[];
+
 #endif /* __LINUX_INITRD_H */
init/do_mounts_initrd.c +0 -1
index 425f4bcf4b77..b35c1b0babc2 100644
@@ -87,7 +87,6 @@ static void __init handle_initrd(char *root_device_name)
 {
 	struct subprocess_info *info;
 	static char *argv[] = { "linuxrc", NULL, };
-	extern char *envp_init[];
 	int error;
 
 	pr_warn("using deprecated initrd support, will be removed in 2021.\n");

---
base-commit: 706a741595047797872e669b3101429ab8d378ef
change-id: 20230830-missingvardecl2-init-main-c-93dc1013ff8a

Best regards,
--
Justin Stitt <[email protected]>

#1 lore raw
Quoted 7 lines · Justin Stitt

When building x86/defconfig with Clang-18 I encounter the following warning:

| init/main.c:189:13: warning: no previous extern declaration for non-static variable 'envp_init' [-Wmissing-variable-declarations]
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
| init/main.c:189:7: note: declare 'static' if the variable is not intended to be used outside of this translation unit
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
Quoted 6 lines

Move the extern declaration to a header file so that the compiler can find it when compiling init/main.c.

Add const qualifier to extern declaration so that the types match and we follow the One-Definition Rule (ODR).

I'm not sure the lack of a "const" has anything to do with the ODR. C's type system isn't nearly as strict as C++'s. :-) That said, adding "const" is definitely correct.

-bw

Quoted 50 lines

---
Note: build-tested only.

There's a previous RFC [1] wherein the kernel test robot reported some build errors which lead me to creating this patch which both fixes the errors as well as the -Wmissing-variable-declarations warning.

[1]: https://lore.kernel.org/all/[email protected]/

Also, get_maintainer.pl had a hard time finding folks to Cc here, not sure why.

---
 include/linux/initrd.h  | 2 ++
 init/do_mounts_initrd.c | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)
include/linux/initrd.h +2 -0
index f1a1f4c92ded..0114f1acbb45 100644
@@ -34,4 +34,6 @@ extern unsigned long __initramfs_size;
 
 void console_on_rootfs(void);
 
+extern const char *envp_init[];
+
 #endif /* __LINUX_INITRD_H */
init/do_mounts_initrd.c +0 -2
index 425f4bcf4b77..b35c1b0babc2 100644
@@ -87,7 +87,6 @@ static void __init handle_initrd(char *root_device_name)
 {
        struct subprocess_info *info;
        static char *argv[] = { "linuxrc", NULL, };
-       extern char *envp_init[];
        int error;
 
        pr_warn("using deprecated initrd support, will be removed in 2021.\n");
 
---

base-commit: 706a741595047797872e669b3101429ab8d378ef
change-id: 20230830-missingvardecl2-init-main-c-93dc1013ff8a

Best regards,
--
Justin Stitt <[email protected]>