[llvm] 7068497 - [AsmParser] Fix crash when hex literal exceeds 16-bit float range (#172669)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 23 04:37:21 PST 2025
Author: Miloš Poletanović
Date: 2025-12-23T12:37:16Z
New Revision: 7068497a1dd2a367f051ab1d615430133704ae99
URL: https://github.com/llvm/llvm-project/commit/7068497a1dd2a367f051ab1d615430133704ae99
DIFF: https://github.com/llvm/llvm-project/commit/7068497a1dd2a367f051ab1d615430133704ae99.diff
LOG: [AsmParser] Fix crash when hex literal exceeds 16-bit float range (#172669)
Fixes #172650.
Added:
llvm/test/Assembler/hex-float-overflow.ll
Modified:
llvm/lib/AsmParser/LLLexer.cpp
Removed:
################################################################################
diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
index ef1515ca10494..9ac671062966c 100644
--- a/llvm/lib/AsmParser/LLLexer.cpp
+++ b/llvm/lib/AsmParser/LLLexer.cpp
@@ -1129,16 +1129,26 @@ lltok::Kind LLLexer::Lex0x() {
HexToIntPair(TokStart+3, CurPtr, Pair);
APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
return lltok::APFloat;
- case 'H':
- APFloatVal = APFloat(APFloat::IEEEhalf(),
- APInt(16,HexIntToVal(TokStart+3, CurPtr)));
+ case 'H': {
+ uint64_t Val = HexIntToVal(TokStart + 3, CurPtr);
+ if (!llvm::isUInt<16>(Val)) {
+ LexError("hexadecimal constant too large for half (16-bit)");
+ return lltok::Error;
+ }
+ APFloatVal = APFloat(APFloat::IEEEhalf(), APInt(16, Val));
return lltok::APFloat;
- case 'R':
+ }
+ case 'R': {
// Brain floating point
- APFloatVal = APFloat(APFloat::BFloat(),
- APInt(16, HexIntToVal(TokStart + 3, CurPtr)));
+ uint64_t Val = HexIntToVal(TokStart + 3, CurPtr);
+ if (!llvm::isUInt<16>(Val)) {
+ LexError("hexadecimal constant too large for bfloat (16-bit)");
+ return lltok::Error;
+ }
+ APFloatVal = APFloat(APFloat::BFloat(), APInt(16, Val));
return lltok::APFloat;
}
+ }
}
/// Lex tokens for a label or a numeric constant, possibly starting with -.
diff --git a/llvm/test/Assembler/hex-float-overflow.ll b/llvm/test/Assembler/hex-float-overflow.ll
new file mode 100644
index 0000000000000..af8aad7b9e043
--- /dev/null
+++ b/llvm/test/Assembler/hex-float-overflow.ll
@@ -0,0 +1,22 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: split-file %s %t
+; RUN: not opt -S -disable-output < %t/half.ll 2>&1 | FileCheck %s --check-prefix=HALF
+; RUN: not opt -S -disable-output < %t/bfloat.ll 2>&1 | FileCheck %s --check-prefix=BFLOAT
+
+; Test that providing a hex literal larger than 16 bits for half-precision
+; triggers a proper error message instead of a compiler crash.
+
+;--- half.ll
+define half @test_half_overflow() {
+; HALF: error: hexadecimal constant too large for half (16-bit)
+ %1 = fadd half 0xH5F2F00, 0xH0000
+ ret half %1
+}
+
+;--- bfloat.ll
+define bfloat @test_bfloat_overflow() {
+; BFLOAT: error: hexadecimal constant too large for bfloat (16-bit)
+ %1 = fadd bfloat 0xR5F2F00, 0xR0000
+ ret bfloat %1
+}
+
More information about the llvm-commits
mailing list