Skip to contentSkip to conversation

dm: Add check for NULL dev_part_str

PATCH New Sean Anderson 2 participants 4 messages May 15, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dm: Add check for NULL dev_part_str

Some callers (e.g. cmd/fs.c) of fs_set_blk_dev may use a NULL dev_part_str. While blk_get_device_part_str handles this fine, part_get_info_by_dev_and_name does not. This fixes commands crashing when implicitly using bootdevice.

Fixes 7194527b6a ("cmd: fs: Use part_get_info_by_dev_and_name_or_num to parse partitions")

---

 disk/part.c    | 6 +++++-
 test/dm/part.c | 2 ++
 2 files changed, 7 insertions(+), 1 deletion(-)
disk/part.c +5 -1
index 5e7e59cf25..086da84b7f 100644
@@ -714,7 +714,11 @@ static int part_get_info_by_dev_and_name(const char *dev_iface,
 	int ret;
 
 	/* Separate device and partition name specification */
-	part_str = strchr(dev_part_str, '#');
+	if (dev_part_str)
+		part_str = strchr(dev_part_str, '#');
+	else
+		part_str = NULL;
+
 	if (part_str) {
 		dup_str = strdup(dev_part_str);
 		dup_str[part_str - dev_part_str] = 0;
test/dm/part.c +2 -0
index 051e9010b6..aff7cfe55f 100644
@@ -46,8 +46,10 @@ static int dm_test_part(struct unit_test_state *uts)
 
 	test(-ENODEV, "", true);
 	env_set("bootdevice", "0");
+	test(0, NULL, true);
 	test(0, "", true);
 	env_set("bootdevice", "1");
+	test(1, NULL, false);
 	test(1, "", false);
 	test(1, "-", false);
 	env_set("bootdevice", "");
Signature · 1 line
2.31.0
#1 lore raw

PATCH 1/1 dm: Add check for NULL dev_part_str

Quoted 1 line

Re: [PATCH] dm: Add check for NULL dev_part_str

This should probably be "part: ..."

Quoted 48 lines · Sean Anderson

Some callers (e.g. cmd/fs.c) of fs_set_blk_dev may use a NULL dev_part_str. While blk_get_device_part_str handles this fine, part_get_info_by_dev_and_name does not. This fixes commands crashing when implicitly using bootdevice.

Fixes 7194527b6a ("cmd: fs: Use part_get_info_by_dev_and_name_or_num to parse partitions")

---

  disk/part.c    | 6 +++++-
  test/dm/part.c | 2 ++
  2 files changed, 7 insertions(+), 1 deletion(-)
disk/part.c +5 -1
index 5e7e59cf25..086da84b7f 100644
@@ -714,7 +714,11 @@ static int part_get_info_by_dev_and_name(const char *dev_iface,
  	int ret;
 
  	/* Separate device and partition name specification */
-	part_str = strchr(dev_part_str, '#');
+	if (dev_part_str)
+		part_str = strchr(dev_part_str, '#');
+	else
+		part_str = NULL;
+
  	if (part_str) {
  		dup_str = strdup(dev_part_str);
  		dup_str[part_str - dev_part_str] = 0;
test/dm/part.c +2 -0
index 051e9010b6..aff7cfe55f 100644
@@ -46,8 +46,10 @@ static int dm_test_part(struct unit_test_state *uts)
 
  	test(-ENODEV, "", true);
  	env_set("bootdevice", "0");
+	test(0, NULL, true);
  	test(0, "", true);
  	env_set("bootdevice", "1");
+	test(1, NULL, false);
  	test(1, "", false);
  	test(1, "-", false);
  	env_set("bootdevice", "");
#2 lore raw

PATCH 1/1 dm: Add check for NULL dev_part_str

In reply to Sean Anderson
Quoted 8 lines · Sean Anderson

Some callers (e.g. cmd/fs.c) of fs_set_blk_dev may use a NULL dev_part_str. While blk_get_device_part_str handles this fine, part_get_info_by_dev_and_name does not. This fixes commands crashing when implicitly using bootdevice.

Fixes 7194527b6a ("cmd: fs: Use part_get_info_by_dev_and_name_or_num to parse partitions")

With this patch the reported error cannot be reproduced:

=> host bind 0 ../sandbox.img
=> setenv bootdevice 0:1
=> ls host

       148   boot.scr

...

But the unit test fails after issuing:

setenv bootdevice 0:1

=> ut dm dm_test_part
Test: dm_test_part: part.c
test/dm/part.c:47, dm_test_part(): -19 == part_get_info_by_dev_and_name_or_num("mmc", "", &mmc_dev_desc, &part_info, 1): Expected 0xffffffed (-19), got 0xffffffa3 (-93)
Test: dm_test_part: part.c (flat tree) test/dm/part.c:47, dm_test_part(): -19 == part_get_info_by_dev_and_name_or_num("mmc", "", &mmc_dev_desc, &part_info, 1): Expected 0xffffffed (-19), got 0xffffffa3 (-93)
Failures: 2
=>

We get 'No such device' instead of 'Protocol not supported'.

As the environment variable bootdevice influences the test result it should be unset when setting up the test and set to its original value when tearing down the test.

Best regards

Heinrich

#3 lore raw

PATCH 1/1 dm: Add check for NULL dev_part_str

Quoted 34 lines · Heinrich Schuchardt
Quoted 8 lines · Sean Anderson

Some callers (e.g. cmd/fs.c) of fs_set_blk_dev may use a NULL dev_part_str. While blk_get_device_part_str handles this fine, part_get_info_by_dev_and_name does not. This fixes commands crashing when implicitly using bootdevice.

Fixes 7194527b6a ("cmd: fs: Use part_get_info_by_dev_and_name_or_num to parse partitions")

With this patch the reported error cannot be reproduced:

=> host bind 0 ../sandbox.img
=> setenv bootdevice 0:1
=> ls host

       148   boot.scr

...

But the unit test fails after issuing:

setenv bootdevice 0:1

=> ut dm dm_test_part
Test: dm_test_part: part.c
test/dm/part.c:47, dm_test_part(): -19 == part_get_info_by_dev_and_name_or_num("mmc", "", &mmc_dev_desc, &part_info, 1): Expected 0xffffffed (-19), got 0xffffffa3 (-93)
Test: dm_test_part: part.c (flat tree) test/dm/part.c:47, dm_test_part(): -19 == part_get_info_by_dev_and_name_or_num("mmc", "", &mmc_dev_desc, &part_info, 1): Expected 0xffffffed (-19), got 0xffffffa3 (-93)
Failures: 2
=>

Quoted 5 lines

We get 'No such device' instead of 'Protocol not supported'.

As the environment variable bootdevice influences the test result it should be unset when setting up the test and set to its original value when tearing down the test.

ugh...

This really should be handled by the unit test framework (e.g. do the equivalent of "env save; env default -a; ut; env load" but to some temporary location). Will send a v2 with a fix...

--Sean

Quoted 4 lines

Best regards

Heinrich