[clang] [clang] fix divide by zero in ComplexExprEvaluator (PR #104666)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 18 05:11:14 PDT 2024
https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/104666
>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 1/4] [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; }
>From 8a0be6659ac649e45b7a04458c3266f9ba5d6315 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 17 Aug 2024 09:37:18 +0000
Subject: [PATCH 2/4] address CR comment
---
clang/docs/ReleaseNotes.rst | 1 +
clang/test/SemaCXX/complex-div.cpp | 3 ---
clang/test/SemaCXX/constant-expression-cxx11.cpp | 3 +++
3 files changed, 4 insertions(+), 3 deletions(-)
delete mode 100644 clang/test/SemaCXX/complex-div.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ffdd063ec99037..71a7abc8db166a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -264,6 +264,7 @@ Bug Fixes to C++ Support
- Properly reject defaulted copy/move assignment operators that have a non-reference explicit object parameter.
- Clang now properly handles the order of attributes in `extern` blocks. (#GH101990).
- Fixed an assertion failure by preventing null explicit object arguments from being deduced. (#GH102025).
+- Fixed a crash that occurred when dividing by zero in complex integer division. (#GH55390).
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/SemaCXX/complex-div.cpp b/clang/test/SemaCXX/complex-div.cpp
deleted file mode 100644
index a5f9a79b8ebde0..00000000000000
--- a/clang/test/SemaCXX/complex-div.cpp
+++ /dev/null
@@ -1,3 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only %s
-
-auto f() { return 43273096 / 65536j; }
diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index d888887bd8c6f3..44ef540f41fa8c 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -1268,6 +1268,9 @@ constexpr complex_wrap makeComplexWrap(int re, int im) {
static_assert(makeComplexWrap(1,0) == complex(1), "");
static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
+constexpr auto GH55390 = 1 / 65536j; // expected-note {{division by zero}} \
+ // expected-error {{constexpr variable 'GH55390' must be initialized by a constant expression}} \
+ // expected-warning {{imaginary constants are a GNU extension}}
}
namespace PR11595 {
>From 73a9f7bb42d10ec2df2f2f1f2d5fc9ae939d266b Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 17 Aug 2024 09:43:40 +0000
Subject: [PATCH 3/4] relocate note
---
clang/docs/ReleaseNotes.rst | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 71a7abc8db166a..c28fbd881eda86 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -264,11 +264,12 @@ Bug Fixes to C++ Support
- Properly reject defaulted copy/move assignment operators that have a non-reference explicit object parameter.
- Clang now properly handles the order of attributes in `extern` blocks. (#GH101990).
- Fixed an assertion failure by preventing null explicit object arguments from being deduced. (#GH102025).
-- Fixed a crash that occurred when dividing by zero in complex integer division. (#GH55390).
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
+- Fixed a crash that occurred when dividing by zero in complex integer division. (#GH55390).
+
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
>From 75c9fe1fb3b88d6760aca2d4a2c0ea53ed4a18c3 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 18 Aug 2024 20:11:05 +0800
Subject: [PATCH 4/4] Update clang/lib/AST/ExprConstant.cpp
Co-authored-by: Sergei Barannikov <barannikov88 at gmail.com>
---
clang/lib/AST/ExprConstant.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index aa902280f1861e..ec4f254632748a 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -15570,8 +15570,7 @@ bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
ComplexValue LHS = Result;
APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
RHS.getComplexIntImag() * RHS.getComplexIntImag();
- if ((RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) ||
- Den.isZero())
+ if (Den.isZero())
return Error(E, diag::note_expr_divide_by_zero);
Result.getComplexIntReal() =
More information about the cfe-commits
mailing list