Skip to contentSkip to conversation

tools/sparse-ltp: Check all struct arrays in tst_test for ending {}

PATCH In review Richard Palethorpe via ltp 3 participants 5 messages Mar 14, 2022 – Mar 15, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tools/sparse-ltp: Check all struct arrays in tst_test for ending {}

Check for null termination on tst_test.save_restore and all the other struct arrays in tst_test. They are all null terminated.

Also prevents a segfault when scalar NULL is used as the final entry and print a helpful error message instead.

---
 tools/sparse/sparse-ltp.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
tools/sparse/sparse-ltp.c +20 -9
index 608025800..d649ee7b7 100644
@@ -178,27 +178,31 @@ static bool is_terminated_with_null_struct(const struct symbol *const sym)
 	if (item_init->type == EXPR_POS)
 		item_init = item_init->init_expr;
 
+	if (item_init->type != EXPR_INITIALIZER)
+		return false;
+
 	return ptr_list_empty((struct ptr_list *)item_init->expr_list);
 }
 
-/* Check for (one instance of) LTP-005
+/* LTP-005: Check array sentinel value
  *
- * The tags array is only accessed when the test fails. So we perform
- * a static check to ensure it ends with {}
+ * This is most important for the tags array. It is only accessed when
+ * the test fails. So we perform a static check to ensure it ends with
+ * {}.
  */
-static void check_tag_initializer(const struct symbol *const sym)
+static void check_struct_array_initializer(const struct symbol *const sym)
 {
 	if (is_terminated_with_null_struct(sym))
 		return;
 
 	warning(sym->pos,
-		"LTP-005: test.tags array doesn't appear to be null-terminated; did you forget to add '{}' as the final entry?");
+		"LTP-005: Struct array doesn't appear to be null-terminated; did you forget to add '{}' as the final entry?");
 }
 
 /* Find struct tst_test test = { ... } and perform tests on its initializer */
 static void check_test_struct(const struct symbol *const sym)
 {
-	static struct ident *tst_test, *tst_test_test, *tst_tag;
+	static struct ident *tst_test, *tst_test_test;
 	struct ident *ctype_name = NULL;
 	struct expression *init = sym->initializer;
 	struct expression *entry;
@@ -214,7 +218,6 @@ static void check_test_struct(const struct symbol *const sym)
 	if (!tst_test_test) {
 		tst_test = built_in_ident("tst_test");
 		tst_test_test = built_in_ident("test");
-		tst_tag = built_in_ident("tst_tag");
 	}
 
 	if (sym->ident != tst_test_test)
@@ -227,11 +230,19 @@ static void check_test_struct(const struct symbol *const sym)
 		if (entry->init_expr->type != EXPR_SYMBOL)
 			continue;
 
+		switch (entry->ctype->ctype.base_type->type) {
+		case SYM_PTR:
+		case SYM_ARRAY:
+			break;
+		default:
+			return;
+		}
+
 		const struct symbol *entry_init = entry->init_expr->symbol;
 		const struct symbol *entry_ctype = unwrap_base_type(entry_init);
 
-		if (entry_ctype->ident == tst_tag)
-			check_tag_initializer(entry_init);
+		if (entry_ctype->type == SYM_STRUCT)
+			check_struct_array_initializer(entry_init);
 	} END_FOR_EACH_PTR(entry);
 
 }
Signature · 5 lines
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 2/2 LTP-005: Switch opts terminator to {}

---
 .../kernel/security/integrity/ima/src/ima_boot_aggregate.c      | 2 +-
 testcases/network/nfs/nfs_stress/nfs05_make_tree.c              | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
testcases/kernel/security/integrity/ima/src/ima_boot_aggregate.c +1 -1
index 05c20e5ae..62468e0d1 100644
@@ -125,7 +125,7 @@ static void do_test(void)
 static struct tst_option options[] = {
 	{"d", &debug, "Enable debug"},
 	{"f:", &file, "binary_bios_measurement file (required)\n"},
-	{NULL, NULL, NULL}
+	{}
 };
 
 static struct tst_test test = {
testcases/network/nfs/nfs_stress/nfs05_make_tree.c +1 -1
index add55e156..fdc13bd5d 100644
@@ -63,7 +63,7 @@ static struct tst_option opts[] = {
 	{"t:", &t_arg, "Number of threads to generate, default: 8"},
 	{"d:", &d_arg, "Number of subdirs to generate, default: 100"},
 	{"f:", &f_arg, "Number of c files in each dir, default: 100"},
-	{NULL, NULL, NULL}
+	{}
 };
 
 static void run_targets(const char *dirname, char *cfile, pid_t tid)
Signature · 5 lines
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 2/2
#2 lore raw

Re: [LTP] [PATCH 2/2] LTP-005: Switch opts terminator to {}

Hi Richie,

obviously correct. Thanks!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/2
#3 lore raw
In reply to Richard Palethorpe via ltp

Hi Richie,

Quoted 5 lines

Check for null termination on tst_test.save_restore and all the other struct arrays in tst_test. They are all null terminated.

Also prevents a segfault when scalar NULL is used as the final entry and print a helpful error message instead.

thanks for this! LGTM.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hello Petr,

Quoted 5 lines · Petr Vorel

Hi Richie,

Quoted 2 lines

Check for null termination on tst_test.save_restore and all the other struct arrays in tst_test. They are all null terminated.

Quoted 8 lines
Quoted 2 lines

Also prevents a segfault when scalar NULL is used as the final entry and print a helpful error message instead.

thanks for this! LGTM.

Kind regards,
Petr

Thanks merged!

Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp