[clang] [clang] fix divide by zero in ComplexExprEvaluator (PR #104666)

via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 16 22:43:57 PDT 2024


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/104666

fix: #55390.

>From b58b9c3ad5fb2b37715ba9f52c905b6961159f0c Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 17 Aug 2024 05:42:39 +0000
Subject: [PATCH] [clang] fix divide by zero in ComplexExprEvaluator

---
 clang/lib/AST/ExprConstant.cpp     | 7 ++++---
 clang/test/SemaCXX/complex-div.cpp | 3 +++
 2 files changed, 7 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/complex-div.cpp

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 7bfc63ffd81e28..aa902280f1861e 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -15567,12 +15567,13 @@ bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
         HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
       }
     } else {
-      if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0)
-        return Error(E, diag::note_expr_divide_by_zero);
-
       ComplexValue LHS = Result;
       APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
         RHS.getComplexIntImag() * RHS.getComplexIntImag();
+      if ((RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) ||
+          Den.isZero())
+        return Error(E, diag::note_expr_divide_by_zero);
+
       Result.getComplexIntReal() =
         (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
          LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
diff --git a/clang/test/SemaCXX/complex-div.cpp b/clang/test/SemaCXX/complex-div.cpp
new file mode 100644
index 00000000000000..a5f9a79b8ebde0
--- /dev/null
+++ b/clang/test/SemaCXX/complex-div.cpp
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -fsyntax-only %s
+
+auto f() { return 43273096 / 65536j; }



More information about the cfe-commits mailing list