Skip to contentSkip to conversation

fs: erofs: Do NULL check before dereferencing pointer

PATCH In review Andrew Goodbody 3 participants 3 messages Jul 4, 2025 – Jul 11, 2025 EROFS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: erofs: Do NULL check before dereferencing pointer

The assignments to sect and off use the pointer from ctxt.cur_dev but that has not been NULL checked before this is done. So instead move the assignments after the NULL check.

This issue found by Smatch

---
 fs/erofs/fs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/erofs/fs.c +5 -2
index dcdc883e34c..db86928511e 100644
@@ -11,12 +11,15 @@ static struct erofs_ctxt {
 
 int erofs_dev_read(int device_id, void *buf, u64 offset, size_t len)
 {
-	lbaint_t sect = offset >> ctxt.cur_dev->log2blksz;
-	int off = offset & (ctxt.cur_dev->blksz - 1);
+	lbaint_t sect;
+	int off;
 
 	if (!ctxt.cur_dev)
 		return -EIO;
 
+	sect = offset >> ctxt.cur_dev->log2blksz;
+	off = offset & (ctxt.cur_dev->blksz - 1);
+
 	if (fs_devread(ctxt.cur_dev, &ctxt.cur_part_info, sect,
 		       off, len, buf))
 		return 0;

---
base-commit: 7027b445cc0bfb86204ecb1f1fe596f5895048d9
change-id: 20250704-erofs_fix-77cd80979cf6

Best regards,

Signature · 1 line
Andrew Goodbody <[email protected]>
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Andrew Goodbody

The assignments to sect and off use the pointer from ctxt.cur_dev but that has not been NULL checked before this is done. So instead move the assignments after the NULL check.

This issue found by Smatch

Thanks!
Gao Xiang

#2 lore raw
In reply to Andrew Goodbody
Quoted 8 lines · Andrew Goodbody

The assignments to sect and off use the pointer from ctxt.cur_dev but that has not been NULL checked before this is done. So instead move the assignments after the NULL check.

This issue found by Smatch

[...]

Applied to u-boot/master, thanks!

[1/1] fs: erofs: Do NULL check before dereferencing pointer

      commit: ff8a41ce4947c6d7f1826990d5d7b96d30928e3f
Signature · 1 line
Tom