[PATCH] ext4: Use IS_ERR_OR_NULL() helper function

Xichao Zhao posted 1 patch 2 months, 1 week ago
fs/ext4/namei.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] ext4: Use IS_ERR_OR_NULL() helper function
Posted by Xichao Zhao 2 months, 1 week ago
Use the IS_ERR_OR_NULL() helper instead of open-coding a NULL and 
an error pointer checks to simplify the code and improve readability.

Signed-off-by: Xichao Zhao <zhao.xichao@vivo.com>
---
 fs/ext4/namei.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index d83f91b62317..7dff0dc70d0c 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -723,7 +723,7 @@ struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
 		struct stats stats;
 		printk("%s%3u:%03u hash %8x/%8x ",levels?"":"   ", i, block, hash, range);
 		bh = ext4_bread(NULL,dir, block, 0);
-		if (!bh || IS_ERR(bh))
+		if (IS_ERR_OR_NULL(bh))
 			continue;
 		stats = levels?
 		   dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
-- 
2.34.1
Re: [PATCH] ext4: Use IS_ERR_OR_NULL() helper function
Posted by Al Viro 2 months, 1 week ago
On Wed, Jul 30, 2025 at 02:38:23PM +0800, Xichao Zhao wrote:
> Use the IS_ERR_OR_NULL() helper instead of open-coding a NULL and 
> an error pointer checks to simplify the code and improve readability.

IS_ERR_OR_NULL() is almost always a bad idea.  ext4_bread() *is* one
of the cases where NULL is a legitimate return values that is not an
error - it indicates a hole in a sparse file (BTW, not sure if it
can legitimately occur in a directory); the thing is, among the
functions that can return both NULL and ERR_PTR() the meaning assigned
to NULL varies a lot; so much that "ERR_PTR() or NULL" has no good
semantics.

Most of the uses are either sloppy code from somebody who couldn't be
arsed to find out whether it's pointer-or-NULL or pointer-or-ERR_PTR()
or misguided "defensive" crap.  Any caller that is *not* of that sort
is drowning in a pile of garbage.  Let's not breed that...
Re: [PATCH] ext4: Use IS_ERR_OR_NULL() helper function
Posted by Xichao Zhao 2 months, 1 week ago
Thank you for your response. As a beginner who just started submitting patches to the community, 
I mistakenly thought these could simplify the code. I have learned a lot from your reply, thank you very much.