Bug 1453456 - Remove nsCRT::IsAscii(null-terminated string) in favor of mozilla::IsAsciiNullTerminated. r=froydnj

This commit is contained in:
Jeff Walden 2019-02-25 12:22:24 -08:00
parent a25f0304c2
commit 7b8ce42e6e
9 changed files with 112 additions and 21 deletions

View File

@ -20,6 +20,7 @@
#include "mozilla/Algorithm.h"
#include "mozilla/ipc/CrashReporterClient.h"
#include "mozilla/ipc/ProcessChild.h"
#include "mozilla/TextUtils.h"
#include "GMPUtils.h"
#include "prio.h"
#include "base/task.h"
@ -264,7 +265,8 @@ mozilla::ipc::IPCResult GMPChild::RecvPreloadLibs(const nsCString& aLibs) {
u"msmpeg2vdec.dll", // H.264 decoder
u"psapi.dll", // For GetMappedFileNameW, see bug 1383611
};
constexpr static bool (*IsASCII)(const char16_t*) = NS_IsAscii;
constexpr static bool (*IsASCII)(const char16_t*) =
IsAsciiNullTerminated<char16_t>;
static_assert(AllOf(std::begin(whitelist), std::end(whitelist), IsASCII),
"Items in the whitelist must not contain non-ASCII "
"characters!");

View File

@ -4,6 +4,8 @@
* 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/. */
#include "mozilla/TextUtils.h"
#include "FunctionHook.h"
#include "FunctionBroker.h"
#include "nsClassHashtable.h"
@ -65,7 +67,7 @@ WindowsDllInterceptor* FunctionHook::GetDllInterceptorFor(
sDllInterceptorCache = new DllInterceptors();
}
MOZ_ASSERT(NS_IsAscii(aModuleName),
MOZ_ASSERT(IsAsciiNullTerminated(aModuleName),
"Non-ASCII module names are not supported");
NS_ConvertASCIItoUTF16 moduleName(aModuleName);

View File

@ -41,6 +41,24 @@ constexpr bool IsAscii(Char aChar) {
return uc < 0x80;
}
/**
* Returns true iff every character in the null-terminated string pointed to by
* |aChar| is ASCII, i.e. in the range [0, 0x80).
*/
template <typename Char>
constexpr bool IsAsciiNullTerminated(const Char* aChar) {
while (Char c = *aChar++) {
if (!IsAscii(c)) {
return false;
}
}
return true;
}
/**
* Returns true iff |aChar| is Latin-1 but not ASCII, i.e. in the range
* [0x80, 0xFF].
*/
template <typename Char>
constexpr bool IsNonAsciiLatin1(Char aChar) {
using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;

View File

@ -13,6 +13,7 @@ using mozilla::IsAsciiAlpha;
using mozilla::IsAsciiAlphanumeric;
using mozilla::IsAsciiDigit;
using mozilla::IsAsciiLowercaseAlpha;
using mozilla::IsAsciiNullTerminated;
using mozilla::IsAsciiUppercaseAlpha;
static void TestIsAscii() {
@ -101,6 +102,84 @@ static void TestIsAscii() {
static_assert(!IsAscii(U'\x80'), "U'\\x80' isn't ASCII");
}
static void TestIsAsciiNullTerminated() {
// char
constexpr char allChar[] =
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\0x0C\x0D\x0E\x0F"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\0x1C\x1D\x1E\x1F"
"\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\0x2C\x2D\x2E\x2F"
"\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\0x3C\x3D\x3E\x3F"
"\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\0x4C\x4D\x4E\x4F"
"\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\0x5C\x5D\x5E\x5F"
"\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\0x6C\x6D\x6E\x6F"
"\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\0x7C\x7D\x7E\x7F";
static_assert(IsAsciiNullTerminated(allChar), "allChar is ASCII");
constexpr char loBadChar[] = "\x80";
static_assert(!IsAsciiNullTerminated(loBadChar), "loBadChar isn't ASCII");
constexpr char hiBadChar[] = "\xFF";
static_assert(!IsAsciiNullTerminated(hiBadChar), "hiBadChar isn't ASCII");
// char16_t
constexpr char16_t allChar16[] =
u"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\0x0C\x0D\x0E\x0F"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\0x1C\x1D\x1E\x1F"
"\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\0x2C\x2D\x2E\x2F"
"\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\0x3C\x3D\x3E\x3F"
"\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\0x4C\x4D\x4E\x4F"
"\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\0x5C\x5D\x5E\x5F"
"\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\0x6C\x6D\x6E\x6F"
"\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\0x7C\x7D\x7E\x7F";
static_assert(IsAsciiNullTerminated(allChar16), "allChar16 is ASCII");
constexpr char16_t loBadChar16[] = u"\x80";
static_assert(!IsAsciiNullTerminated(loBadChar16), "loBadChar16 isn't ASCII");
constexpr char16_t hiBadChar16[] = u"\xFF";
static_assert(!IsAsciiNullTerminated(hiBadChar16), "hiBadChar16 isn't ASCII");
constexpr char16_t highestChar16[] = u"\uFFFF";
static_assert(!IsAsciiNullTerminated(highestChar16),
"highestChar16 isn't ASCII");
// char32_t
constexpr char32_t allChar32[] =
U"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\0x0C\x0D\x0E\x0F"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\0x1C\x1D\x1E\x1F"
"\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\0x2C\x2D\x2E\x2F"
"\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\0x3C\x3D\x3E\x3F"
"\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\0x4C\x4D\x4E\x4F"
"\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\0x5C\x5D\x5E\x5F"
"\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\0x6C\x6D\x6E\x6F"
"\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\0x7C\x7D\x7E\x7F";
static_assert(IsAsciiNullTerminated(allChar32), "allChar32 is ASCII");
constexpr char32_t loBadChar32[] = U"\x80";
static_assert(!IsAsciiNullTerminated(loBadChar32), "loBadChar32 isn't ASCII");
constexpr char32_t hiBadChar32[] = U"\xFF";
static_assert(!IsAsciiNullTerminated(hiBadChar32), "hiBadChar32 isn't ASCII");
constexpr char32_t highestChar32[] = {static_cast<char32_t>(-1), 0};
static_assert(!IsAsciiNullTerminated(highestChar32),
"highestChar32 isn't ASCII");
}
static void TestIsAsciiAlpha() {
// char
@ -975,6 +1054,7 @@ static void TestIsAsciiDigit() {
int main() {
TestIsAscii();
TestIsAsciiNullTerminated();
TestIsAsciiAlpha();
TestIsAsciiUppercaseAlpha();
TestIsAsciiLowercaseAlpha();

View File

@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ArrayUtils.h"
#include "mozilla/TextUtils.h"
#include <ole2.h>
#include <shlobj.h>
@ -1157,7 +1158,7 @@ nsDataObj ::GetFileContentsInternetShortcut(FORMATETC& aFE, STGMEDIUM& aSTG) {
rv = icoFile->GetPath(path);
NS_ENSURE_SUCCESS(rv, E_FAIL);
if (NS_IsAscii(path.get())) {
if (IsAsciiNullTerminated(path.get())) {
LossyCopyUTF16toASCII(path, asciiPath);
shortcutFormatStr =
"[InternetShortcut]\r\nURL=%s\r\n"

View File

@ -93,16 +93,6 @@ inline char NS_ToLower(char aChar) {
bool NS_IsUpper(char aChar);
bool NS_IsLower(char aChar);
constexpr bool NS_IsAscii(const char16_t* aString) {
while (*aString) {
if (0x0080 <= *aString) {
return false;
}
aString++;
}
return true;
}
constexpr bool NS_IsAscii(const char* aString) {
while (*aString) {
if (0x80 & *aString) {

View File

@ -12,6 +12,7 @@
#include "mozilla/Mutex.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Sprintf.h"
#include "mozilla/TextUtils.h"
#include "mozilla/Unused.h"
#include "nsAtom.h"
@ -508,7 +509,7 @@ void nsAtomTable::RegisterStaticAtoms(const nsStaticAtom* aAtoms,
for (uint32_t i = 0; i < aAtomsLen; ++i) {
const nsStaticAtom* atom = &aAtoms[i];
MOZ_ASSERT(nsCRT::IsAscii(atom->String()));
MOZ_ASSERT(IsAsciiNullTerminated(atom->String()));
MOZ_ASSERT(NS_strlen(atom->String()) == atom->GetLength());
MOZ_ASSERT(atom->IsAsciiLowercase() ==
::IsAsciiLowercase(atom->String(), atom->GetLength()));

View File

@ -89,14 +89,9 @@ class nsCRT {
static bool IsUpper(char aChar) { return NS_IsUpper(aChar); }
static bool IsLower(char aChar) { return NS_IsLower(aChar); }
static bool IsAscii(const char16_t* aString) { return NS_IsAscii(aString); }
static bool IsAsciiSpace(char16_t aChar) {
return NS_IsAsciiWhitespace(aChar);
}
static bool IsAscii(const char* aString) { return NS_IsAscii(aString); }
static bool IsAscii(const char* aString, uint32_t aLength) {
return NS_IsAscii(aString, aLength);
}
};
inline bool NS_IS_SPACE(char16_t aChar) {

View File

@ -6,10 +6,12 @@
/* Class to manage lookup of static names in a table. */
#include "mozilla/HashFunctions.h"
#include "mozilla/TextUtils.h"
#include "nsCRT.h"
#include "nscore.h"
#include "mozilla/HashFunctions.h"
#include "nsISupportsImpl.h"
#include "nsStaticNameTable.h"
@ -107,7 +109,7 @@ nsStaticCaseInsensitiveNameTable::nsStaticCaseInsensitiveNameTable(
nsDependentCString temp2(raw);
ToLowerCase(temp1);
MOZ_ASSERT(temp1.Equals(temp2), "upper case char in table");
MOZ_ASSERT(nsCRT::IsAscii(raw),
MOZ_ASSERT(IsAsciiNullTerminated(raw),
"non-ascii string in table -- "
"case-insensitive matching won't work right");
}