From 11d627f2279b836d0c4e624d4ca0f6c665971fd2 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Fri, 7 Oct 2022 15:20:23 +0300 Subject: [PATCH] zero_to_err_ptr: silence some false positives Doing "return PTR_ERR(p);" when "p" is NULL is a perfectly valid thing to do. However, although it can be valid, most of the time, it is a bug. Hence this check. However, when people use the PTR_ERR(p) as part of a condition: if (PTR_ERR(p) == -ENOMEM) { then most of those uses are valid so don't print a warning about those. Signed-off-by: Dan Carpenter --- check_zero_to_err_ptr.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/check_zero_to_err_ptr.c b/check_zero_to_err_ptr.c index 53dd8291..88ca0285 100644 --- a/check_zero_to_err_ptr.c +++ b/check_zero_to_err_ptr.c @@ -46,6 +46,25 @@ static int is_comparison_call(struct expression *expr) return 1; } +static bool is_switch_condition(struct expression *expr) +{ + struct statement *stmt; + + stmt = expr_get_parent_stmt(expr); + if (stmt && stmt->type == STMT_SWITCH) + return true; + return false; +} + +static bool is_condition_expr(struct expression *expr) +{ + if (is_comparison_call(expr) || + is_select_assign(expr) || + is_switch_condition(expr)) + return true; + return false; +} + static int next_line_is_if(struct expression *expr) { struct expression *next; @@ -147,9 +166,7 @@ static void match_err_ptr(const char *fn, struct expression *expr, void *data) if (!sm) return; - if (is_comparison_call(expr)) - return; - if (is_select_assign(expr)) + if (is_condition_expr(expr)) return; if (next_line_checks_IS_ERR(expr, arg_expr)) -- 2.11.4.GIT