diff --git a/js/public/CharacterEncoding.h b/js/public/CharacterEncoding.h index af040ffbc4a3..fe39a415c77f 100644 --- a/js/public/CharacterEncoding.h +++ b/js/public/CharacterEncoding.h @@ -323,6 +323,13 @@ UTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen); extern Latin1CharsZ LossyUTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen); +/* + * Returns true if all characters in the given null-terminated string are + * ASCII, i.e. < 0x80, false otherwise. + */ +extern bool +StringIsASCII(const char* s); + } // namespace JS inline void JS_free(JS::Latin1CharsZ& ptr) { js_free((void*)ptr.get()); } diff --git a/js/src/jscntxt.cpp b/js/src/jscntxt.cpp index 9b064aff76ad..7bb4d8b36565 100644 --- a/js/src/jscntxt.cpp +++ b/js/src/jscntxt.cpp @@ -317,17 +317,6 @@ checkReportFlags(JSContext* cx, unsigned* flags) return false; } -#ifdef DEBUG -static void -AssertIsASCII(const char* s) -{ - while (*s) { - MOZ_ASSERT((*s & 0x80) == 0); - s++; - } -} -#endif - bool js::ReportErrorVA(JSContext* cx, unsigned flags, const char* format, ErrorArgumentsType argumentsType, va_list ap) @@ -348,10 +337,7 @@ js::ReportErrorVA(JSContext* cx, unsigned flags, const char* format, } messagelen = strlen(message); -#ifdef DEBUG - if (argumentsType == ArgumentsAreASCII) - AssertIsASCII(message); -#endif + MOZ_ASSERT_IF(argumentsType == ArgumentsAreASCII, JS::StringIsASCII(message)); report.flags = flags; report.errorNumber = JSMSG_USER_DEFINED_ERROR; @@ -570,10 +556,7 @@ class MOZ_RAII AutoMessageArgs const char* charArg = va_arg(ap, char*); size_t charArgLength = strlen(charArg); -#ifdef DEBUG - if (typeArg == ArgumentsAreASCII) - AssertIsASCII(charArg); -#endif + MOZ_ASSERT_IF(typeArg == ArgumentsAreASCII, JS::StringIsASCII(charArg)); args_[i] = InflateString(cx, charArg, &charArgLength); if (!args_[i]) @@ -631,10 +614,7 @@ js::ExpandErrorArgumentsVA(ExclusiveContext* cx, JSErrorCallback callback, if (efs) { reportp->exnType = efs->exnType; -#ifdef DEBUG - if (argumentsType == ArgumentsAreASCII) - AssertIsASCII(efs->format); -#endif + MOZ_ASSERT_IF(argumentsType == ArgumentsAreASCII, JS::StringIsASCII(efs->format)); uint16_t argCount = efs->argCount; MOZ_RELEASE_ASSERT(argCount <= JS::MaxNumErrorArguments); diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index b3fedad81f31..24a95b34bce1 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -3790,7 +3790,7 @@ Compile(JSContext* cx, unsigned argc, Value* vp) } if (!args[0].isString()) { const char* typeName = InformalValueTypeName(args[0]); - JS_ReportError(cx, "expected string to compile, got %s", typeName); + JS_ReportErrorASCII(cx, "expected string to compile, got %s", typeName); return false; } @@ -3827,7 +3827,7 @@ ParseModule(JSContext* cx, unsigned argc, Value* vp) if (!args[0].isString()) { const char* typeName = InformalValueTypeName(args[0]); - JS_ReportError(cx, "expected string to compile, got %s", typeName); + JS_ReportErrorASCII(cx, "expected string to compile, got %s", typeName); return false; } @@ -3840,7 +3840,7 @@ ParseModule(JSContext* cx, unsigned argc, Value* vp) if (args.length() > 1) { if (!args[1].isString()) { const char* typeName = InformalValueTypeName(args[1]); - JS_ReportError(cx, "expected filename string, got %s", typeName); + JS_ReportErrorASCII(cx, "expected filename string, got %s", typeName); return false; } @@ -3882,7 +3882,7 @@ SetModuleResolveHook(JSContext* cx, unsigned argc, Value* vp) if (!args[0].isObject() || !args[0].toObject().is()) { const char* typeName = InformalValueTypeName(args[0]); - JS_ReportError(cx, "expected hook function, got %s", typeName); + JS_ReportErrorASCII(cx, "expected hook function, got %s", typeName); return false; } @@ -3915,7 +3915,7 @@ Parse(JSContext* cx, unsigned argc, Value* vp) } if (!args[0].isString()) { const char* typeName = InformalValueTypeName(args[0]); - JS_ReportError(cx, "expected string to parse, got %s", typeName); + JS_ReportErrorASCII(cx, "expected string to parse, got %s", typeName); return false; } @@ -3966,7 +3966,7 @@ SyntaxParse(JSContext* cx, unsigned argc, Value* vp) } if (!args[0].isString()) { const char* typeName = InformalValueTypeName(args[0]); - JS_ReportError(cx, "expected string to parse, got %s", typeName); + JS_ReportErrorASCII(cx, "expected string to parse, got %s", typeName); return false; } @@ -4031,7 +4031,7 @@ OffThreadCompileScript(JSContext* cx, unsigned argc, Value* vp) } if (!args[0].isString()) { const char* typeName = InformalValueTypeName(args[0]); - JS_ReportError(cx, "expected string to parse, got %s", typeName); + JS_ReportErrorASCII(cx, "expected string to parse, got %s", typeName); return false; } diff --git a/js/src/vm/CharacterEncoding.cpp b/js/src/vm/CharacterEncoding.cpp index 74fe3cc06384..1d28511bae23 100644 --- a/js/src/vm/CharacterEncoding.cpp +++ b/js/src/vm/CharacterEncoding.cpp @@ -518,3 +518,14 @@ JS::ConstUTF8CharsZ::validate(size_t aLength) /* smallestEncoding = */ nullptr); } #endif + +bool +JS::StringIsASCII(const char* s) +{ + while (*s) { + if (*s & 0x80) + return false; + s++; + } + return true; +} diff --git a/js/src/vm/ObjectGroup.cpp b/js/src/vm/ObjectGroup.cpp index 701817bd28d0..9a9831f552f8 100644 --- a/js/src/vm/ObjectGroup.cpp +++ b/js/src/vm/ObjectGroup.cpp @@ -13,6 +13,7 @@ #include "gc/Policy.h" #include "gc/StoreBuffer.h" #include "gc/Zone.h" +#include "js/CharacterEncoding.h" #include "vm/ArrayObject.h" #include "vm/TaggedProto.h" #include "vm/UnboxedObject.h" @@ -37,6 +38,7 @@ ObjectGroup::ObjectGroup(const Class* clasp, TaggedProto proto, JSCompartment* c /* Windows may not appear on prototype chains. */ MOZ_ASSERT_IF(proto.isObject(), !IsWindow(proto.toObject())); + MOZ_ASSERT(JS::StringIsASCII(clasp->name)); this->clasp_ = clasp; this->proto_ = proto; diff --git a/js/src/vm/ObjectGroup.h b/js/src/vm/ObjectGroup.h index 4bc3e69cd74b..4e24de9f14b8 100644 --- a/js/src/vm/ObjectGroup.h +++ b/js/src/vm/ObjectGroup.h @@ -12,6 +12,7 @@ #include "ds/IdValuePair.h" #include "gc/Barrier.h" +#include "js/CharacterEncoding.h" #include "js/GCHashTable.h" #include "vm/TaggedProto.h" #include "vm/TypeInference.h" @@ -102,6 +103,7 @@ class ObjectGroup : public gc::TenuredCell } void setClasp(const Class* clasp) { + MOZ_ASSERT(JS::StringIsASCII(clasp->name)); clasp_ = clasp; }