From 01303f6d1bba5f8640aab022469f9e9a9d66f877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <1.int32@gmail.com> Date: Wed, 21 Dec 2022 09:05:43 +0100 Subject: [PATCH] [clang-tidy] Fix crash in bugprone-suspicious-realloc-usage. The problem occurs if a statement is found by the checker that has a null child. Fixes issue #59518. Reviewed By: hokein Differential Revision: https://reviews.llvm.org/D140194 --- .../clang-tidy/bugprone/SuspiciousReallocUsageCheck.cpp | 2 +- .../checkers/bugprone/suspicious-realloc-usage.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousReallocUsageCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousReallocUsageCheck.cpp index bb975bc893d0d..9b78c219fc5a9 100644 --- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousReallocUsageCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousReallocUsageCheck.cpp @@ -95,7 +95,7 @@ class FindAssignToVarBefore } bool VisitStmt(const Stmt *S) { for (const Stmt *Child : S->children()) - if (Visit(Child)) + if (Child && Visit(Child)) return true; return false; } diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-realloc-usage.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-realloc-usage.cpp index 6e3c7f4174845..3647d1232b4ff 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-realloc-usage.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-realloc-usage.cpp @@ -100,3 +100,10 @@ void warn_if_copy_exists_after(void *p) { // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage] void *q = p; } + +void test_null_child(void *p) { + for (;;) + break; + p = realloc(p, 111); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage] +}