Skip to contentSkip to conversation

kconfig: error on reverse range

PATCH In review Julian Braha 3 participants 5 messages Oct 14 – Nov 5

Subsystems (2)
lore
Share & tools
lore

Julian Braha

author reviewer
#0 lore raw

v2 cover kconfig: error on reverse range

This series adds more input validation to Kconfig, specifically checking that ranges for numeric options are properly ordered. This check was originally introduced as part of kconfirm [1], but there are no findings for this across the tree, so the check can be added directly into Kconfig.

Patch 1/2 adds a prop_err() function to match the existing prop_warn(), which is then used in patch 2/2 where the check is introduced.

[1] https://lore.kernel.org/all/20260904220559.GB2787252@ax162/
---
Changes since v1:
- fix typo in commit message: where -> there (Nicolas)
- adjust warning and error printing to match gcc (Nicolas)
- handle case with multiple (conditional) ranges (sashiko)

Link to v1:
https://lore.kernel.org/linux-kbuild/[email protected]/
---
Julian Braha (2):
kconfig: add prop_err function to simplify property error handling
kconfig: error on reverse range

 scripts/kconfig/menu.c                        |  49 +++++---
 .../tests/err_num_reverse_range/Kconfig       | 111 ++++++++++++++++++
 .../tests/err_num_reverse_range/__init__.py   |   8 ++
 .../err_num_reverse_range/expected_stderr     |  11 ++
 .../tests/err_transitional/expected_stderr    |   2 +-
 5 files changed, 166 insertions(+), 15 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
Signature · 1 line
2.55.0
author reviewer
#1 lore raw

PATCH v2 1/2 kconfig: add prop_err function to simplify property error handling

As suggested by Nicolas while reviewing another Kconfig property error [1], we can add a prop_err() function to simplify the tracking and display of Kconfig property errors.

Note that the formatting of the warning printing is changed slightly (adding a space) to better match the formatting used by gcc. This causes a change to the expected test output for the existing warnings that call prop_warn() and menu_warn().

---
Changes since v1:
- adjust warning and error printing to match gcc (Nicolas)

Link to v1:
https://lore.kernel.org/linux-kbuild/[email protected]/

---
 scripts/kconfig/menu.c                        | 27 ++++++++++++-------
 .../tests/err_transitional/expected_stderr    |  2 +-
 2 files changed, 18 insertions(+), 11 deletions(-)
scripts/kconfig/menu.c +17 -10
index bda3e2650216..db5ba57125c5 100644
@@ -43,7 +43,7 @@ void menu_warn(const struct menu *menu, const char *fmt, ...)
 {
 	va_list ap;
 	va_start(ap, fmt);
-	fprintf(stderr, "%s:%d:warning: ", menu->filename, menu->lineno);
+	fprintf(stderr, "%s:%d: warning: ", menu->filename, menu->lineno);
 	vfprintf(stderr, fmt, ap);
 	fprintf(stderr, "\n");
 	va_end(ap);
@@ -53,7 +53,17 @@ static void prop_warn(const struct property *prop, const char *fmt, ...)
 {
 	va_list ap;
 	va_start(ap, fmt);
-	fprintf(stderr, "%s:%d:warning: ", prop->filename, prop->lineno);
+	fprintf(stderr, "%s:%d: warning: ", prop->filename, prop->lineno);
+	vfprintf(stderr, fmt, ap);
+	fprintf(stderr, "\n");
+	va_end(ap);
+}
+
+static void prop_err(const struct property *prop, const char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	fprintf(stderr, "%s:%d: error: ", prop->filename, prop->lineno);
 	vfprintf(stderr, fmt, ap);
 	fprintf(stderr, "\n");
 	va_end(ap);
@@ -246,18 +256,15 @@ static int menu_validate_number(struct symbol *sym, struct symbol *sym2,
 
 	if (sym2->type != S_UNKNOWN ||
 		!sym_string_valid(sym, sym2->name)) {
-		fprintf(stderr, "%s:%d: error: '%s' is an invalid value for '%s'\n",
-			prop->filename, prop->lineno, sym2->name,
-			sym_type_name(sym->type));
+		prop_err(prop, "'%s' is an invalid value for '%s'",
+			 sym2->name, sym_type_name(sym->type));
 		return 1;
 	}
 
 	if (!sym_string_check_bounds(sym, sym2->name)) {
-		fprintf(stderr,
-			"%s:%d: error: %s constant '%s' is outside the 64-bit %s bounds\n",
-			prop->filename, prop->lineno, sym_type_name(sym->type),
-			sym2->name, sym->type == S_INT ? "signed" : "unsigned");
-
+		prop_err(prop, "%s constant '%s' is outside the 64-bit %s bounds",
+			 sym_type_name(sym->type), sym2->name,
+			 sym->type == S_INT ? "signed" : "unsigned");
 		return 1;
 	}
 
scripts/kconfig/tests/err_transitional/expected_stderr +1 -1
index b52db4f680f4..ba317850c043 100644
@@ -1,4 +1,4 @@
-Kconfig:46:warning: config symbol defined without type
+Kconfig:46: warning: config symbol defined without type
 Kconfig:7: error: transitional symbols can only have help sections
 Kconfig:14: error: transitional symbols can only have help sections
 Kconfig:21: error: transitional symbols can only have help sections
Signature · 1 line
2.55.0
author reviewer
#2 lore raw

PATCH v2 2/2 kconfig: error on reverse range

In reply to Julian Braha

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.

---
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/[email protected]/

---
 scripts/kconfig/menu.c                        |  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
scripts/kconfig/menu.c +18 -4
index db5ba57125c5..1a3ad09a92d2 100644
@@ -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:
 			;
scripts/kconfig/tests/err_num_reverse_range/Kconfig new file +111 -0

111 lines not on this page · raw message

scripts/kconfig/tests/err_num_reverse_range/__init__.py new file +8 -0
new file mode 100644
index 000000000000..4a7219bbe49e
@@ -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')
scripts/kconfig/tests/err_num_reverse_range/expected_stderr new file +11 -0
new file mode 100644
index 000000000000..8742f3f46469
@@ -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'
Signature · 1 line
2.55.0
maintainer Reviewed-by on the v2 series
#3 lore raw
In reply to Julian Braha
Quoted 15 lines

This series adds more input validation to Kconfig, specifically checking that ranges for numeric options are properly ordered. This check was originally introduced as part of kconfirm [1], but there are no findings for this across the tree, so the check can be added directly into Kconfig.

Patch 1/2 adds a prop_err() function to match the existing prop_warn(), which is then used in patch 2/2 where the check is introduced.

[1] https://lore.kernel.org/all/20260904220559.GB2787252@ax162/
---
Changes since v1:
- fix typo in commit message: where -> there (Nicolas)
- adjust warning and error printing to match gcc (Nicolas)
- handle case with multiple (conditional) ranges (sashiko)

Quoted 6 lines

Link to v1:
https://lore.kernel.org/linux-kbuild/[email protected]/
---
Julian Braha (2):
kconfig: add prop_err function to simplify property error handling
kconfig: error on reverse range

Signature · 2 lines
Cheers,
Nathan
maintainer
#4 lore raw
In reply to Julian Braha
Quoted 9 lines · Julian Braha

This series adds more input validation to Kconfig, specifically checking that ranges for numeric options are properly ordered. This check was originally introduced as part of kconfirm [1], but there are no findings for this across the tree, so the check can be added directly into Kconfig.

Patch 1/2 adds a prop_err() function to match the existing prop_warn(), which is then used in patch 2/2 where the check is introduced.

[...]

Applied to kbuild/linux.git (kbuild-next-unstable), thanks!

[1/2] kconfig: add prop_err function to simplify property error handling

      https://git.kernel.org/kbuild/c/cff21174
[2/2] kconfig: error on reverse range
      https://git.kernel.org/kbuild/c/fa68f88e

Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped, reverted or modified (e.g. trailers).

Patches applied to the kbuild-next-unstable branch are accepted pending wider testing in linux-next and any post-commit review; they will generally be moved to the kbuild-next branch in about a week if no issues are found.

Best regards,

Signature · 1 line
Nicolas