diff mbox series

test: eth: Don't crash if env_get returns NULL

Message ID 20231028225727.1046361-1-seanga2@gmail.com
State Accepted
Commit 2a1812de208168f2f4edeb48e8da6b12315ff77f
Delegated to: Tom Rini
Headers show
Series test: eth: Don't crash if env_get returns NULL | expand

Commit Message

Sean Anderson Oct. 28, 2023, 10:57 p.m. UTC
env_get can return NULL if it fails to find the variable. Check its result
before using it.

Fixes: 6d9764c2a87 ("dm: test: Add a new test case against dm eth codes for NULL pointer access")
Fixes: df33fd28897 ("test: eth: Add test for ethernet addresses")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 test/dm/eth.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Comments

Tom Rini Nov. 17, 2023, 1:41 p.m. UTC | #1
On Sat, Oct 28, 2023 at 06:57:27PM -0400, Sean Anderson wrote:

> env_get can return NULL if it fails to find the variable. Check its result
> before using it.
> 
> Fixes: 6d9764c2a87 ("dm: test: Add a new test case against dm eth codes for NULL pointer access")
> Fixes: df33fd28897 ("test: eth: Add test for ethernet addresses")
> Signed-off-by: Sean Anderson <seanga2@gmail.com>

Applied to u-boot/next, thanks!
diff mbox series

Patch

diff --git a/test/dm/eth.c b/test/dm/eth.c
index d05d2a9abe1..bb3dcc6b954 100644
--- a/test/dm/eth.c
+++ b/test/dm/eth.c
@@ -263,12 +263,16 @@  static int dm_test_eth_act(struct unit_test_state *uts)
 
 	/* Prepare the test scenario */
 	for (i = 0; i < DM_TEST_ETH_NUM; i++) {
+		char *addr;
+
 		ut_assertok(uclass_find_device_by_name(UCLASS_ETH,
 						       ethname[i], &dev[i]));
 		ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL));
 
 		/* Invalidate MAC address */
-		strncpy(ethaddr[i], env_get(addrname[i]), 17);
+		addr = env_get(addrname[i]);
+		ut_assertnonnull(addr);
+		strncpy(ethaddr[i], addr, 17);
 		/* Must disable access protection for ethaddr before clearing */
 		env_set(".flags", addrname[i]);
 		env_set(addrname[i], NULL);
@@ -312,12 +316,16 @@  static int dm_test_ethaddr(struct unit_test_state *uts)
 
 	for (i = 0; i < ARRAY_SIZE(addr); i++) {
 		char addrname[10];
+		char *env_addr;
 
 		if (i)
 			snprintf(addrname, sizeof(addrname), "eth%daddr", i + 1);
 		else
 			strcpy(addrname, "ethaddr");
-		ut_asserteq_str(addr[i], env_get(addrname));
+
+		env_addr = env_get(addrname);
+		ut_assertnonnull(env_addr);
+		ut_asserteq_str(addr[i], env_addr);
 	}
 
 	return 0;