[clang] [analyzer] Avoid a crash in a debug printout function (PR #79446)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 25 07:35:12 PST 2024


https://github.com/NagyDonat updated https://github.com/llvm/llvm-project/pull/79446

>From 2a1bb37fef538ecfde67f10c50df07100cc3b69a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Thu, 25 Jan 2024 14:13:03 +0100
Subject: [PATCH 1/2] [analyzer] Avoid a crash in a debug printout function

Previously the function `RangeConstraintManager::printValue()` crashed
when it encountered an empty rangeset (because `RangeSet::getBitwidth()`
and `RangeSet::isUnsigned()` assert that the rangeset is not empty).
This commit adds a special case that avoids this behavior.

As `printValue()` is only used by the checker debug.ExprInspection (and
during manual debugging), the impacts of this commit are very limited.
---
 clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp b/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
index 25d066c4652f2b..cc1cad1e002cdf 100644
--- a/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
@@ -3270,8 +3270,12 @@ void RangeConstraintManager::printJson(raw_ostream &Out, ProgramStateRef State,
 void RangeConstraintManager::printValue(raw_ostream &Out, ProgramStateRef State,
                                         SymbolRef Sym) {
   const RangeSet RS = getRange(State, Sym);
-  Out << RS.getBitWidth() << (RS.isUnsigned() ? "u:" : "s:");
-  RS.dump(Out);
+  if (RS.isEmpty()) {
+    Out << "<empty rangeset>";
+  } else {
+    Out << RS.getBitWidth() << (RS.isUnsigned() ? "u:" : "s:");
+    RS.dump(Out);
+  }
 }
 
 static std::string toString(const SymbolRef &Sym) {

>From 92a7e99f8cf0f2a58a63c40d12f4bcf98d1626d2 Mon Sep 17 00:00:00 2001
From: NagyDonat <donat.nagy at ericsson.com>
Date: Thu, 25 Jan 2024 16:35:05 +0100
Subject: [PATCH 2/2] Use early return

Co-authored-by: Balazs Benics <benicsbalazs at gmail.com>
---
 clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp b/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
index cc1cad1e002cdf..2d8498e3601556 100644
--- a/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
@@ -3272,10 +3272,10 @@ void RangeConstraintManager::printValue(raw_ostream &Out, ProgramStateRef State,
   const RangeSet RS = getRange(State, Sym);
   if (RS.isEmpty()) {
     Out << "<empty rangeset>";
-  } else {
-    Out << RS.getBitWidth() << (RS.isUnsigned() ? "u:" : "s:");
-    RS.dump(Out);
+    return;
   }
+  Out << RS.getBitWidth() << (RS.isUnsigned() ? "u:" : "s:");
+  RS.dump(Out);
 }
 
 static std::string toString(const SymbolRef &Sym) {



More information about the cfe-commits mailing list