Bug 1427023 - Remove nsCRT::strncmp. r=erahm
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Tue, 09 Jan 2018 23:49:37 +0900
changeset 398656 077e64a34f6b99495167267619e53e60c403e4b1
parent 398655 b69e9e95470a48e8d8ba0235c79e62b55454647f
child 398657 ac990767e3e83207f325c9a1ec98e28dfdf9f9a1
push id33228
push useraciure@mozilla.com
push dateThu, 11 Jan 2018 09:55:35 +0000
treeherdermozilla-central@e61c4485494e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1427023
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1427023 - Remove nsCRT::strncmp. r=erahm
dom/base/nsPlainTextSerializer.cpp
image/imgLoader.cpp
layout/style/nsStyleUtil.h
netwerk/base/nsStandardURL.cpp
netwerk/base/nsURLHelper.cpp
netwerk/cache/nsMemoryCacheDevice.cpp
netwerk/streamconv/converters/mozTXTToHTMLConv.cpp
xpcom/ds/nsCRT.cpp
xpcom/ds/nsCRT.h
xpcom/io/nsPipe3.cpp
xpcom/tests/gtest/TestCRT.cpp
--- a/dom/base/nsPlainTextSerializer.cpp
+++ b/dom/base/nsPlainTextSerializer.cpp
@@ -1248,17 +1248,17 @@ nsPlainTextSerializer::Output(nsString& 
   }
   mOutputString->Append(aString);
 }
 
 static bool
 IsSpaceStuffable(const char16_t *s)
 {
   if (s[0] == '>' || s[0] == ' ' || s[0] == kNBSP ||
-      nsCRT::strncmp(s, u"From ", 5) == 0)
+      NS_strncmp(s, u"From ", 5) == 0)
     return true;
   else
     return false;
 }
 
 /**
  * This function adds a piece of text to the current stored line. If we are
  * wrapping text and the stored line will become too long, a suitable
--- a/image/imgLoader.cpp
+++ b/image/imgLoader.cpp
@@ -2726,18 +2726,18 @@ imgLoader::GetMIMETypeFromContent(nsIReq
 
 /* static */
 nsresult
 imgLoader::GetMimeTypeFromContent(const char* aContents,
                                   uint32_t aLength,
                                   nsACString& aContentType)
 {
   /* Is it a GIF? */
-  if (aLength >= 6 && (!nsCRT::strncmp(aContents, "GIF87a", 6) ||
-                       !nsCRT::strncmp(aContents, "GIF89a", 6))) {
+  if (aLength >= 6 && (!strncmp(aContents, "GIF87a", 6) ||
+                       !strncmp(aContents, "GIF89a", 6))) {
     aContentType.AssignLiteral(IMAGE_GIF);
 
   /* or a PNG? */
   } else if (aLength >= 8 && ((unsigned char)aContents[0]==0x89 &&
                               (unsigned char)aContents[1]==0x50 &&
                               (unsigned char)aContents[2]==0x4E &&
                               (unsigned char)aContents[3]==0x47 &&
                               (unsigned char)aContents[4]==0x0D &&
@@ -2764,17 +2764,17 @@ imgLoader::GetMimeTypeFromContent(const 
    * Minor version offset 3. Offset 4 must be nullptr.
    */
   } else if (aLength >= 5 &&
              ((unsigned char) aContents[0])==0x4a &&
              ((unsigned char) aContents[1])==0x47 &&
              ((unsigned char) aContents[4])==0x00 ) {
     aContentType.AssignLiteral(IMAGE_ART);
 
-  } else if (aLength >= 2 && !nsCRT::strncmp(aContents, "BM", 2)) {
+  } else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
     aContentType.AssignLiteral(IMAGE_BMP);
 
   // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
   // CURs begin with 2-byte 0 followed by 2-byte 2.
   } else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
                               !memcmp(aContents, "\000\000\002\000", 4))) {
     aContentType.AssignLiteral(IMAGE_ICO);
 
--- a/layout/style/nsStyleUtil.h
+++ b/layout/style/nsStyleUtil.h
@@ -211,17 +211,17 @@ public:
                                    uint32_t aLineNumber,
                                    const nsAString& aStyleText,
                                    nsresult* aRv);
 
   template<size_t N>
   static bool MatchesLanguagePrefix(const char16_t* aLang, size_t aLen,
                                     const char16_t (&aPrefix)[N])
   {
-    return !nsCRT::strncmp(aLang, aPrefix, N - 1) &&
+    return !NS_strncmp(aLang, aPrefix, N - 1) &&
            (aLen == N - 1 || aLang[N - 1] == '-');
   }
 
   template<size_t N>
   static bool MatchesLanguagePrefix(const nsAtom* aLang,
                                     const char16_t (&aPrefix)[N])
   {
     MOZ_ASSERT(aLang);
--- a/netwerk/base/nsStandardURL.cpp
+++ b/netwerk/base/nsStandardURL.cpp
@@ -2519,18 +2519,17 @@ nsStandardURL::Resolve(const nsACString 
                                         | NET_COALESCE_DOUBLE_SLASH_IS_ROOT);
 
         }
         // this URL appears to be absolute
         // but try to find out more
         if (SegmentIs(mScheme, relpath, scheme, true)) {
             // mScheme and Scheme are the same
             // but this can still be relative
-            if (nsCRT::strncmp(relpath + scheme.mPos + scheme.mLen,
-                               "://",3) == 0) {
+            if (strncmp(relpath + scheme.mPos + scheme.mLen, "://", 3) == 0) {
                 // now this is really absolute
                 // because a :// follows the scheme
                 result = NS_strdup(relpath);
             } else {
                 // This is a deprecated form of relative urls like
                 // http:file or http:/path/file
                 // we will support it for now ...
                 relative = true;
--- a/netwerk/base/nsURLHelper.cpp
+++ b/netwerk/base/nsURLHelper.cpp
@@ -249,17 +249,17 @@ net_CoalesceDirs(netCoalesceFlags flags,
     if (flags & NET_COALESCE_DOUBLE_SLASH_IS_ROOT)
     {
        /* some schemes (for example ftp) have the speciality that
           the path can begin // or /%2F to mark the root of the
           servers filesystem, a simple / only marks the root relative
           to the user loging in. We remember the length of the marker */
         if (nsCRT::strncasecmp(path,"/%2F",4) == 0)
             special_ftp_len = 4;
-        else if (nsCRT::strncmp(path,"//",2) == 0 )
+        else if (strncmp(path, "//", 2) == 0 )
             special_ftp_len = 2;
     }
 
     /* find the last slash before # or ? */
     for(; (*fwdPtr != '\0') &&
             (*fwdPtr != '?') &&
             (*fwdPtr != '#'); ++fwdPtr)
     {
--- a/netwerk/cache/nsMemoryCacheDevice.cpp
+++ b/netwerk/cache/nsMemoryCacheDevice.cpp
@@ -458,17 +458,17 @@ struct ClientIDArgs {
 };
 
 static bool
 EntryMatchesClientID(nsCacheEntry* entry, void* args)
 {
     const char * clientID = static_cast<ClientIDArgs*>(args)->clientID;
     uint32_t prefixLength = static_cast<ClientIDArgs*>(args)->prefixLength;
     const char * key = entry->Key()->get();
-    return !clientID || nsCRT::strncmp(clientID, key, prefixLength) == 0;
+    return !clientID || strncmp(clientID, key, prefixLength) == 0;
 }
 
 nsresult
 nsMemoryCacheDevice::DoEvictEntries(bool (*matchFn)(nsCacheEntry* entry, void* args), void* args)
 {
     nsCacheEntry * entry;
 
     for (int i = kQueueCount - 1; i >= 0; --i) {
--- a/netwerk/streamconv/converters/mozTXTToHTMLConv.cpp
+++ b/netwerk/streamconv/converters/mozTXTToHTMLConv.cpp
@@ -111,32 +111,32 @@ mozTXTToHTMLConv::UnescapeStr(const char
 {
   const char16_t * subString = nullptr;
   for (uint32_t i = aStartPos; int32_t(i) - aStartPos < aLength;)
   {
     int32_t remainingChars = i - aStartPos;
     if (aInString[i] == '&')
     {
       subString = &aInString[i];
-      if (!nsCRT::strncmp(subString, u"&lt;", std::min(4, aLength - remainingChars)))
+      if (!NS_strncmp(subString, u"&lt;", std::min(4, aLength - remainingChars)))
       {
         aOutString.Append(char16_t('<'));
         i += 4;
       }
-      else if (!nsCRT::strncmp(subString, u"&gt;", std::min(4, aLength - remainingChars)))
+      else if (!NS_strncmp(subString, u"&gt;", std::min(4, aLength - remainingChars)))
       {
         aOutString.Append(char16_t('>'));
         i += 4;
       }
-      else if (!nsCRT::strncmp(subString, u"&amp;", std::min(5, aLength - remainingChars)))
+      else if (!NS_strncmp(subString, u"&amp;", std::min(5, aLength - remainingChars)))
       {
         aOutString.Append(char16_t('&'));
         i += 5;
       }
-      else if (!nsCRT::strncmp(subString, u"&quot;", std::min(6, aLength - remainingChars)))
+      else if (!NS_strncmp(subString, u"&quot;", std::min(6, aLength - remainingChars)))
       {
         aOutString.Append(char16_t('"'));
         i += 6;
       }
       else
       {
         aOutString += aInString[i];
         i++;
@@ -189,17 +189,17 @@ bool
 mozTXTToHTMLConv::FindURLStart(const char16_t * aInString, int32_t aInLength,
                                const uint32_t pos, const modetype check,
                                uint32_t& start)
 {
   switch(check)
   { // no breaks, because end of blocks is never reached
   case RFC1738:
   {
-    if (!nsCRT::strncmp(&aInString[std::max(int32_t(pos - 4), 0)], u"<URL:", 5))
+    if (!NS_strncmp(&aInString[std::max(int32_t(pos - 4), 0)], u"<URL:", 5))
     {
       start = pos + 1;
       return true;
     }
     else
       return false;
   }
   case RFC2396E:
--- a/xpcom/ds/nsCRT.cpp
+++ b/xpcom/ds/nsCRT.cpp
@@ -106,45 +106,16 @@ nsCRT::strcmp(const char16_t* aStr1, con
     }
     if (aStr2) {  // aStr1 must have been null
       return 1;
     }
   }
   return 0;
 }
 
-/**
- * Compare unichar string ptrs, stopping at the 1st null or nth char.
- * NOTE: If either is null, we return 0.
- * NOTE: We DO NOT terminate the search upon encountering nullptr's before N
- *
- * @update  gess 11/10/99
- * @param   s1 and s2 both point to unichar strings
- * @return  0 if they match, -1 if s1<s2; 1 if s1>s2
- */
-int32_t
-nsCRT::strncmp(const char16_t* aStr1, const char16_t* aStr2, uint32_t aNum)
-{
-  if (aStr1 && aStr2) {
-    if (aNum != 0) {
-      do {
-        char16_t c1 = *aStr1++;
-        char16_t c2 = *aStr2++;
-        if (c1 != c2) {
-          if (c1 < c2) {
-            return -1;
-          }
-          return 1;
-        }
-      } while (--aNum != 0);
-    }
-  }
-  return 0;
-}
-
 const char*
 nsCRT::memmem(const char* aHaystack, uint32_t aHaystackLen,
               const char* aNeedle, uint32_t aNeedleLen)
 {
   // Sanity checking
   if (!(aHaystack && aNeedle && aHaystackLen && aNeedleLen &&
         aNeedleLen <= aHaystackLen)) {
     return nullptr;
--- a/xpcom/ds/nsCRT.h
+++ b/xpcom/ds/nsCRT.h
@@ -37,22 +37,16 @@ public:
   };
 
   /// String comparison.
   static int32_t strcmp(const char* aStr1, const char* aStr2)
   {
     return int32_t(PL_strcmp(aStr1, aStr2));
   }
 
-  static int32_t strncmp(const char* aStr1, const char* aStr2,
-                         uint32_t aMaxLen)
-  {
-    return int32_t(PL_strncmp(aStr1, aStr2, aMaxLen));
-  }
-
   /// Case-insensitive string comparison.
   static int32_t strcasecmp(const char* aStr1, const char* aStr2)
   {
     return int32_t(PL_strcasecmp(aStr1, aStr2));
   }
 
   /// Case-insensitive string comparison with length
   static int32_t strncasecmp(const char* aStr1, const char* aStr2,
@@ -62,27 +56,16 @@ public:
     //Egads. PL_strncasecmp is returning *very* negative numbers.
     //Some folks expect -1,0,1, so let's temper its enthusiasm.
     if (result < 0) {
       result = -1;
     }
     return result;
   }
 
-  static int32_t strncmp(const char* aStr1, const char* aStr2, int32_t aMaxLen)
-  {
-    // inline the first test (assumes strings are not null):
-    int32_t diff =
-      ((const unsigned char*)aStr1)[0] - ((const unsigned char*)aStr2)[0];
-    if (diff != 0) {
-      return diff;
-    }
-    return int32_t(PL_strncmp(aStr1, aStr2, unsigned(aMaxLen)));
-  }
-
   /**
 
     How to use this fancy (thread-safe) version of strtok:
 
     void main(void) {
       printf("%s\n\nTokens:\n", string);
       // Establish string and get the first token:
       char* newStr;
@@ -96,19 +79,16 @@ public:
     }
     * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
     * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
   */
   static char* strtok(char* aStr, const char* aDelims, char** aNewStr);
 
   /// Like strcmp except for ucs2 strings
   static int32_t strcmp(const char16_t* aStr1, const char16_t* aStr2);
-  /// Like strcmp except for ucs2 strings
-  static int32_t strncmp(const char16_t* aStr1, const char16_t* aStr2,
-                         uint32_t aMaxLen);
 
   // The GNU libc has memmem, which is strstr except for binary data
   // This is our own implementation that uses memmem on platforms
   // where it's available.
   static const char* memmem(const char* aHaystack, uint32_t aHaystackLen,
                             const char* aNeedle, uint32_t aNeedleLen);
 
   // String to longlong
--- a/xpcom/io/nsPipe3.cpp
+++ b/xpcom/io/nsPipe3.cpp
@@ -1547,17 +1547,17 @@ nsPipeInputStream::SetEOF()
   NS_NOTREACHED("nsPipeInputStream::SetEOF");
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 static bool strings_equal(bool aIgnoreCase,
                           const char* aS1, const char* aS2, uint32_t aLen)
 {
   return aIgnoreCase
-    ? !nsCRT::strncasecmp(aS1, aS2, aLen) : !nsCRT::strncmp(aS1, aS2, aLen);
+    ? !nsCRT::strncasecmp(aS1, aS2, aLen) : !strncmp(aS1, aS2, aLen);
 }
 
 NS_IMETHODIMP
 nsPipeInputStream::Search(const char* aForString,
                           bool aIgnoreCase,
                           bool* aFound,
                           uint32_t* aOffsetSearchedTo)
 {
--- a/xpcom/tests/gtest/TestCRT.cpp
+++ b/xpcom/tests/gtest/TestCRT.cpp
@@ -43,25 +43,19 @@ static void Check(const char* s1, const 
 
   nsAutoString t1,t2;
   CopyASCIItoUTF16(s1, t1);
   CopyASCIItoUTF16(s2, t2);
   const char16_t* us1 = t1.get();
   const char16_t* us2 = t2.get();
 
   int u2, u2_n;
-  // nsCRT::strncmp will cause buffer overrun
-  // if the string buffer is shorter than |n|.
-  if (!longerThanN) {
-    u2 = nsCRT::strcmp(us1, us2);
-    u2_n = nsCRT::strncmp(us1, us2, n);
+  u2 = nsCRT::strcmp(us1, us2);
 
-    EXPECT_EQ(sign(clib), sign(u2));
-    EXPECT_EQ(sign(clib_n), sign(u2_n));
-  }
+  EXPECT_EQ(sign(clib), sign(u2));
 
   u2 = NS_strcmp(us1, us2);
   u2_n = NS_strncmp(us1, us2, n);
 
   EXPECT_EQ(sign(clib), sign(u2));
   EXPECT_EQ(sign(clib_n), sign(u2_n));
 }