diff mbox series

[2/2] lib: tst_kconfig: Fix segfaults on variable value mismatch

Message ID 20210915143725.13170-3-chrubis@suse.cz
State Accepted
Headers show
Series Two small fixes for kconfig parser | expand

Commit Message

Cyril Hrubis Sept. 15, 2021, 2:37 p.m. UTC
If we add CONFIG_FOO=value to the test .needs_kconfig while the kernel
config will not define the particular value we will blindly call
strlen() on the val->val and end up with SegFault. Fix this by checking
that the variable has value set first.

Also initialize the val->val to NULL so that we get segfaults 100% of
the time when we attempt to dereference the value when not defined.

+ Test

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 lib/newlib_tests/config07 | 5 +++++
 lib/tst_kconfig.c         | 4 ++++
 2 files changed, 9 insertions(+)
 create mode 100644 lib/newlib_tests/config07

Comments

Richard Palethorpe Sept. 16, 2021, 10:41 a.m. UTC | #1
Hello,

Reviewed-by: Richard Palethorpe <rpalethorpe@suse.com>
Cyril Hrubis Sept. 16, 2021, 1:06 p.m. UTC | #2
Hi!
Thanks for the review, pushed.
diff mbox series

Patch

diff --git a/lib/newlib_tests/config07 b/lib/newlib_tests/config07
new file mode 100644
index 000000000..dd67120f7
--- /dev/null
+++ b/lib/newlib_tests/config07
@@ -0,0 +1,5 @@ 
+# The default hostaname value mismatch
+CONFIG_MMU=y
+CONFIG_EXT4_FS=m
+CONFIG_PGTABLE_LEVELS=4
+CONFIG_DEFAULT_HOSTNAME=m
diff --git a/lib/tst_kconfig.c b/lib/tst_kconfig.c
index c9de3017b..5af67b0a5 100644
--- a/lib/tst_kconfig.c
+++ b/lib/tst_kconfig.c
@@ -392,6 +392,7 @@  static inline unsigned int populate_vars(struct tst_expr *exprs[],
 			strncpy(vars[cnt].id, j->tok, vars[cnt].id_len);
 			vars[cnt].id[vars[cnt].id_len] = 0;
 			vars[cnt].choice = 0;
+			vars[cnt].val = NULL;
 
 			var = find_var(vars, cnt, vars[cnt].id);
 
@@ -435,6 +436,9 @@  static int map(struct tst_expr_tok *expr)
 	if (choice != 'v')
 		return var->choice == choice;
 
+	if (var->choice != 'v')
+		return 0;
+
 	if (strlen(var->val) != len)
 		return 0;