summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Braha <julianbraha@gmail.com>2026-09-15 22:15:06 +0100
committerNathan Chancellor <nathan@kernel.org>2026-09-17 16:31:00 -0700
commitd13bad83d89d1022fee254c08f6e01084eb4395e (patch)
tree2b85fa9a6c1ad4c7ef23d6c4caafd3ed480c8967
parenta5e84d6f6df1fdb64c06fdb64894199aaf5ec899 (diff)
downloadlinux-next-d13bad83d89d1022fee254c08f6e01084eb4395e.tar.gz
linux-next-d13bad83d89d1022fee254c08f6e01084eb4395e.zip
kconfig: check for hex and int mismatches
Using a numeric option of one type (e.g. 'int') to determine the value of a different numeric type (e.g. 'hex') currently fails silently in various ways if attempted, because the underlying string representation is naively reused. Example 1: config I int default -1 config HEX_DEFAULT_INT hex default I Here, HEX_DEFAULT_INT actually gets set to '0x-1', which is of course not a valid hex value. Example 2: config H hex default A config INT_DEFAULT_HEX int default H Here, INT_DEFAULT_HEX actually gets set to 'A', without even converting into the base-10 equivalent of 10. This value, 'A', is otherwise a rejected int value if entered in the frontend, or read in from an existing .config file. These int-hex mismatches currently do not appear anywhere in the tree, so it is already safe to make these error out. Assisted-by: LLM Signed-off-by: Julian Braha <julianbraha@gmail.com> Reviewed-by: Nicolas Schier <n.schier@fritz.com> Link: https://patch.msgid.link/20260915211508.291790-4-julianbraha@gmail.com Signed-off-by: Nathan Chancellor <nathan@kernel.org>
-rw-r--r--scripts/kconfig/menu.c2
-rw-r--r--scripts/kconfig/tests/err_num_mismatch/Kconfig38
-rw-r--r--scripts/kconfig/tests/err_num_mismatch/__init__.py9
-rw-r--r--scripts/kconfig/tests/err_num_mismatch/expected_stderr4
4 files changed, 52 insertions, 1 deletions
diff --git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c
index ede791a2fe1b..2d8b0c65ce1e 100644
--- a/scripts/kconfig/menu.c
+++ b/scripts/kconfig/menu.c
@@ -244,7 +244,7 @@ static int menu_validate_number(struct symbol *sym, struct symbol *sym2,
if (sym->type != S_INT && sym->type != S_HEX)
return 0;
- if (sym2->type == S_INT || sym2->type == S_HEX)
+ if (sym2->type == sym->type)
return 0;
if (sym2->type != S_UNKNOWN ||
diff --git a/scripts/kconfig/tests/err_num_mismatch/Kconfig b/scripts/kconfig/tests/err_num_mismatch/Kconfig
new file mode 100644
index 000000000000..8406f3bb6419
--- /dev/null
+++ b/scripts/kconfig/tests/err_num_mismatch/Kconfig
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0
+# Test 'int' and 'hex' symbols that reference each other
+
+config INT_SOURCE
+ int
+
+config HEX_SOURCE
+ hex
+
+# 'hex' reference from 'int'
+
+config INT_DEFAULT_HEX
+ int
+ default HEX_SOURCE
+
+config INT_RANGE_HEX
+ int
+ range HEX_SOURCE 1
+
+# A hex symbol must not reference an int symbol
+
+config HEX_DEFAULT_INT
+ hex
+ default INT_SOURCE
+
+config HEX_RANGE_INT
+ hex
+ range INT_SOURCE 0x1
+
+# Referencing the same type is valid
+
+config INT_FROM_INT
+ int
+ default INT_SOURCE
+
+config HEX_FROM_HEX
+ hex
+ range 0 HEX_SOURCE
diff --git a/scripts/kconfig/tests/err_num_mismatch/__init__.py b/scripts/kconfig/tests/err_num_mismatch/__init__.py
new file mode 100644
index 000000000000..275f2a6e9a5b
--- /dev/null
+++ b/scripts/kconfig/tests/err_num_mismatch/__init__.py
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0
+"""
+Reject direct references ('default' or 'range') between int and hex options.
+"""
+
+
+def test(conf):
+ assert conf.olddefconfig() == 1
+ assert conf.stderr_matches('expected_stderr')
diff --git a/scripts/kconfig/tests/err_num_mismatch/expected_stderr b/scripts/kconfig/tests/err_num_mismatch/expected_stderr
new file mode 100644
index 000000000000..587c34467ae6
--- /dev/null
+++ b/scripts/kconfig/tests/err_num_mismatch/expected_stderr
@@ -0,0 +1,4 @@
+Kconfig:14: error: 'HEX_SOURCE' is an invalid value for 'integer'
+Kconfig:18: error: 'HEX_SOURCE' is an invalid value for 'integer'
+Kconfig:24: error: 'INT_SOURCE' is an invalid value for 'hex'
+Kconfig:28: error: 'INT_SOURCE' is an invalid value for 'hex'