Re: [PATCH] init: fix -Wmissing-variable-declarations clang warning

From: Bill Wendling
Date: Wed Aug 30 2023 - 16:23:02 EST


On Wed, Aug 30, 2023 at 12:47 PM Justin Stitt <justinstitt@xxxxxxxxxx> wrote:
>
> 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).
>
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

> Link: https://github.com/ClangBuiltLinux/linux/issues/1920
> Signed-off-by: Justin Stitt <justinstitt@xxxxxxxxxx>
> ---
> 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/20230829-missingvardecl-init-main-c-v1-1-ddf0f1a71215@xxxxxxxxxx/
>
> 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(-)
>
> diff --git a/include/linux/initrd.h b/include/linux/initrd.h
> index f1a1f4c92ded..0114f1acbb45 100644
> --- a/include/linux/initrd.h
> +++ b/include/linux/initrd.h
> @@ -34,4 +34,6 @@ extern unsigned long __initramfs_size;
>
> void console_on_rootfs(void);
>
> +extern const char *envp_init[];
> +
> #endif /* __LINUX_INITRD_H */
> diff --git a/init/do_mounts_initrd.c b/init/do_mounts_initrd.c
> index 425f4bcf4b77..b35c1b0babc2 100644
> --- a/init/do_mounts_initrd.c
> +++ b/init/do_mounts_initrd.c
> @@ -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 <justinstitt@xxxxxxxxxx>
>
>