diff options
author | Timm Baeder <tbaeder@redhat.com> | 2025-07-22 14:23:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-22 14:23:01 +0200 |
commit | a5d9ba66258e452e4658281bbbd07a2214d4dc51 (patch) | |
tree | bc561431e984cc83b607dbb92ed0ae894f5c605a /clang/lib/AST/ByteCode | |
parent | 4ae9fdca8af095afd91705f8dd143e93b304b6fb (diff) | |
download | llvm-a5d9ba66258e452e4658281bbbd07a2214d4dc51.zip llvm-a5d9ba66258e452e4658281bbbd07a2214d4dc51.tar.gz llvm-a5d9ba66258e452e4658281bbbd07a2214d4dc51.tar.bz2 |
[clang][bytecode] Error on bitcasts to bool that aren't 0 or 1 (#149996)
This is also what the current interpreter does.
Diffstat (limited to 'clang/lib/AST/ByteCode')
-rw-r--r-- | clang/lib/AST/ByteCode/Interp.h | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index 7f29200..b42c766 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -3557,12 +3557,22 @@ inline bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte, Floating Result = S.allocFloat(*Sem); Floating::bitcastFromMemory(Buff.data(), *Sem, &Result); S.Stk.push<Floating>(Result); - - // S.Stk.push<Floating>(T::bitcastFromMemory(Buff.data(), *Sem)); } else if constexpr (needsAlloc<T>()) { T Result = S.allocAP<T>(ResultBitWidth); T::bitcastFromMemory(Buff.data(), ResultBitWidth, &Result); S.Stk.push<T>(Result); + } else if constexpr (std::is_same_v<T, Boolean>) { + // Only allow to cast single-byte integers to bool if they are either 0 + // or 1. + assert(FullBitWidth.getQuantity() == 8); + auto Val = static_cast<unsigned int>(Buff[0]); + if (Val > 1) { + S.FFDiag(S.Current->getSource(OpPC), + diag::note_constexpr_bit_cast_unrepresentable_value) + << S.getASTContext().BoolTy << Val; + return false; + } + S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth)); } else { assert(!Sem); S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth)); |