[libc-commits] [PATCH] D85790: [libc][NFC] Extend <ASSERT|EXPECT>_STR* macros to compare with nullptr.

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Aug 11 16:34:35 PDT 2020


sivachandra updated this revision to Diff 284933.
sivachandra added a comment.

Correct diff (hopefully) with a fix to strstr included.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85790/new/

https://reviews.llvm.org/D85790

Files:
  libc/src/string/strstr.cpp
  libc/utils/UnitTest/Test.cpp


Index: libc/utils/UnitTest/Test.cpp
===================================================================
--- libc/utils/UnitTest/Test.cpp
+++ libc/utils/UnitTest/Test.cpp
@@ -242,6 +242,10 @@
 bool Test::testStrEq(RunContext &Ctx, const char *LHS, const char *RHS,
                      const char *LHSStr, const char *RHSStr, const char *File,
                      unsigned long Line) {
+  if (LHS == nullptr)
+    LHS = "<nullptr>";
+  if (RHS == nullptr)
+    RHS = "<nullptr>";
   return internal::test(Ctx, Cond_EQ, llvm::StringRef(LHS),
                         llvm::StringRef(RHS), LHSStr, RHSStr, File, Line);
 }
@@ -249,6 +253,10 @@
 bool Test::testStrNe(RunContext &Ctx, const char *LHS, const char *RHS,
                      const char *LHSStr, const char *RHSStr, const char *File,
                      unsigned long Line) {
+  if (LHS == nullptr)
+    LHS = "<nullptr>";
+  if (RHS == nullptr)
+    RHS = "<nullptr>";
   return internal::test(Ctx, Cond_NE, llvm::StringRef(LHS),
                         llvm::StringRef(RHS), LHSStr, RHSStr, File, Line);
 }
Index: libc/src/string/strstr.cpp
===================================================================
--- libc/src/string/strstr.cpp
+++ libc/src/string/strstr.cpp
@@ -16,6 +16,11 @@
 // TODO: This is a simple brute force implementation. This can be
 // improved upon using well known string matching algorithms.
 char *LLVM_LIBC_ENTRYPOINT(strstr)(const char *haystack, const char *needle) {
+  // The standard requires that haystack is returned if the needle is an empty
+  // string.
+  if (*needle == '\0')
+    return const_cast<char *>(haystack);
+
   for (size_t i = 0; haystack[i]; ++i) {
     size_t j;
     for (j = 0; haystack[i + j] && haystack[i + j] == needle[j]; ++j)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85790.284933.patch
Type: text/x-patch
Size: 1757 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20200811/a9b11011/attachment.bin>


More information about the libc-commits mailing list