of: declare string literals const
PATCH 1/1 of: declare string literals const
of_overlay_action_name() returns a string literal from a function local array. Modifying string literals is undefined behavior which usage of const pointer can avoid. of_overlay_action_name() is currently only used once in overlay_notify() to print the returned value.
While on it declare the data array const as well.
Reported by Clang:
In file included from arch/x86/kernel/asm-offsets.c:22:
In file included from arch/x86/kernel/../kvm/vmx/vmx.h:5:
In file included from ./include/linux/kvm_host.h:19:
In file included from ./include/linux/msi.h:23:
In file included from ./arch/x86/include/asm/msi.h:5:
In file included from ./arch/x86/include/asm/irqdomain.h:5:
In file included from ./include/linux/irqdomain.h:35:
./include/linux/of.h:1555:3: error: initializing 'char *' with an expression of type 'const char[5]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"init",
^~~~~~
./include/linux/of.h:1556:3: error: initializing 'char *' with an expression of type 'const char[10]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"pre-apply",
^~~~~~~~~~~
./include/linux/of.h:1557:3: error: initializing 'char *' with an expression of type 'const char[11]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"post-apply",
^~~~~~~~~~~~
./include/linux/of.h:1558:3: error: initializing 'char *' with an expression of type 'const char[11]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"pre-remove",
^~~~~~~~~~~~
./include/linux/of.h:1559:3: error: initializing 'char *' with an expression of type 'const char[12]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"post-remove",
^~~~~~~~~~~~~
--- include/linux/of.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/of.h +2 -2
@@ -1549,9 +1549,9 @@ enum of_overlay_notify_action { OF_OVERLAY_POST_REMOVE, }; -static inline char *of_overlay_action_name (enum of_overlay_notify_action action) +static inline const char *of_overlay_action_name (enum of_overlay_notify_action action) { -static char *of_overlay_action_name[] = { +static const char *const of_overlay_action_name[] = {"init" ,"pre-apply" ,"post-apply" ,
Signature · 1 line
2.37.2
Quoted 33 lines · Christian Göttsche
of_overlay_action_name()returns a string literal from a function local array. Modifying string literals is undefined behavior which usage of const pointer can avoid.of_overlay_action_name()is currently only used once inoverlay_notify()to print the returned value.While on it declare the data array const as well.
Reported by Clang:
In file included from
arch/x86/kernel/asm-offsets.c:22:
In file included fromarch/x86/kernel/../kvm/vmx/vmx.h:5:
In file included from./include/linux/kvm_host.h:19:
In file included from./include/linux/msi.h:23:
In file included from./arch/x86/include/asm/msi.h:5:
In file included from./arch/x86/include/asm/irqdomain.h:5:
In file included from./include/linux/irqdomain.h:35:
./include/linux/of.h:1555:3: error: initializing 'char *' with an expression of type 'const char[5]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"init",
^~~~~~
./include/linux/of.h:1556:3: error: initializing 'char *' with an expression of type 'const char[10]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"pre-apply",
^~~~~~~~~~~
./include/linux/of.h:1557:3: error: initializing 'char *' with an expression of type 'const char[11]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"post-apply",
^~~~~~~~~~~~
./include/linux/of.h:1558:3: error: initializing 'char *' with an expression of type 'const char[11]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"pre-remove",
^~~~~~~~~~~~
./include/linux/of.h:1559:3: error: initializing 'char *' with an expression of type 'const char[12]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"post-remove",
^~~~~~~~~~~~~
Quoted 1 line
Signed-off-by Christian Göttsche <[email protected]>
Thanks for the patch!
Quoted 23 lines
--- include/linux/of.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)include/linux/of.h +2
-3@@ -1549,9 +1549,9 @@ enum of_overlay_notify_action { OF_OVERLAY_POST_REMOVE, }; -static inline char *of_overlay_action_name(enum of_overlay_notify_action action) +static inline const char *of_overlay_action_name (enum of_overlay_notify_action action) { - static char *of_overlay_action_name[] = { +static const char *const of_overlay_action_name[] = {"init" ,"pre-apply" ,"post-apply" , --2.37.2
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 43 lines · Christian Göttsche
of_overlay_action_name()returns a string literal from a function local array. Modifying string literals is undefined behavior which usage of const pointer can avoid.of_overlay_action_name()is currently only used once inoverlay_notify()to print the returned value.While on it declare the data array const as well.
Reported by Clang:
In file included from
arch/x86/kernel/asm-offsets.c:22:
In file included fromarch/x86/kernel/../kvm/vmx/vmx.h:5:
In file included from./include/linux/kvm_host.h:19:
In file included from./include/linux/msi.h:23:
In file included from./arch/x86/include/asm/msi.h:5:
In file included from./arch/x86/include/asm/irqdomain.h:5:
In file included from./include/linux/irqdomain.h:35:
./include/linux/of.h:1555:3: error: initializing 'char *' with an expression of type 'const char[5]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"init",
^~~~~~
./include/linux/of.h:1556:3: error: initializing 'char *' with an expression of type 'const char[10]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"pre-apply",
^~~~~~~~~~~
./include/linux/of.h:1557:3: error: initializing 'char *' with an expression of type 'const char[11]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"post-apply",
^~~~~~~~~~~~
./include/linux/of.h:1558:3: error: initializing 'char *' with an expression of type 'const char[11]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"pre-remove",
^~~~~~~~~~~~
./include/linux/of.h:1559:3: error: initializing 'char *' with an expression of type 'const char[12]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"post-remove",
^~~~~~~~~~~~~Signed-off-by Christian Göttsche <[email protected]>--- include/linux/of.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)include/linux/of.h +0
-0@@ -1549,9 +1549,9 @@ enum of_overlay_notify_action { OF_OVERLAY_POST_REMOVE,
include/linux/of.h enum of_overlay_notify_action { · line 1557
Quoted 10 lines
include/linux/of.h +2
-2}; -static inline char *of_overlay_action_name (enum of_overlay_notify_action action) +static inline const char *of_overlay_action_name (enum of_overlay_notify_action action) { -static char *of_overlay_action_name[] = { +static const char *const of_overlay_action_name[] = {"init" ,"pre-apply" ,"post-apply" ,
Quoted 32 lines · Christian Göttsche
of_overlay_action_name()returns a string literal from a function local array. Modifying string literals is undefined behavior which usage of const pointer can avoid.of_overlay_action_name()is currently only used once inoverlay_notify()to print the returned value.While on it declare the data array const as well.
Reported by Clang:
In file included from
arch/x86/kernel/asm-offsets.c:22:
In file included fromarch/x86/kernel/../kvm/vmx/vmx.h:5:
In file included from./include/linux/kvm_host.h:19:
In file included from./include/linux/msi.h:23:
In file included from./arch/x86/include/asm/msi.h:5:
In file included from./arch/x86/include/asm/irqdomain.h:5:
In file included from./include/linux/irqdomain.h:35:
./include/linux/of.h:1555:3: error: initializing 'char *' with an expression of type 'const char[5]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"init",
^~~~~~
./include/linux/of.h:1556:3: error: initializing 'char *' with an expression of type 'const char[10]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"pre-apply",
^~~~~~~~~~~
./include/linux/of.h:1557:3: error: initializing 'char *' with an expression of type 'const char[11]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"post-apply",
^~~~~~~~~~~~
./include/linux/of.h:1558:3: error: initializing 'char *' with an expression of type 'const char[11]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"pre-remove",
^~~~~~~~~~~~
./include/linux/of.h:1559:3: error: initializing 'char *' with an expression of type 'const char[12]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
"post-remove",
^~~~~~~~~~~~~
Quoted 5 lines
Signed-off-by Christian Göttsche <[email protected]>--- include/linux/of.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Applied, thanks!