Skip to contentSkip to conversation

ofnode: NULL check bootscr_flash_size before dereference

PATCH Superseded Andrew Goodbody 2 participants 3 messages Jul 3, 2025 – Jul 7, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ofnode: NULL check bootscr_flash_size before dereference

Move the NULL check of bootscr_flash_size to before the first time it is dereferenced to avoid any possible segment violations.

This issue found by Smatch.

---
 drivers/core/ofnode.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/core/ofnode.c +6 -6
index 26e014d5c53..1c285a867b3 100644
@@ -1960,6 +1960,12 @@ int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
 	int ret;
 	ofnode uboot;
 
+	if (!bootscr_flash_size) {
+		dm_warn("bootscr-flash-size is zero. Ignoring properties!\n");
+		*bootscr_flash_offset = 0;
+		return -EINVAL;
+	}
+
 	*bootscr_flash_offset = 0;
 	*bootscr_flash_size = 0;
 
@@ -1979,12 +1985,6 @@ int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
 	if (ret)
 		return -EINVAL;
 
-	if (!bootscr_flash_size) {
-		dm_warn("bootscr-flash-size is zero. Ignoring properties!\n");
-		*bootscr_flash_offset = 0;
-		return -EINVAL;
-	}
-
 	return 0;
 }
 

---
base-commit: 7027b445cc0bfb86204ecb1f1fe596f5895048d9
change-id: 20250703-ofnode_fix-284e601e4329

Best regards,

Signature · 1 line
Andrew Goodbody <[email protected]>
#1 lore raw
Quoted 16 lines · Andrew Goodbody

Move the NULL check of bootscr_flash_size to before the first time it is dereferenced to avoid any possible segment violations.

This issue found by Smatch.

---
 drivers/core/ofnode.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/core/ofnode.c +0 -0
index 26e014d5c53..1c285a867b3 100644
@@ -1960,6 +1960,12 @@ int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
 	int ret;

drivers/core/ofnode.c int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset, · line 1970

Quoted 10 lines
drivers/core/ofnode.c +6 -0
 	ofnode uboot;
 
+	if (!bootscr_flash_size) {
+		dm_warn("bootscr-flash-size is zero. Ignoring properties!\n");
+		*bootscr_flash_offset = 0;
+		return -EINVAL;
+	}
+
 	*bootscr_flash_offset = 0;
 	*bootscr_flash_size = 0;

Is the compiler likely to optimize this to:
*bootscr_flash_offset = 0;
if (!bootscr_flash_size) {
...dm_warn(...)
return -EINVAL;
}
*bootscr_flash_size = 0;

Otherwise we should re-organize it like that. And yes, the two patches today for "fix platform ... because it grew too much" reminded me to be picky like this.

Signature · 1 line
Tom
#2 lore raw
Quoted 35 lines · Tom Rini
Quoted 16 lines · Andrew Goodbody

Move the NULL check of bootscr_flash_size to before the first time it is dereferenced to avoid any possible segment violations.

This issue found by Smatch.

---
  drivers/core/ofnode.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
drivers/core/ofnode.c +0 -0
index 26e014d5c53..1c285a867b3 100644
@@ -1960,6 +1960,12 @@ int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
  	int ret;

drivers/core/ofnode.c int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset, · line 1970

Quoted 10 lines
drivers/core/ofnode.c +6 -0
  	ofnode uboot;
 
+	if (!bootscr_flash_size) {
+		dm_warn("bootscr-flash-size is zero. Ignoring properties!\n");
+		*bootscr_flash_offset = 0;
+		return -EINVAL;
+	}
+
  	*bootscr_flash_offset = 0;
  	*bootscr_flash_size = 0;

Is the compiler likely to optimize this to:
*bootscr_flash_offset = 0;

if (!bootscr_flash_size) {
   ...dm_warn(...)
   return -EINVAL;
}
Quoted 6 lines

*bootscr_flash_size = 0;

Otherwise we should re-organize it like that. And yes, the two patches today for "fix platform ... because it grew too much" reminded me to be picky like this.

Thanks, yes.

Andrew