From af65073a075b162ccf6e3dd8fb925f29d5743ca3 Mon Sep 17 00:00:00 2001 From: Toomas Soome Date: Wed, 28 Sep 2022 03:09:21 +0300 Subject: [PATCH] btree_test: smatch did detect few issues Add missing header. Properly ignore return values. Memory leak/unchecked malloc. We do allocate a bit too early (and fail to validate the result). From this, smatch is angry when we overwrite the value of 'node' later. Reviewed-by: Brian Behlendorf Reviewed-by: Igor Kozhukhov Reviewed-by: Richard Yao Signed-off-by: Toomas Soome Closes #13941 --- tests/zfs-tests/cmd/btree_test.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/zfs-tests/cmd/btree_test.c b/tests/zfs-tests/cmd/btree_test.c index 456a36f31..4e2023003 100644 --- a/tests/zfs-tests/cmd/btree_test.c +++ b/tests/zfs-tests/cmd/btree_test.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -164,7 +165,7 @@ find_without_index(zfs_btree_t *bt, char *why) zfs_btree_add(bt, &i); if ((p = (u_longlong_t *)zfs_btree_find(bt, &i, NULL)) == NULL || *p != i) { - snprintf(why, BUFSIZE, "Unexpectedly found %llu\n", + (void) snprintf(why, BUFSIZE, "Unexpectedly found %llu\n", p == NULL ? 0 : *p); return (1); } @@ -172,7 +173,7 @@ find_without_index(zfs_btree_t *bt, char *why) i++; if ((p = (u_longlong_t *)zfs_btree_find(bt, &i, NULL)) != NULL) { - snprintf(why, BUFSIZE, "Found bad value: %llu\n", *p); + (void) snprintf(why, BUFSIZE, "Found bad value: %llu\n", *p); return (1); } @@ -189,10 +190,10 @@ insert_find_remove(zfs_btree_t *bt, char *why) /* Insert 'i' into the tree, and attempt to find it again. */ zfs_btree_add(bt, &i); if ((p = (u_longlong_t *)zfs_btree_find(bt, &i, &bt_idx)) == NULL) { - snprintf(why, BUFSIZE, "Didn't find value in tree\n"); + (void) snprintf(why, BUFSIZE, "Didn't find value in tree\n"); return (1); } else if (*p != i) { - snprintf(why, BUFSIZE, "Found (%llu) in tree\n", *p); + (void) snprintf(why, BUFSIZE, "Found (%llu) in tree\n", *p); return (1); } ASSERT3S(zfs_btree_numnodes(bt), ==, 1); @@ -201,7 +202,8 @@ insert_find_remove(zfs_btree_t *bt, char *why) /* Remove 'i' from the tree, and verify it is not found. */ zfs_btree_remove(bt, &i); if ((p = (u_longlong_t *)zfs_btree_find(bt, &i, &bt_idx)) != NULL) { - snprintf(why, BUFSIZE, "Found removed value (%llu)\n", *p); + (void) snprintf(why, BUFSIZE, + "Found removed value (%llu)\n", *p); return (1); } ASSERT3S(zfs_btree_numnodes(bt), ==, 0); @@ -240,9 +242,12 @@ drain_tree(zfs_btree_t *bt, char *why) zfs_btree_add_idx(bt, &randval, &bt_idx); node = malloc(sizeof (int_node_t)); + ASSERT3P(node, !=, NULL); + node->data = randval; if ((ret = avl_find(&avl, node, &avl_idx)) != NULL) { - snprintf(why, BUFSIZE, "Found in avl: %llu\n", randval); + (void) snprintf(why, BUFSIZE, + "Found in avl: %llu\n", randval); return (1); } avl_insert(&avl, node, avl_idx); @@ -422,7 +427,8 @@ do_negative_test(zfs_btree_t *bt, char *test_name) { int rval = 0; struct rlimit rlim = {0}; - setrlimit(RLIMIT_CORE, &rlim); + + (void) setrlimit(RLIMIT_CORE, &rlim); if (strcmp(test_name, "insert_duplicate") == 0) { rval = insert_duplicate(bt);