Skip to content

[ConstEval] Fix crash when comparing strings past the end #137078

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

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2234,10 +2234,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;
Expand Down
9 changes: 9 additions & 0 deletions clang/test/AST/ByteCode/cxx20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ constexpr auto b3 = name1() == name1(); // ref-error {{must be initialized by a
constexpr auto b4 = name1() == name2();
static_assert(!b4);

constexpr auto bar(const char *p) { return p + __builtin_strlen(p); }
constexpr auto b5 = bar(p1) == p1;
static_assert(!b5);
constexpr auto b6 = bar(p1) == ""; // ref-error {{must be initialized by a constant expression}} \
// ref-note {{comparison of addresses of potentially overlapping literals}}
constexpr auto b7 = bar(p1) + 1 == ""; // both-error {{must be initialized by a constant expression}} \
// ref-note {{comparison against pointer '&"test1"[6]' that points past the end of a complete object has unspecified value}} \
// expected-note {{comparison against pointer '&"test1"[6] + 1' that points past the end of a complete object has unspecified value}}
Copy link
Member Author

@hnrklssn hnrklssn Apr 23, 2025

Choose a reason for hiding this comment

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

@tbaederr Not sure what causes the '&"test1"[6]' vs '&"test1"[6] + 1' discrepancy, but I thought I'd highlight it in case it isn't a known issue

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm I may have been to quick with this, CI reports differently now. Reverting to check.


namespace UninitializedFields {
class A {
public:
Expand Down
2 changes: 2 additions & 0 deletions clang/test/SemaCXX/constant-expression-cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2199,6 +2199,8 @@ namespace BuiltinStrlen {
static_assert(__builtin_strlen("foo") == 3, "");
static_assert(__builtin_strlen("foo\0quux") == 3, "");
static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
static_assert(__builtin_strlen("foo") + 1 + "foo" == "foo", ""); // expected-error {{static assertion expression is not an integral constant expression}}
// expected-note@-1 {{comparison against pointer '&"foo"[4]' that points past the end of a complete object has unspecified value}}

constexpr bool check(const char *p) {
return __builtin_strlen(p) == 3 &&
Expand Down
Loading