Bug 1542736 - Part 5: Replace std::isdigit with mozilla::IsAsciiDigit. r=jwalden
authorAndré Bargull <andre.bargull@gmail.com>
Thu, 11 Apr 2019 11:35:26 +0000
changeset 469056 21497aef0046d257fbaeec2e88e9e625b2d55c01
parent 469055 544ee6df501b898955b11055eedeca824ca2605d
child 469057 e130d82fa361173eb53c78c030e705170c6a8e0d
push id35856
push usercsabou@mozilla.com
push dateFri, 12 Apr 2019 03:19:48 +0000
treeherdermozilla-central@940684cd1065 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwalden
bugs1542736
milestone68.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 1542736 - Part 5: Replace std::isdigit with mozilla::IsAsciiDigit. r=jwalden mozilla::IsAsciiDigit is equivalent to std::isdigit, except it's not necessary to worry about UB when calling it with an input which can't be represented as `unsigned char`. Differential Revision: https://phabricator.services.mozilla.com/D26508
js/src/gc/GC.cpp
js/src/jsdate.cpp
js/src/vm/Initialization.cpp
js/src/vm/JSContext.cpp
--- a/js/src/gc/GC.cpp
+++ b/js/src/gc/GC.cpp
@@ -191,21 +191,21 @@
 
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/DebugOnly.h"
 #include "mozilla/MacroForEach.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Move.h"
 #include "mozilla/Range.h"
 #include "mozilla/ScopeExit.h"
+#include "mozilla/TextUtils.h"
 #include "mozilla/TimeStamp.h"
 #include "mozilla/TypeTraits.h"
 #include "mozilla/Unused.h"
 
-#include <ctype.h>
 #include <initializer_list>
 #include <string.h>
 #ifndef XP_WIN
 #  include <sys/mman.h>
 #  include <unistd.h>
 #endif
 
 #include "jsapi.h"
@@ -1153,17 +1153,17 @@ static bool ParseZealModeName(CharRange 
 }
 
 static bool ParseZealModeNumericParam(CharRange text, uint32_t* paramOut) {
   if (text.length() == 0) {
     return false;
   }
 
   for (auto c : text) {
-    if (!isdigit(c)) {
+    if (!mozilla::IsAsciiDigit(c)) {
       return false;
     }
   }
 
   *paramOut = atoi(text.begin().get());
   return true;
 }
 
--- a/js/src/jsdate.cpp
+++ b/js/src/jsdate.cpp
@@ -53,16 +53,17 @@
 #include "vm/JSObject-inl.h"
 
 using namespace js;
 
 using mozilla::ArrayLength;
 using mozilla::Atomic;
 using mozilla::BitwiseCast;
 using mozilla::IsAsciiAlpha;
+using mozilla::IsAsciiDigit;
 using mozilla::IsFinite;
 using mozilla::IsNaN;
 using mozilla::NumbersAreIdentical;
 using mozilla::Relaxed;
 
 using JS::AutoCheckCannotGC;
 using JS::ClippedTime;
 using JS::GenericNaN;
@@ -2886,21 +2887,22 @@ static bool ToLocaleFormatHelper(JSConte
     if (result_len == 0) {
       return FormatDate(cx, utcTime, FormatSpec::DateTime, rval);
     }
 
     /* Hacked check against undesired 2-digit year 00/00/00 form. */
     if (strcmp(format, "%x") == 0 && result_len >= 6 &&
         /* Format %x means use OS settings, which may have 2-digit yr, so
            hack end of 3/11/22 or 11.03.22 or 11Mar22 to use 4-digit yr...*/
-        !isdigit(buf[result_len - 3]) && isdigit(buf[result_len - 2]) &&
-        isdigit(buf[result_len - 1]) &&
+        !IsAsciiDigit(buf[result_len - 3]) &&
+        IsAsciiDigit(buf[result_len - 2]) &&
+        IsAsciiDigit(buf[result_len - 1]) &&
         /* ...but not if starts with 4-digit year, like 2022/3/11. */
-        !(isdigit(buf[0]) && isdigit(buf[1]) && isdigit(buf[2]) &&
-          isdigit(buf[3]))) {
+        !(IsAsciiDigit(buf[0]) && IsAsciiDigit(buf[1]) &&
+          IsAsciiDigit(buf[2]) && IsAsciiDigit(buf[3]))) {
       int year = int(YearFromTime(localTime));
       snprintf(buf + (result_len - 2), (sizeof buf) - (result_len - 2), "%d",
                year);
     }
   }
 
   if (cx->runtime()->localeCallbacks &&
       cx->runtime()->localeCallbacks->localeToUnicode) {
--- a/js/src/vm/Initialization.cpp
+++ b/js/src/vm/Initialization.cpp
@@ -4,18 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* SpiderMonkey initialization and shutdown code. */
 
 #include "js/Initialization.h"
 
 #include "mozilla/Assertions.h"
-
-#include <ctype.h>
+#include "mozilla/TextUtils.h"
 
 #include "jstypes.h"
 
 #include "builtin/AtomicsObject.h"
 #include "ds/MemoryProtectionExceptionHandler.h"
 #include "gc/Statistics.h"
 #include "jit/AtomicOperations.h"
 #include "jit/ExecutableAllocator.h"
@@ -41,17 +40,17 @@ using JS::detail::InitState;
 using JS::detail::libraryInitState;
 
 InitState JS::detail::libraryInitState;
 
 #ifdef DEBUG
 static unsigned MessageParameterCount(const char* format) {
   unsigned numfmtspecs = 0;
   for (const char* fmt = format; *fmt != '\0'; fmt++) {
-    if (*fmt == '{' && isdigit(fmt[1])) {
+    if (*fmt == '{' && mozilla::IsAsciiDigit(fmt[1])) {
       ++numfmtspecs;
     }
   }
   return numfmtspecs;
 }
 
 static void CheckMessageParameterCounts() {
   // Assert that each message format has the correct number of braced
--- a/js/src/vm/JSContext.cpp
+++ b/js/src/vm/JSContext.cpp
@@ -12,17 +12,16 @@
 
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/DebugOnly.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Sprintf.h"
 #include "mozilla/TextUtils.h"
 #include "mozilla/Unused.h"
 
-#include <ctype.h>
 #include <stdarg.h>
 #include <string.h>
 #ifdef ANDROID
 #  include <android/log.h>
 #  include <fstream>
 #  include <string>
 #endif  // ANDROID
 #ifdef XP_WIN
@@ -733,17 +732,17 @@ bool ExpandErrorArgumentsHelper(JSContex
         char* utf8 = out = cx->pod_malloc<char>(expandedLength + 1);
         if (!out) {
           return false;
         }
 
         fmt = efs->format;
         while (*fmt) {
           if (*fmt == '{') {
-            if (isdigit(fmt[1])) {
+            if (mozilla::IsAsciiDigit(fmt[1])) {
               int d = AsciiDigitToNumber(fmt[1]);
               MOZ_RELEASE_ASSERT(d < args.count());
               strncpy(out, args.args(d), args.lengths(d));
               out += args.lengths(d);
               fmt += 3;
 #ifdef DEBUG
               expandedArgs++;
 #endif