From: Julian Braha <julianbraha@gmail.com>
To: nathan@kernel.org, nsc@kernel.org
Cc: n.schier@fritz.com, grahamr@qti.qualcomm.com,
rdunlap@infradead.org, nico@fluxnic.net,
linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
Julian Braha <julianbraha@gmail.com>
Subject: [PATCH v2 2/2] kconfig: error on reverse range
Date: Wed, 14 Oct 2026 14:40:17 +0100 [thread overview]
Message-ID: <20261014134017.2310819-3-julianbraha@gmail.com> (raw)
In-Reply-To: <20261014134017.2310819-1-julianbraha@gmail.com>
As discussed on kconfirm v4 [1], it was decided to move checks directly
into the kconfig interpreter once there are no findings from kconfirm.
This "reverse range" currently has no findings across the tree, so let's
add the check.
Currently, if a reverse range is attempted, such as:
config EXAMPLE
int "Reverse range example"
range 2 1
then the value of the option defaults to its "lower" limit of 2. If the
option is visible in the frontend, then the user gets trapped in the value
selection interface, since there is no value that is actually valid for
the user to set.
Note that this only checks ranges that use constant values. Statically
checking ranges using symbols would require SAT solving, or similar.
In the future, the code should be refactored to store range values as
numerics so we can avoid calling strtoll() and strtoull() multiple times.
Link: https://lore.kernel.org/all/20260904220559.GB2787252@ax162/ [1]
Assisted-by: LLM
Signed-off-by: Julian Braha <julianbraha@gmail.com>
---
Changes since v1:
- fix typo in commit message: where -> there (Nicolas)
- handle case with multiple (conditional) ranges (sashiko)
Link to v1:
https://lore.kernel.org/linux-kbuild/20260922163749.2031594-1-julianbraha@gmail.com/
---
| 22 +++-
.../tests/err_num_reverse_range/Kconfig | 111 ++++++++++++++++++
.../tests/err_num_reverse_range/__init__.py | 8 ++
.../err_num_reverse_range/expected_stderr | 11 ++
4 files changed, 148 insertions(+), 4 deletions(-)
create mode 100644 scripts/kconfig/tests/err_num_reverse_range/Kconfig
create mode 100644 scripts/kconfig/tests/err_num_reverse_range/__init__.py
create mode 100644 scripts/kconfig/tests/err_num_reverse_range/expected_stderr
--git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c
index db5ba57125c5..1a3ad09a92d2 100644
--- a/scripts/kconfig/menu.c
+++ b/scripts/kconfig/menu.c
@@ -274,9 +274,9 @@ static int menu_validate_number(struct symbol *sym, struct symbol *sym2,
static int sym_check_prop(struct symbol *sym)
{
struct property *prop;
- struct symbol *sym2;
+ struct symbol *sym2, *lower, *upper;
char *use;
- int errors = 0;
+ int errors = 0, numeric_errors;
for (prop = sym->prop; prop; prop = prop->next) {
switch (prop->type) {
@@ -325,8 +325,22 @@ static int sym_check_prop(struct symbol *sym)
if (sym->type != S_INT && sym->type != S_HEX)
prop_warn(prop, "range is only allowed "
"for int or hex symbols");
- errors += menu_validate_number(sym, prop->expr->left.sym, prop);
- errors += menu_validate_number(sym, prop->expr->right.sym, prop);
+ lower = prop->expr->left.sym;
+ upper = prop->expr->right.sym;
+ numeric_errors = menu_validate_number(sym, lower, prop);
+ numeric_errors += menu_validate_number(sym, upper, prop);
+ errors += numeric_errors;
+ if (numeric_errors || lower->type != S_UNKNOWN || upper->type != S_UNKNOWN)
+ break;
+
+ if ((sym->type == S_INT &&
+ strtoll(lower->name, NULL, 10) > strtoll(upper->name, NULL, 10)) ||
+ (sym->type == S_HEX &&
+ strtoull(lower->name, NULL, 16) > strtoull(upper->name, NULL, 16))) {
+ prop_err(prop, "range lower limit '%s' is greater than upper limit '%s'",
+ lower->name, upper->name);
+ errors++;
+ }
break;
default:
;
diff --git a/scripts/kconfig/tests/err_num_reverse_range/Kconfig b/scripts/kconfig/tests/err_num_reverse_range/Kconfig
new file mode 100644
index 000000000000..c55091aca274
--- /dev/null
+++ b/scripts/kconfig/tests/err_num_reverse_range/Kconfig
@@ -0,0 +1,111 @@
+# SPDX-License-Identifier: GPL-2.0
+# Check constant range bounds.
+
+config INT_ORDERED
+ int
+ range -10 10
+
+config INT_EQUAL
+ int
+ range 5 5
+
+config HEX_ORDERED
+ hex
+ range 0x7fffffffffffffff 0xffffffffffffffff
+
+config HEX_EQUAL
+ hex
+ range 0xffffffffffffffff 0xffffffffffffffff
+
+# Skip ranges with symbolic bounds.
+
+config INT_LOWER
+ int
+
+config INT_UPPER
+ int
+
+config INT_SYMBOL_LOWER
+ int
+ range INT_LOWER 0
+
+config INT_SYMBOL_UPPER
+ int
+ range 0 INT_UPPER
+
+config INT_SYMBOL_BOTH
+ int
+ range INT_LOWER INT_UPPER
+
+config HEX_LOWER
+ hex
+
+config HEX_UPPER
+ hex
+
+config HEX_SYMBOL_LOWER
+ hex
+ range HEX_LOWER 0x0
+
+config HEX_SYMBOL_UPPER
+ hex
+ range 0x0 HEX_UPPER
+
+config HEX_SYMBOL_BOTH
+ hex
+ range HEX_LOWER HEX_UPPER
+
+# Reject reversed constant bounds.
+
+config INT_REVERSED
+ int
+ range 10 2
+
+config INT_REVERSED_NEGATIVE
+ int
+ range -1 -10
+
+config INT_REVERSED_SIGNED
+ int
+ range 1 -1
+
+config INT_REVERSED_QUOTED
+ int
+ range "8" "2"
+
+config HEX_REVERSED
+ hex
+ range 0x10 0xf
+
+config HEX_REVERSED_HIGH
+ hex
+ range 0xffffffffffffffff 0x8000000000000000
+
+config HEX_REVERSED_UNPREFIXED
+ hex
+ range f a
+
+# Check each conditional range independently.
+
+config RANGE_CONDITION
+ bool
+
+config INT_REVERSED_FIRST
+ int
+ range 10 2 if RANGE_CONDITION
+ range -10 10 if !RANGE_CONDITION
+
+config INT_REVERSED_SECOND
+ int
+ range -10 10 if RANGE_CONDITION
+ range 10 2 if !RANGE_CONDITION
+
+config HEX_REVERSED_FIRST
+ hex
+ range 0x10 0xf if RANGE_CONDITION
+ range 0x0 0x10 if !RANGE_CONDITION
+
+config HEX_REVERSED_SECOND
+ hex
+ range 0x0 0x10 if RANGE_CONDITION
+ range 0x10 0xf if !RANGE_CONDITION
diff --git a/scripts/kconfig/tests/err_num_reverse_range/__init__.py b/scripts/kconfig/tests/err_num_reverse_range/__init__.py
new file mode 100644
index 000000000000..4a7219bbe49e
--- /dev/null
+++ b/scripts/kconfig/tests/err_num_reverse_range/__init__.py
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+"""
+Reject constant range bounds when the lower bound is greater than the upper.
+"""
+
+def test(conf):
+ assert conf.olddefconfig() == 1
+ assert conf.stderr_matches('expected_stderr')
diff --git a/scripts/kconfig/tests/err_num_reverse_range/expected_stderr b/scripts/kconfig/tests/err_num_reverse_range/expected_stderr
new file mode 100644
index 000000000000..8742f3f46469
--- /dev/null
+++ b/scripts/kconfig/tests/err_num_reverse_range/expected_stderr
@@ -0,0 +1,11 @@
+Kconfig:62: error: range lower limit '10' is greater than upper limit '2'
+Kconfig:66: error: range lower limit '-1' is greater than upper limit '-10'
+Kconfig:70: error: range lower limit '1' is greater than upper limit '-1'
+Kconfig:74: error: range lower limit '8' is greater than upper limit '2'
+Kconfig:78: error: range lower limit '0x10' is greater than upper limit '0xf'
+Kconfig:82: error: range lower limit '0xffffffffffffffff' is greater than upper limit '0x8000000000000000'
+Kconfig:86: error: range lower limit 'f' is greater than upper limit 'a'
+Kconfig:95: error: range lower limit '10' is greater than upper limit '2'
+Kconfig:101: error: range lower limit '10' is greater than upper limit '2'
+Kconfig:105: error: range lower limit '0x10' is greater than upper limit '0xf'
+Kconfig:111: error: range lower limit '0x10' is greater than upper limit '0xf'
--
2.55.0
prev parent reply other threads:[~2026-10-14 13:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-14 13:40 [PATCH v2 0/2] " Julian Braha
2026-10-14 13:40 ` [PATCH v2 1/2] kconfig: add prop_err function to simplify property error handling Julian Braha
2026-10-14 13:40 ` Julian Braha [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261014134017.2310819-3-julianbraha@gmail.com \
--to=julianbraha@gmail.com \
--cc=grahamr@qti.qualcomm.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=n.schier@fritz.com \
--cc=nathan@kernel.org \
--cc=nico@fluxnic.net \
--cc=nsc@kernel.org \
--cc=rdunlap@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®