From: Masahiro Yamada <masahi...@kernel.org>

This is a cherry-pick from the kernel commit:
6262afa10ef7c (kconfig: default to zero if int/hex symbol lacks default 
property, 2023-11-26)

When a default property is missing in an int or hex symbol, it defaults
to an empty string, which is not a valid symbol value.

It results in an incorrect .config, and can also lead to an infinite
loop in scripting.

Use "0" for int and "0x0" for hex as a default value.

Signed-off-by: Masahiro Yamada <masahi...@kernel.org>
Reviewed-by: Yoann Congal <yoann.con...@smile.fr>

Signed-off-by: Yoann Congal <yoann.con...@smile.fr>
---
Added context that was not in the upstream commit:
The infinite loop case happens with a configuration defined like this
(a hex config without a valid default value):
  config HEX_TEST
        hex "Hex config without default"

And using:
  $ make oldconfig < /dev/null
  scripts/kconfig/conf  --oldconfig Kconfig
  *
  * General setup
  *

  Error in reading or end of file.

  Error in reading or end of file.
  Hex config without default (HEX_TEST) [] (NEW)

  Error in reading or end of file.
  Hex config without default (HEX_TEST) [] (NEW)
  # This loops forever

NB: Scripted config manipulation often call make with /dev/null as
stdin (Yocto recipe, CI build, ...)

This was discovered when working on Yocto bug:
https://bugzilla.yoctoproject.org/show_bug.cgi?id=14136
---
 scripts/kconfig/symbol.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c
index 703b9b899ee..b4c51de3f20 100644
--- a/scripts/kconfig/symbol.c
+++ b/scripts/kconfig/symbol.c
@@ -343,7 +343,11 @@ void sym_calc_value(struct symbol *sym)
 
        switch (sym->type) {
        case S_INT:
+               newval.val = "0";
+               break;
        case S_HEX:
+               newval.val = "0x0";
+               break;
        case S_STRING:
                newval = symbol_empty.curr;
                break;
@@ -753,15 +757,17 @@ const char *sym_get_string_default(struct symbol *sym)
                case yes: return "y";
                }
        case S_INT:
+               if (!str[0])
+                       str = "0";
+               break;
        case S_HEX:
-               return str;
-       case S_STRING:
-               return str;
-       case S_OTHER:
-       case S_UNKNOWN:
+               if (!str[0])
+                       str = "0x0";
+               break;
+       default:
                break;
        }
-       return "";
+       return str;
 }
 
 const char *sym_get_string_value(struct symbol *sym)
-- 
2.39.2

Reply via email to