* [PATCH -next v2 0/2] Introduce several opposite string choice helpers
@ 2024-09-05 9:25 Hongbo Li
2024-09-05 9:25 ` [PATCH -next v2 1/2] lib/string_choices: " Hongbo Li
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Hongbo Li @ 2024-09-05 9:25 UTC (permalink / raw)
To: kees, andy; +Cc: linux-hardening, lihongbo22, chris.zjh
Similar to the exists helper: str_enable_disable/
str_enabled_disabled/str_on_off/str_yes_no helpers, we can
add the opposite helpers. That's str_disable_enable,
str_disabled_enabled, str_off_on and str_no_yes.
There are more than 10 cases currently (expect
str_disable_enable now has 3 use cases) exist in the code
can be replaced with these helper.
Changes in v2:
- Remove the use cases and only keep the added helpers.
- Add comments to explaing the use of string choices' helpers
as suggested by Andy.
v1: https://lore.kernel.org/netdev/20240831095840.4173362-1-lihongbo22@huawei.com/
Hongbo Li (2):
lib/string_choices: Introduce several opposite string choice helpers
lib/string_choices: Add some comments to make more clear for string
choices helpers.
include/linux/string_choices.h | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH -next v2 1/2] lib/string_choices: Introduce several opposite string choice helpers
2024-09-05 9:25 [PATCH -next v2 0/2] Introduce several opposite string choice helpers Hongbo Li
@ 2024-09-05 9:25 ` Hongbo Li
2024-09-05 9:25 ` [PATCH -next v2 2/2] lib/string_choices: Add some comments to make more clear for string choices helpers Hongbo Li
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Hongbo Li @ 2024-09-05 9:25 UTC (permalink / raw)
To: kees, andy; +Cc: linux-hardening, lihongbo22, chris.zjh
Similar to the exists helper: str_enable_disable/
str_enabled_disabled/str_on_off/str_yes_no helpers, we can
add the opposite helpers. That's str_disable_enable,
str_disabled_enabled, str_off_on and str_no_yes.
There are more than 10 cases currently (expect
str_disable_enable now has 3 use cases) exist in the code
can be replaced with these helper.
Signed-off-by: Hongbo Li <lihongbo22@huawei.com>
---
include/linux/string_choices.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/include/linux/string_choices.h b/include/linux/string_choices.h
index f3670dbd1169..c2134eeda1fd 100644
--- a/include/linux/string_choices.h
+++ b/include/linux/string_choices.h
@@ -8,11 +8,13 @@ static inline const char *str_enable_disable(bool v)
{
return v ? "enable" : "disable";
}
+#define str_disable_enable(v) str_enable_disable(!(v))
static inline const char *str_enabled_disabled(bool v)
{
return v ? "enabled" : "disabled";
}
+#define str_disabled_enabled(v) str_enabled_disabled(!(v))
static inline const char *str_hi_lo(bool v)
{
@@ -36,11 +38,13 @@ static inline const char *str_on_off(bool v)
{
return v ? "on" : "off";
}
+#define str_off_on(v) str_on_off(!(v))
static inline const char *str_yes_no(bool v)
{
return v ? "yes" : "no";
}
+#define str_no_yes(v) str_yes_no(!(v))
static inline const char *str_true_false(bool v)
{
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH -next v2 2/2] lib/string_choices: Add some comments to make more clear for string choices helpers.
2024-09-05 9:25 [PATCH -next v2 0/2] Introduce several opposite string choice helpers Hongbo Li
2024-09-05 9:25 ` [PATCH -next v2 1/2] lib/string_choices: " Hongbo Li
@ 2024-09-05 9:25 ` Hongbo Li
2024-09-05 11:32 ` [PATCH -next v2 0/2] Introduce several opposite string choice helpers Andy Shevchenko
2024-09-05 16:53 ` Kees Cook
3 siblings, 0 replies; 5+ messages in thread
From: Hongbo Li @ 2024-09-05 9:25 UTC (permalink / raw)
To: kees, andy; +Cc: linux-hardening, lihongbo22, chris.zjh
Add some comments to explain why we should use string_choices helpers.
Signed-off-by: Hongbo Li <lihongbo22@huawei.com>
---
include/linux/string_choices.h | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/include/linux/string_choices.h b/include/linux/string_choices.h
index c2134eeda1fd..5a89261d3918 100644
--- a/include/linux/string_choices.h
+++ b/include/linux/string_choices.h
@@ -2,6 +2,19 @@
#ifndef _LINUX_STRING_CHOICES_H_
#define _LINUX_STRING_CHOICES_H_
+/*
+ * Here provide a series of helpers in the str_$TRUE_$FALSE format (you can
+ * also expand some helpers as needed), where $TRUE and $FALSE are their
+ * corresponding literal strings. These helpers can be used in the printing
+ * and also in other places where constant strings are required. Using these
+ * helpers offers the following benefits:
+ * 1) Reducing the hardcoding of strings, which makes the code more elegant
+ * through these simple literal-meaning helpers.
+ * 2) Unifying the output, which prevents the same string from being printed
+ * in various forms, such as enable/disable, enabled/disabled, en/dis.
+ * 3) Deduping by the linker, which results in a smaller binary file.
+ */
+
#include <linux/types.h>
static inline const char *str_enable_disable(bool v)
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH -next v2 0/2] Introduce several opposite string choice helpers
2024-09-05 9:25 [PATCH -next v2 0/2] Introduce several opposite string choice helpers Hongbo Li
2024-09-05 9:25 ` [PATCH -next v2 1/2] lib/string_choices: " Hongbo Li
2024-09-05 9:25 ` [PATCH -next v2 2/2] lib/string_choices: Add some comments to make more clear for string choices helpers Hongbo Li
@ 2024-09-05 11:32 ` Andy Shevchenko
2024-09-05 16:53 ` Kees Cook
3 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2024-09-05 11:32 UTC (permalink / raw)
To: Hongbo Li; +Cc: kees, linux-hardening, chris.zjh
On Thu, Sep 05, 2024 at 05:25:38PM +0800, Hongbo Li wrote:
> Similar to the exists helper: str_enable_disable/
> str_enabled_disabled/str_on_off/str_yes_no helpers, we can
> add the opposite helpers. That's str_disable_enable,
> str_disabled_enabled, str_off_on and str_no_yes.
>
> There are more than 10 cases currently (expect
> str_disable_enable now has 3 use cases) exist in the code
> can be replaced with these helper.
Acked-by: Andy Shevchenko <andy@kernel.org>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH -next v2 0/2] Introduce several opposite string choice helpers
2024-09-05 9:25 [PATCH -next v2 0/2] Introduce several opposite string choice helpers Hongbo Li
` (2 preceding siblings ...)
2024-09-05 11:32 ` [PATCH -next v2 0/2] Introduce several opposite string choice helpers Andy Shevchenko
@ 2024-09-05 16:53 ` Kees Cook
3 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2024-09-05 16:53 UTC (permalink / raw)
To: andy, Hongbo Li; +Cc: Kees Cook, linux-hardening, chris.zjh
On Thu, 05 Sep 2024 17:25:38 +0800, Hongbo Li wrote:
> Similar to the exists helper: str_enable_disable/
> str_enabled_disabled/str_on_off/str_yes_no helpers, we can
> add the opposite helpers. That's str_disable_enable,
> str_disabled_enabled, str_off_on and str_no_yes.
>
> There are more than 10 cases currently (expect
> str_disable_enable now has 3 use cases) exist in the code
> can be replaced with these helper.
>
> [...]
Applied to for-next/hardening, thanks!
[1/2] lib/string_choices: Introduce several opposite string choice helpers
https://git.kernel.org/kees/c/c2708ba91c3c
[2/2] lib/string_choices: Add some comments to make more clear for string choices helpers.
https://git.kernel.org/kees/c/c121d5cc3a99
Take care,
--
Kees Cook
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-09-05 16:53 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-05 9:25 [PATCH -next v2 0/2] Introduce several opposite string choice helpers Hongbo Li
2024-09-05 9:25 ` [PATCH -next v2 1/2] lib/string_choices: " Hongbo Li
2024-09-05 9:25 ` [PATCH -next v2 2/2] lib/string_choices: Add some comments to make more clear for string choices helpers Hongbo Li
2024-09-05 11:32 ` [PATCH -next v2 0/2] Introduce several opposite string choice helpers Andy Shevchenko
2024-09-05 16:53 ` Kees Cook
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.