From 521d30d2cfc5306fec52eb1102b164a20845ea8d Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Sat, 19 Apr 2025 16:42:38 +0300 Subject: [PATCH] Cast character types when calling character classification functions. --- src/format_parser.cpp | 3 ++- src/setup/filter_parser.cpp | 5 +++-- src/setup/formatter_parser.cpp | 7 ++++--- src/setup/init_from_settings.cpp | 16 +++++++++------- src/setup/parser_utils.cpp | 10 +++++----- src/setup/parser_utils.hpp | 2 +- src/setup/settings_parser.cpp | 7 ++++--- src/text_file_backend.cpp | 2 +- 8 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/format_parser.cpp b/src/format_parser.cpp index 1215d6b..5b50763 100644 --- a/src/format_parser.cpp +++ b/src/format_parser.cpp @@ -42,6 +42,7 @@ BOOST_LOG_API format_description< CharT > parse_format(const CharT* begin, const typedef CharT char_type; typedef format_description< char_type > description; typedef typename encoding< char_type >::type traits; + typedef typename traits::classify_type char_classify_type; const char_type* original_begin = begin; description descr; @@ -74,7 +75,7 @@ BOOST_LOG_API format_description< CharT > parse_format(const CharT* begin, const } // Check if this is a positional argument - if (traits::isdigit(c)) + if (traits::isdigit(static_cast< char_classify_type >(c))) { if (c != static_cast< char_type >('0')) { diff --git a/src/setup/filter_parser.cpp b/src/setup/filter_parser.cpp index cf41bca..4379e1d 100644 --- a/src/setup/filter_parser.cpp +++ b/src/setup/filter_parser.cpp @@ -123,6 +123,7 @@ private: typedef CharT char_type; typedef const char_type* iterator_type; typedef typename log::aux::encoding< char_type >::type encoding; + typedef typename encoding::classify_type char_classify_type; typedef log::aux::encoding_specific< encoding > encoding_specific; typedef std::basic_string< char_type > string_type; typedef log::aux::char_constants< char_type > constants; @@ -314,7 +315,7 @@ private: else { // Check for custom relation - while (next != end && (encoding::isalnum(*next) || *next == constants::char_underline)) + while (next != end && (encoding::isalnum(static_cast< char_classify_type >(*next)) || *next == constants::char_underline)) ++next; if (p == next) goto DoneL; @@ -347,7 +348,7 @@ private: char_type c1 = *p, c2 = *keyword; if (c2 == 0) { - if (encoding::isspace(c1)) + if (encoding::isspace(static_cast< char_classify_type >(c1))) { next = p; return true; diff --git a/src/setup/formatter_parser.cpp b/src/setup/formatter_parser.cpp index 597b6ca..1cd1d70 100644 --- a/src/setup/formatter_parser.cpp +++ b/src/setup/formatter_parser.cpp @@ -183,6 +183,7 @@ private: typedef basic_formatter< char_type > formatter_type; typedef boost::log::aux::char_constants< char_type > constants; typedef typename log::aux::encoding< char_type >::type encoding; + typedef typename encoding::classify_type char_classify_type; typedef log::aux::encoding_specific< encoding > encoding_specific; typedef formatter_factory< char_type > formatter_factory_type; typedef typename formatter_factory_type::args_map args_map; @@ -299,14 +300,14 @@ private: // Read argument name iterator_type start = p; - if (!encoding::isalpha(*p)) + if (!encoding::isalpha(static_cast< char_classify_type >(*p))) BOOST_LOG_THROW_DESCR(parse_error, "Placeholder argument name is invalid"); for (++p; p != end; ++p) { c = *p; - if (encoding::isspace(c) || c == constants::char_equal) + if (encoding::isspace(static_cast< char_classify_type >(c)) || c == constants::char_equal) break; - if (!encoding::isalnum(c) && c != constants::char_underline) + if (!encoding::isalnum(static_cast< char_classify_type >(c)) && c != constants::char_underline) BOOST_LOG_THROW_DESCR(parse_error, "Placeholder argument name is invalid"); } diff --git a/src/setup/init_from_settings.cpp b/src/setup/init_from_settings.cpp index de639df..86f4c41 100644 --- a/src/setup/init_from_settings.cpp +++ b/src/setup/init_from_settings.cpp @@ -116,7 +116,8 @@ struct is_case_insensitive_equal result_type operator() (CharT left, CharT right) const BOOST_NOEXCEPT { typedef typename boost::log::aux::encoding< CharT >::type encoding; - return encoding::tolower(left) == encoding::tolower(right); + typedef typename encoding::classify_type char_classify_type; + return encoding::tolower(static_cast< char_classify_type >(left)) == encoding::tolower(static_cast< char_classify_type >(right)); } }; @@ -193,6 +194,7 @@ sinks::file::rotation_at_time_point param_cast_to_rotation_time_point(const char typedef CharT char_type; typedef boost::log::aux::char_constants< char_type > constants; typedef typename boost::log::aux::encoding< char_type >::type encoding; + typedef typename encoding::classify_type char_classify_type; typedef qi::extract_uint< unsigned short, 10, 1, 2 > day_extract; typedef qi::extract_uint< unsigned char, 10, 2, 2 > time_component_extract; @@ -202,14 +204,14 @@ sinks::file::rotation_at_time_point param_cast_to_rotation_time_point(const char unsigned char hour = 0, minute = 0, second = 0; const char_type* begin = value.c_str(), *end = begin + value.size(); - if (!encoding::isalnum(*begin)) // begin is null-terminated, so we also check that the string is not empty here + if (!encoding::isalnum(static_cast< char_classify_type >(*begin))) // begin is null-terminated, so we also check that the string is not empty here throw_invalid_value(param_name); const char_type* p = begin + 1; - if (encoding::isalpha(*begin)) + if (encoding::isalpha(static_cast< char_classify_type >(*begin))) { // This must be a weekday - while (encoding::isalpha(*p)) + while (encoding::isalpha(static_cast< char_classify_type >(*p))) ++p; std::size_t len = p - begin; @@ -233,10 +235,10 @@ sinks::file::rotation_at_time_point param_cast_to_rotation_time_point(const char else { // This may be either a month day or an hour - while (encoding::isdigit(*p)) + while (encoding::isdigit(static_cast< char_classify_type >(*p))) ++p; - if (encoding::isspace(*p)) + if (encoding::isspace(static_cast< char_classify_type >(*p))) { // This is a month day unsigned short mday = 0; @@ -256,7 +258,7 @@ sinks::file::rotation_at_time_point param_cast_to_rotation_time_point(const char } // Skip spaces - while (encoding::isspace(*p)) + while (encoding::isspace(static_cast< char_classify_type >(*p))) ++p; // Parse hour diff --git a/src/setup/parser_utils.cpp b/src/setup/parser_utils.cpp index 08a5de8..a8c3299 100644 --- a/src/setup/parser_utils.cpp +++ b/src/setup/parser_utils.cpp @@ -60,7 +60,7 @@ const char_constants< char >::char_type char_constants< char >::char_paren_brack const char* char_constants< char >::trim_spaces_left(const char_type* begin, const char_type* end) { using namespace std; - while (begin != end && isspace(*begin)) + while (begin != end && isspace(static_cast< unsigned char >(*begin))) ++begin; return begin; } @@ -69,7 +69,7 @@ const char* char_constants< char >::trim_spaces_left(const char_type* begin, con const char* char_constants< char >::trim_spaces_right(const char_type* begin, const char_type* end) { using namespace std; - while (begin != end && isspace(*(end - 1))) + while (begin != end && isspace(static_cast< unsigned char >(*(end - 1)))) --end; return end; } @@ -81,7 +81,7 @@ const char* char_constants< char >::scan_attr_placeholder(const char_type* begin while (begin != end) { char_type c = *begin; - if (!isalnum(c) && c != char_underline) + if (!isalnum(static_cast< unsigned char >(c)) && c != char_underline) break; ++begin; } @@ -132,7 +132,7 @@ const char* char_constants< char >::parse_operand(const char_type* begin, const for (++p; p != end; ++p) { c = *p; - if (!isalnum(c) && c != '_' && c != '-' && c != '+' && c != '.') + if (!isalnum(static_cast< unsigned char >(c)) && c != '_' && c != '-' && c != '+' && c != '.') break; } @@ -174,7 +174,7 @@ void char_constants< char >::translate_escape_sequences(string_type& str) if (std::distance(++b, str.end()) >= 2) { char_type c1 = *b++, c2 = *b++; - if (isxdigit(c1) && isxdigit(c2)) + if (isxdigit(static_cast< unsigned char >(c1)) && isxdigit(static_cast< unsigned char >(c2))) { *it++ = char_type((to_number(c1) << 4) | to_number(c2)); it = str.erase(it, b); diff --git a/src/setup/parser_utils.hpp b/src/setup/parser_utils.hpp index bf0be4b..f6e6180 100644 --- a/src/setup/parser_utils.hpp +++ b/src/setup/parser_utils.hpp @@ -150,7 +150,7 @@ struct char_constants< char > { using namespace std; // to make sure we can use C functions unqualified int n = 0; - if (isdigit(c)) + if (isdigit(static_cast< unsigned char >(c))) n = c - '0'; else if (c >= 'a' && c <= 'f') n = c - 'a' + 10; diff --git a/src/setup/settings_parser.cpp b/src/setup/settings_parser.cpp index 9a06345..e3e99a6 100644 --- a/src/setup/settings_parser.cpp +++ b/src/setup/settings_parser.cpp @@ -50,6 +50,7 @@ private: typedef CharT char_type; typedef const char_type* iterator_type; typedef typename log::aux::encoding< char_type >::type encoding; + typedef typename encoding::classify_type char_classify_type; typedef settings_parser< char_type > this_type; typedef std::basic_string< char_type > string_type; @@ -157,7 +158,7 @@ private: for (iterator_type p = begin; p != end; ++p) { char_type c = *p; - if (c != constants::char_dot && !encoding::isalnum(c)) + if (c != constants::char_dot && !encoding::isalnum(static_cast< char_classify_type >(c))) BOOST_LOG_THROW_DESCR_PARAMS(parse_error, "Section name is invalid", (m_LineCounter)); } @@ -183,12 +184,12 @@ private: BOOST_LOG_THROW_DESCR_PARAMS(parse_error, "Parameter name is empty", (m_LineCounter)); iterator_type p = begin; - if (!encoding::isalpha(*p)) + if (!encoding::isalpha(static_cast< char_classify_type >(*p))) BOOST_LOG_THROW_DESCR_PARAMS(parse_error, "Parameter name is invalid", (m_LineCounter)); for (++p; p != end; ++p) { char_type c = *p; - if (!encoding::isgraph(c)) + if (!encoding::isgraph(static_cast< char_classify_type >(c))) BOOST_LOG_THROW_DESCR_PARAMS(parse_error, "Parameter name is invalid", (m_LineCounter)); } diff --git a/src/text_file_backend.cpp b/src/text_file_backend.cpp index f1e8f28..dcf1faa 100644 --- a/src/text_file_backend.cpp +++ b/src/text_file_backend.cpp @@ -133,7 +133,7 @@ BOOST_LOG_ANONYMOUS_NAMESPACE { static bool is_digit(char c) { using namespace std; - return (isdigit(c) != 0); + return (isdigit(static_cast< unsigned char >(c)) != 0); } static std::string default_file_name_pattern() { return "%5N.log"; } };