diff mbox series

[net-next] lib: packing: catch kunit_kzalloc() failure in the pack() test

Message ID 20241004110012.1323427-1-vladimir.oltean@nxp.com (mailing list archive)
State New
Delegated to: Netdev Maintainers
Headers show
Series [net-next] lib: packing: catch kunit_kzalloc() failure in the pack() test | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 6 this patch: 6
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: akpm@linux-foundation.org
netdev/build_clang success Errors and warnings before: 6 this patch: 6
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 5 this patch: 5
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 7 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-10-05--03-00 (tests: 774)

Commit Message

Vladimir Oltean Oct. 4, 2024, 11 a.m. UTC
kunit_kzalloc() may fail. Other call sites verify that this is the case,
either using a direct comparison with the NULL pointer, or the
KUNIT_ASSERT_NOT_NULL() or KUNIT_ASSERT_NOT_ERR_OR_NULL().

Pick KUNIT_ASSERT_NOT_NULL() as the error handling method that made most
sense to me. It's an unlikely thing to happen, but at least we call
__kunit_abort() instead of dereferencing this NULL pointer.

Fixes: e9502ea6db8a ("lib: packing: add KUnit tests adapted from selftests")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
 lib/packing_test.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Jacob Keller Oct. 4, 2024, 7:20 p.m. UTC | #1
> -----Original Message-----
> From: Vladimir Oltean <vladimir.oltean@nxp.com>
> Sent: Friday, October 4, 2024 4:00 AM
> To: netdev@vger.kernel.org
> Cc: David S. Miller <davem@davemloft.net>; Eric Dumazet
> <edumazet@google.com>; Jakub Kicinski <kuba@kernel.org>; Paolo Abeni
> <pabeni@redhat.com>; Keller, Jacob E <jacob.e.keller@intel.com>; Kitszel,
> Przemyslaw <przemyslaw.kitszel@intel.com>; linux-kernel@vger.kernel.org
> Subject: [PATCH net-next] lib: packing: catch kunit_kzalloc() failure in the pack()
> test
> 
> kunit_kzalloc() may fail. Other call sites verify that this is the case,
> either using a direct comparison with the NULL pointer, or the
> KUNIT_ASSERT_NOT_NULL() or KUNIT_ASSERT_NOT_ERR_OR_NULL().
> 
> Pick KUNIT_ASSERT_NOT_NULL() as the error handling method that made most
> sense to me. It's an unlikely thing to happen, but at least we call
> __kunit_abort() instead of dereferencing this NULL pointer.
> 
> Fixes: e9502ea6db8a ("lib: packing: add KUnit tests adapted from selftests")
> Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
> ---
>  lib/packing_test.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/lib/packing_test.c b/lib/packing_test.c
> index 015ad1180d23..b38ea43c03fd 100644
> --- a/lib/packing_test.c
> +++ b/lib/packing_test.c
> @@ -375,6 +375,7 @@ static void packing_test_pack(struct kunit *test)
>  	int err;
> 
>  	pbuf = kunit_kzalloc(test, params->pbuf_size, GFP_KERNEL);
> +	KUNIT_ASSERT_NOT_NULL(test, pbuf);
> 
>  	err = pack(pbuf, params->uval, params->start_bit, params->end_bit,
>  		   params->pbuf_size, params->quirks);
> --
> 2.43.0

Oh good catch! I guess I had assumed that kunit_kzalloc would itself detect and fail instead of continuing....
diff mbox series

Patch

diff --git a/lib/packing_test.c b/lib/packing_test.c
index 015ad1180d23..b38ea43c03fd 100644
--- a/lib/packing_test.c
+++ b/lib/packing_test.c
@@ -375,6 +375,7 @@  static void packing_test_pack(struct kunit *test)
 	int err;
 
 	pbuf = kunit_kzalloc(test, params->pbuf_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, pbuf);
 
 	err = pack(pbuf, params->uval, params->start_bit, params->end_bit,
 		   params->pbuf_size, params->quirks);