Skip to content

Commit 154064e

Browse files
committed
Replace compareMemory() with memcmp(); NFC
WG14 N3322 (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf) was adopted for C2y, which makes zero-length operations on null pointers well-defined. The paper was also adopted as the WG14 equivalent of a WG21 defect report, so it applies in older language modes as well. We are not aware of any C standard library which takes advantage of this former UB, so this workaround is no longer needed.
1 parent ced23aa commit 154064e

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

llvm/include/llvm/ADT/StringRef.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ namespace llvm {
6666
/// The length of the string.
6767
size_t Length = 0;
6868

69-
// Workaround memcmp issue with null pointers (undefined behavior)
70-
// by providing a specialized version
71-
static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
72-
if (Length == 0) { return 0; }
73-
return ::memcmp(Lhs,Rhs,Length);
74-
}
75-
7669
public:
7770
/// @name Constructors
7871
/// @{
@@ -182,8 +175,7 @@ namespace llvm {
182175
/// the \p RHS.
183176
[[nodiscard]] int compare(StringRef RHS) const {
184177
// Check the prefix for a mismatch.
185-
if (int Res =
186-
compareMemory(data(), RHS.data(), std::min(size(), RHS.size())))
178+
if (int Res = ::memcmp(data(), RHS.data(), std::min(size(), RHS.size())))
187179
return Res < 0 ? -1 : 1;
188180

189181
// Otherwise the prefixes match, so we only need to check the lengths.
@@ -264,7 +256,7 @@ namespace llvm {
264256
/// Check if this string starts with the given \p Prefix.
265257
[[nodiscard]] bool starts_with(StringRef Prefix) const {
266258
return size() >= Prefix.size() &&
267-
compareMemory(data(), Prefix.data(), Prefix.size()) == 0;
259+
::memcmp(data(), Prefix.data(), Prefix.size()) == 0;
268260
}
269261
[[nodiscard]] bool starts_with(char Prefix) const {
270262
return !empty() && front() == Prefix;
@@ -276,8 +268,7 @@ namespace llvm {
276268
/// Check if this string ends with the given \p Suffix.
277269
[[nodiscard]] bool ends_with(StringRef Suffix) const {
278270
return size() >= Suffix.size() &&
279-
compareMemory(end() - Suffix.size(), Suffix.data(),
280-
Suffix.size()) == 0;
271+
::memcmp(end() - Suffix.size(), Suffix.data(), Suffix.size()) == 0;
281272
}
282273
[[nodiscard]] bool ends_with(char Suffix) const {
283274
return !empty() && back() == Suffix;

llvm/lib/Support/StringRef.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int StringRef::compare_numeric(StringRef RHS) const {
7676
break;
7777
}
7878
// The two number sequences have the same length (J-I), just memcmp them.
79-
if (int Res = compareMemory(data() + I, RHS.data() + I, J - I))
79+
if (int Res = ::memcmp(data() + I, RHS.data() + I, J - I))
8080
return Res < 0 ? -1 : 1;
8181
// Identical number sequences, continue search after the numbers.
8282
I = J - 1;

0 commit comments

Comments
 (0)