Bug 1542736 - Part 6: Replace std::isprint with js::IsAsciiPrintable. r=jwalden
authorAndré Bargull <andre.bargull@gmail.com>
Thu, 11 Apr 2019 11:58:17 +0000
changeset 469057 e130d82fa361173eb53c78c030e705170c6a8e0d
parent 469056 21497aef0046d257fbaeec2e88e9e625b2d55c01
child 469058 ed1ef97375801e123d5edf8bbe569f8411852004
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 6: Replace std::isprint with js::IsAsciiPrintable. r=jwalden Provide js::IsAsciiPrintable as a safe alternative to std::isprint, which doesn't lead to UB for inputs not representable as `unsigned char` and which also doesn't depend on the current locale. Differential Revision: https://phabricator.services.mozilla.com/D26509
js/src/jit/CacheIRSpewer.cpp
js/src/jsdate.cpp
js/src/util/Text.h
js/src/vm/Printer.cpp
--- a/js/src/jit/CacheIRSpewer.cpp
+++ b/js/src/jit/CacheIRSpewer.cpp
@@ -13,16 +13,17 @@
 #  ifdef XP_WIN
 #    include <process.h>
 #    define getpid _getpid
 #  else
 #    include <unistd.h>
 #  endif
 #  include <stdarg.h>
 
+#  include "util/Text.h"
 #  include "vm/JSFunction.h"
 #  include "vm/JSObject.h"
 #  include "vm/JSScript.h"
 
 #  include "vm/JSObject-inl.h"
 #  include "vm/Realm-inl.h"
 
 using namespace js;
@@ -104,17 +105,17 @@ static void QuoteString(GenericPrinter& 
   const CharT* end = s + length;
   for (const CharT* t = s; t < end; s = ++t) {
     // This quote implementation is probably correct,
     // but uses \u even when not strictly necessary.
     char16_t c = *t;
     if (c == '"' || c == '\\') {
       out.printf("\\");
       out.printf("%c", char(c));
-    } else if (c < ' ' || c >= 127 || !isprint(c)) {
+    } else if (!IsAsciiPrintable(c)) {
       out.printf("\\u%04x", c);
     } else {
       out.printf("%c", char(c));
     }
   }
 }
 
 static void QuoteString(GenericPrinter& out, JSLinearString* str) {
--- a/js/src/jsdate.cpp
+++ b/js/src/jsdate.cpp
@@ -19,17 +19,16 @@
 
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/Atomics.h"
 #include "mozilla/Casting.h"
 #include "mozilla/FloatingPoint.h"
 #include "mozilla/Sprintf.h"
 #include "mozilla/TextUtils.h"
 
-#include <ctype.h>
 #include <math.h>
 #include <string.h>
 
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "jsnum.h"
 #include "jstypes.h"
 #include "jsutil.h"
@@ -2751,17 +2750,17 @@ JSString* DateTimeHelper::timeZoneCommen
     // Decide whether to use the resulting time zone string.
     //
     // Reject it if it contains any non-ASCII or non-printable characters.
     // It's then likely in some other character encoding, and we probably
     // won't display it correctly.
     bool usetz = true;
     for (size_t i = 0; i < tzlen; i++) {
       char16_t c = tzbuf[i];
-      if (c > 127 || !isprint(c)) {
+      if (!IsAsciiPrintable(c)) {
         usetz = false;
         break;
       }
     }
 
     // Also reject it if it's not parenthesized or if it's ' ()'.
     if (tzbuf[0] != ' ' || tzbuf[1] != '(' || tzbuf[2] == ')') {
       usetz = false;
--- a/js/src/util/Text.h
+++ b/js/src/util/Text.h
@@ -44,16 +44,23 @@ class StringBuffer;
 
 template <typename CharT>
 constexpr uint8_t AsciiDigitToNumber(CharT c) {
   using UnsignedCharT = std::make_unsigned_t<CharT>;
   auto uc = static_cast<UnsignedCharT>(c);
   return uc - '0';
 }
 
+template <typename CharT>
+static constexpr bool IsAsciiPrintable(CharT c) {
+  using UnsignedCharT = std::make_unsigned_t<CharT>;
+  auto uc = static_cast<UnsignedCharT>(c);
+  return ' ' <= uc && uc <= '~';
+}
+
 template <typename Char1, typename Char2>
 inline bool EqualChars(const Char1* s1, const Char2* s2, size_t len) {
   return mozilla::ArrayEqual(s1, s2, len);
 }
 
 // Return less than, equal to, or greater than zero depending on whether
 // s1 is less than, equal to, or greater than s2.
 template <typename Char1, typename Char2>
--- a/js/src/vm/Printer.cpp
+++ b/js/src/vm/Printer.cpp
@@ -5,17 +5,16 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "vm/Printer.h"
 
 #include "mozilla/PodOperations.h"
 #include "mozilla/Printf.h"
 #include "mozilla/RangedPtr.h"
 
-#include <ctype.h>
 #include <stdarg.h>
 #include <stdio.h>
 
 #include "jsutil.h"
 
 #include "ds/LifoAlloc.h"
 #include "js/CharacterEncoding.h"
 #include "util/Text.h"
@@ -288,17 +287,17 @@ static bool QuoteString(Sprinter* sp, co
 
   /* Loop control variables: end points at end of string sentinel. */
   for (CharPtr t = chars.begin(); t < end; ++t) {
     /* Move t forward from s past un-quote-worthy characters. */
     const CharPtr s = t;
     char16_t c = *t;
     while (c < 127 && c != '\\') {
       if (target == QuoteTarget::String) {
-        if (!isprint(c) || c == quote || c == '\t') {
+        if (!IsAsciiPrintable(c) || c == quote || c == '\t') {
           break;
         }
       } else {
         if (c < ' ' || c == '"') {
           break;
         }
       }