Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][Interp] Fix float->int casts overflowing #72658

Merged
merged 1 commit into from
Dec 11, 2023

Conversation

tbaederr
Copy link
Contributor

If S.noteUndefinedBehavior() returns true, we will continue evaluation and need a value on the stack.

If S.noteUndefinedBehavior() returns true, we will continue evaluation
and need a value on the stack.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 17, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 17, 2023

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

If S.noteUndefinedBehavior() returns true, we will continue evaluation and need a value on the stack.


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

2 Files Affected:

  • (modified) clang/lib/AST/Interp/Interp.h (+5-1)
  • (modified) clang/test/AST/Interp/floats.cpp (+4)
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 026a95d65488da9..ef09b957057cdcb 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1619,7 +1619,11 @@ bool CastFloatingIntegral(InterpState &S, CodePtr OpPC) {
       QualType Type = E->getType();
 
       S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
-      return S.noteUndefinedBehavior();
+      if (S.noteUndefinedBehavior()) {
+        S.Stk.push<T>(T(Result));
+        return true;
+      }
+      return false;
     }
 
     S.Stk.push<T>(T(Result));
diff --git a/clang/test/AST/Interp/floats.cpp b/clang/test/AST/Interp/floats.cpp
index e17167f5bf6dbbf..45c31c759e47fc6 100644
--- a/clang/test/AST/Interp/floats.cpp
+++ b/clang/test/AST/Interp/floats.cpp
@@ -39,6 +39,10 @@ constexpr float m = 5.0f / 0.0f; // ref-error {{must be initialized by a constan
 static_assert(~2.0f == 3, ""); // ref-error {{invalid argument type 'float' to unary expression}} \
                                // expected-error {{invalid argument type 'float' to unary expression}}
 
+
+typedef int tdb[(long long)4e20]; //expected-error {{variable length}} \
+                                  //ref-error {{variable length}}
+
 /// Initialized by a double.
 constexpr float df = 0.0;
 /// The other way around.

@tbaederr
Copy link
Contributor Author

Ping

1 similar comment
@tbaederr
Copy link
Contributor Author

tbaederr commented Dec 8, 2023

Ping

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@tbaederr tbaederr merged commit 607f19c into llvm:main Dec 11, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants