Skip to content

Commit dd40632

Browse files
c8efs-barannikov
andauthored
[clang] fix divide by zero in ComplexExprEvaluator (#104666)
fix: #55390. --------- Co-authored-by: Sergei Barannikov <[email protected]>
1 parent e05307f commit dd40632

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ Bug Fixes to C++ Support
274274
Bug Fixes to AST Handling
275275
^^^^^^^^^^^^^^^^^^^^^^^^^
276276

277+
- Fixed a crash that occurred when dividing by zero in complex integer division. (#GH55390).
278+
277279
Miscellaneous Bug Fixes
278280
^^^^^^^^^^^^^^^^^^^^^^^
279281

clang/lib/AST/ExprConstant.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15615,12 +15615,12 @@ bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1561515615
HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
1561615616
}
1561715617
} else {
15618-
if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0)
15619-
return Error(E, diag::note_expr_divide_by_zero);
15620-
1562115618
ComplexValue LHS = Result;
1562215619
APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
1562315620
RHS.getComplexIntImag() * RHS.getComplexIntImag();
15621+
if (Den.isZero())
15622+
return Error(E, diag::note_expr_divide_by_zero);
15623+
1562415624
Result.getComplexIntReal() =
1562515625
(LHS.getComplexIntReal() * RHS.getComplexIntReal() +
1562615626
LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;

clang/test/SemaCXX/constant-expression-cxx11.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,9 @@ constexpr complex_wrap makeComplexWrap(int re, int im) {
12681268
static_assert(makeComplexWrap(1,0) == complex(1), "");
12691269
static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
12701270

1271+
constexpr auto GH55390 = 1 / 65536j; // expected-note {{division by zero}} \
1272+
// expected-error {{constexpr variable 'GH55390' must be initialized by a constant expression}} \
1273+
// expected-warning {{imaginary constants are a GNU extension}}
12711274
}
12721275

12731276
namespace PR11595 {

0 commit comments

Comments
 (0)