From 4d3931d626a2311a95a73faf8de333554cd448cf Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 13 Feb 2024 11:45:38 -0800 Subject: [PATCH] Avoid using ctype's isalpha. In general, ctype is local-dependent, and in MSVC isalpha asserts on non-ascii characters PiperOrigin-RevId: 606696757 --- src/google/protobuf/compiler/command_line_interface.cc | 6 +++++- src/google/protobuf/compiler/importer.cc | 10 +++++----- src/google/protobuf/io/BUILD.bazel | 1 + src/google/protobuf/io/io_win32.cc | 4 +++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 60cc517a05..160ef862d0 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -95,6 +95,10 @@ #include "google/protobuf/io/io_win32.h" #endif +#if defined(_WIN32) || defined(__CYGWIN__) +#include "absl/strings/ascii.h" +#endif + // Must be included last. #include "google/protobuf/port_def.inc" @@ -130,7 +134,7 @@ static const char* kDefaultDirectDependenciesViolationMsg = // copy in importer.cc? static bool IsWindowsAbsolutePath(const std::string& text) { #if defined(_WIN32) || defined(__CYGWIN__) - return text.size() >= 3 && text[1] == ':' && isalpha(text[0]) && + return text.size() >= 3 && text[1] == ':' && absl::ascii_isalpha(text[0]) && (text[2] == '/' || text[2] == '\\') && text.find_last_of(':') == 1; #else return false; diff --git a/src/google/protobuf/compiler/importer.cc b/src/google/protobuf/compiler/importer.cc index 9e68da6798..3c4f9ac174 100644 --- a/src/google/protobuf/compiler/importer.cc +++ b/src/google/protobuf/compiler/importer.cc @@ -36,10 +36,6 @@ #include "google/protobuf/io/tokenizer.h" #include "google/protobuf/io/zero_copy_stream_impl.h" -#ifdef _WIN32 -#include -#endif - namespace google { namespace protobuf { namespace compiler { @@ -51,12 +47,16 @@ using google::protobuf::io::win32::access; using google::protobuf::io::win32::open; #endif +#if defined(_WIN32) || defined(__CYGWIN__) +#include "absl/strings/ascii.h" +#endif + // Returns true if the text looks like a Windows-style absolute path, starting // with a drive letter. Example: "C:\foo". TODO: Share this with // copy in command_line_interface.cc? static bool IsWindowsAbsolutePath(absl::string_view text) { #if defined(_WIN32) || defined(__CYGWIN__) - return text.size() >= 3 && text[1] == ':' && isalpha(text[0]) && + return text.size() >= 3 && text[1] == ':' && absl::ascii_isalpha(text[0]) && (text[2] == '/' || text[2] == '\\') && text.find_last_of(':') == 1; #else return false; diff --git a/src/google/protobuf/io/BUILD.bazel b/src/google/protobuf/io/BUILD.bazel index 5163360da2..096ebf71b8 100644 --- a/src/google/protobuf/io/BUILD.bazel +++ b/src/google/protobuf/io/BUILD.bazel @@ -177,6 +177,7 @@ cc_library( deps = [ "//src/google/protobuf:arena", "//src/google/protobuf/stubs:lite", + "@com_google_absl//absl/strings", ], ) diff --git a/src/google/protobuf/io/io_win32.cc b/src/google/protobuf/io/io_win32.cc index 7fc76b99f7..ea43104b95 100644 --- a/src/google/protobuf/io/io_win32.cc +++ b/src/google/protobuf/io/io_win32.cc @@ -37,6 +37,8 @@ #include #include +#include "absl/strings/ascii.h" + #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN 1 #endif @@ -64,7 +66,7 @@ struct CharTraits { template <> struct CharTraits { - static bool is_alpha(char ch) { return isalpha(ch); } + static bool is_alpha(char ch) { return absl::ascii_isalpha(ch); } }; template <>