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>
This commit is contained in:
Julian Braha 2026-09-15 22:15:06 +01:00 committed by Nathan Chancellor
parent a5e84d6f6d
commit d13bad83d8
No known key found for this signature in database
GPG Key ID: 1D6B269171C01A96
4 changed files with 52 additions and 1 deletions

View File

@ -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 ||

View File

@ -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

View File

@ -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')

View File

@ -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'