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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (c8ef)

<details>
<summary>Changes</summary>

fix: #<!-- -->55390.

---
Full diff: https://github.com/llvm/llvm-project/pull/104666.diff


2 Files Affected:

- (modified) clang/lib/AST/ExprConstant.cpp (+4-3) 
- (added) clang/test/SemaCXX/complex-div.cpp (+3) 


``````````diff
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; }

``````````

</details>


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


More information about the cfe-commits mailing list