diff options
author | Henrik G. Olsson <hnrklssn@gmail.com> | 2025-04-23 16:41:21 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-23 16:41:21 -0700 |
commit | 55160e6a89820f219eaa218fa02da2006213ed2c (patch) | |
tree | 46c8147178557961339b5f4db07b6ae5840086b7 /clang/lib/AST/ExprConstant.cpp | |
parent | 239718055d7260caa3e6631e82d68ac27e01c1f4 (diff) | |
download | llvm-55160e6a89820f219eaa218fa02da2006213ed2c.zip llvm-55160e6a89820f219eaa218fa02da2006213ed2c.tar.gz llvm-55160e6a89820f219eaa218fa02da2006213ed2c.tar.bz2 |
[ConstEval] Fix crash when comparing strings past the end (#137078)
When `ArePotentiallyOverlappingStringLiterals`, added in
https://github.com/llvm/llvm-project/pull/109208, compares string
literals it drops the front of the string with the greatest offset from
its base pointer. The number of characters dropped is equal to the
difference between the two strings' offsets from their base pointers.
This would trigger an assert when the resulting offset is past the end
of the object. Not only are one-past-the-end pointers legal constructs,
the compiler should not crash even when faced with illegal constructs.
rdar://149865910
Diffstat (limited to 'clang/lib/AST/ExprConstant.cpp')
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index f598ef59..7c933f4 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -2232,10 +2232,15 @@ static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info, // within RHS. We don't need to look at the characters of one string that // would appear before the start of the other string if they were merged. CharUnits Offset = RHS.Offset - LHS.Offset; - if (Offset.isNegative()) + if (Offset.isNegative()) { + if (LHSString.Bytes.size() < (size_t)-Offset.getQuantity()) + return false; LHSString.Bytes = LHSString.Bytes.drop_front(-Offset.getQuantity()); - else + } else { + if (RHSString.Bytes.size() < (size_t)Offset.getQuantity()) + return false; RHSString.Bytes = RHSString.Bytes.drop_front(Offset.getQuantity()); + } bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size(); StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes; |