From 91c9a71a5da4202f83b6e30bdc30e5a0f26ad504 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 28 Mar 2025 17:28:19 +0100 Subject: [PATCH] Resolve code analysis warnings Signed-off-by: Simon Rozman --- UnitTests/string.cpp | 1 + include/stdex/minisign.hpp | 12 ++++----- include/stdex/parser.hpp | 50 +++++++++++++++++++------------------- include/stdex/stream.hpp | 3 ++- include/stdex/string.hpp | 4 +-- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/UnitTests/string.cpp b/UnitTests/string.cpp index b7c65c469..e08f37fb0 100644 --- a/UnitTests/string.cpp +++ b/UnitTests/string.cpp @@ -16,6 +16,7 @@ namespace UnitTests { stdex::utf32_t tmp[0x100]; stdex::strncpy(tmp, u"This is a 🐔Test🐮."); + tmp[_countof(tmp) - 1] = 0; Assert::IsTrue(stdex::strcmp(U"This is a 🐔Test🐮.", tmp) == 0); } diff --git a/include/stdex/minisign.hpp b/include/stdex/minisign.hpp index f73d87a05..6fcd9a65f 100644 --- a/include/stdex/minisign.hpp +++ b/include/stdex/minisign.hpp @@ -31,9 +31,9 @@ namespace stdex _In_ int flags = stdex::parser::match_default) { _Unreferenced_(flags); - stdex_assert(text || start + 17 >= end); - if (start + 17 < end && - text[start + 0] == 'u' && + stdex_assert(text || start >= end); + if (start < end && start + 17 < end && + text[start] == 'u' && text[start + 1] == 'n' && text[start + 2] == 't' && text[start + 3] == 'r' && @@ -73,9 +73,9 @@ namespace stdex _In_ int flags = stdex::parser::match_default) { _Unreferenced_(flags); - stdex_assert(text || start + 1 >= end); - if (start + 1 < end && - text[start + 0] == '\r' && + stdex_assert(text || start >= end); + if (start < end && start + 1 < end && + text[start] == '\r' && text[start + 1] == '\n') { this->interval.end = (this->interval.start = start) + 2; diff --git a/include/stdex/parser.hpp b/include/stdex/parser.hpp index bb93c4630..45e859275 100644 --- a/include/stdex/parser.hpp +++ b/include/stdex/parser.hpp @@ -4961,7 +4961,7 @@ namespace stdex uint32_t nominator; this->interval.end = start; - if (this->interval.end + 1 >= end || + if (this->interval.end >= end || this->interval.end + 1 >= end || (case_insensitive ? ctype.toupper(text[this->interval.end]) : text[this->interval.end]) != 'R' || (case_insensitive ? ctype.toupper(text[this->interval.end + 1]) : text[this->interval.end + 1]) != 'F') goto error; // incomplete or wrong reference ID @@ -5199,7 +5199,7 @@ namespace stdex const bool case_insensitive = flags & match_case_insensitive ? true : false; this->interval.end = start; - if (this->interval.end + 1 >= end || + if (this->interval.end >= end || this->interval.end + 1 >= end || (case_insensitive ? ctype.toupper(text[this->interval.end]) : text[this->interval.end]) != 'S' || (case_insensitive ? ctype.toupper(text[this->interval.end + 1]) : text[this->interval.end + 1]) != 'I') goto error; // incomplete or wrong reference ID @@ -5894,7 +5894,7 @@ namespace stdex _In_ int flags = match_default) { stdex_assert(text || start >= end); - if (start + 2 < end && + if (start < end && start + 2 < end && text[start] == '*' && text[start + 1] == '/' && text[start + 2] == '*') @@ -6090,7 +6090,7 @@ namespace stdex for (;;) { if (this->interval.end < end && text[this->interval.end]) { if ('0' <= text[this->interval.end] && text[this->interval.end] <= '9') { - size_t _value = static_cast(value) * 10 + static_cast(text[this->interval.end] - '0'); + size_t _value = static_cast(value) * 10 + text[this->interval.end] - '0'; if (_value > UINT16_MAX) { value = 0; this->interval.invalidate(); @@ -6472,7 +6472,7 @@ namespace stdex for (;;) { if (this->interval.end < end && text[this->interval.end]) { if ('0' <= text[this->interval.end] && text[this->interval.end] <= '9') { - celi_del = celi_del * 10 + static_cast(text[this->interval.end] - '0'); + celi_del = celi_del * 10 + text[this->interval.end] - '0'; this->interval.end++; } else if (text[this->interval.end] == '.') { @@ -6480,7 +6480,7 @@ namespace stdex for (;;) { if (this->interval.end < end && text[this->interval.end]) { if ('0' <= text[this->interval.end] && text[this->interval.end] <= '9') { - decimalni_del = decimalni_del * 10 + static_cast(text[this->interval.end] - '0'); + decimalni_del = decimalni_del * 10 + text[this->interval.end] - '0'; decimalni_del_n *= 10; this->interval.end++; } @@ -7368,8 +7368,8 @@ namespace stdex _In_ int flags = match_multiline) { _Unreferenced_(flags); - stdex_assert(text || start + 1 >= end); - if (start + 1 < end && + stdex_assert(text || start >= end); + if (start < end && start + 1 < end && text[start] == '/' && text[start + 1] == '*') { @@ -7419,8 +7419,8 @@ namespace stdex _In_ int flags = match_multiline) { _Unreferenced_(flags); - stdex_assert(text || start + 3 >= end); - if (start + 3 < end && + stdex_assert(text || start >= end); + if (start < end && start + 3 < end && text[start] == '<' && text[start + 1] == '!' && text[start + 2] == '-' && @@ -7457,8 +7457,8 @@ namespace stdex _In_ int flags = match_multiline) { _Unreferenced_(flags); - stdex_assert(text || start + 2 >= end); - if (start + 2 < end && + stdex_assert(text || start >= end); + if (start < end && start + 2 < end && text[start] == '-' && text[start + 1] == '-' && text[start + 2] == '>') @@ -7571,8 +7571,8 @@ namespace stdex { _Unreferenced_(flags); this->interval.end = start; - stdex_assert(text || this->interval.end + 3 >= end); - if (this->interval.end + 3 < end && + stdex_assert(text || this->interval.end >= end); + if (this->interval.end < end && this->interval.end + 3 < end && (text[this->interval.end] == 'u' || text[this->interval.end] == 'U') && (text[this->interval.end + 1] == 'r' || text[this->interval.end + 1] == 'R') && (text[this->interval.end + 2] == 'l' || text[this->interval.end + 2] == 'L') && @@ -7681,8 +7681,8 @@ namespace stdex { _Unreferenced_(flags); this->interval.end = start; - stdex_assert(text || this->interval.end + 6 >= end); - if (this->interval.end + 6 < end && + stdex_assert(text || this->interval.end >= end); + if (this->interval.end < end && this->interval.end + 6 < end && text[this->interval.end] == '@' && (text[this->interval.end + 1] == 'i' || text[this->interval.end + 1] == 'I') && (text[this->interval.end + 2] == 'm' || text[this->interval.end + 2] == 'M') && @@ -7811,7 +7811,7 @@ namespace stdex // Skip whitespace. for (; this->interval.end < end && text[this->interval.end] && ctype.is(ctype.space, text[this->interval.end]); this->interval.end++); - if (this->interval.end + 7 < end && + if (this->interval.end < end && this->interval.end + 7 < end && (text[this->interval.end] == 'c' || text[this->interval.end] == 'C') && (text[this->interval.end + 1] == 'h' || text[this->interval.end + 1] == 'H') && (text[this->interval.end + 2] == 'a' || text[this->interval.end + 2] == 'A') && @@ -8074,7 +8074,7 @@ namespace stdex else if (text[this->interval.end] == '!') { // interval.end++; - if (this->interval.end + 1 < end && + if (this->interval.end < end && this->interval.end + 1 < end && text[this->interval.end] == '-' && text[this->interval.end + 1] == '-') { @@ -8149,7 +8149,7 @@ namespace stdex this->attributes.clear(); for (;;) { if (this->type == html_sequence_t::element_start && - this->interval.end + 1 < end && + this->interval.end < end && this->interval.end + 1 < end && text[this->interval.end] == '/' && text[this->interval.end + 1] == '>') { @@ -8166,7 +8166,7 @@ namespace stdex break; } if (this->type == html_sequence_t::declaration && - this->interval.end + 1 < end && + this->interval.end < end && this->interval.end + 1 < end && text[this->interval.end] == '!' && text[this->interval.end + 1] == '>') { @@ -8175,7 +8175,7 @@ namespace stdex break; } if (this->type == html_sequence_t::declaration && - this->interval.end + 1 < end && + this->interval.end < end && this->interval.end + 1 < end && text[this->interval.end] == '-' && text[this->interval.end + 1] == '-') { @@ -8285,8 +8285,8 @@ namespace stdex _In_ int flags = match_multiline) { _Unreferenced_(flags); - stdex_assert(text || start + 2 >= end); - if (start + 2 < end && + stdex_assert(text || start >= end); + if (start < end && start + 2 < end && text[start] == '<' && text[start + 1] == '!' && text[start + 2] == '[') @@ -8342,8 +8342,8 @@ namespace stdex _In_ int flags = match_multiline) { _Unreferenced_(flags); - stdex_assert(text || start + 2 >= end); - if (start + 2 < end && + stdex_assert(text || start >= end); + if (start < end && start + 2 < end && text[start] == ']' && text[start + 1] == ']' && text[start + 2] == '>') diff --git a/include/stdex/stream.hpp b/include/stdex/stream.hpp index 1f8cd3151..73617363e 100644 --- a/include/stdex/stream.hpp +++ b/include/stdex/stream.hpp @@ -1195,7 +1195,8 @@ namespace stdex case op_t::flush: source->flush(); break; - case op_t::noop:; + case op_t::noop: + break; } op = op_t::noop; lk.unlock(); diff --git a/include/stdex/string.hpp b/include/stdex/string.hpp index 92bc9d828..32d31b0e1 100644 --- a/include/stdex/string.hpp +++ b/include/stdex/string.hpp @@ -1840,7 +1840,7 @@ namespace stdex /// template size_t strncpy( - _Out_ _Post_maybez_ T1(&dst)[N1], + _Inout_ _Post_maybez_ T1(&dst)[N1], _In_ const T2(&src)[N2]) { return strncpy(dst, N1, src, N2); @@ -2823,7 +2823,7 @@ namespace stdex } size_t offset = str.size(); str.resize(offset + count); - if (vsnprintf(&str[offset], count + 1, format, locale, arg) != count) _Unlikely_ + if (vsnprintf(&str[offset], static_cast(count) + 1, format, locale, arg) != count) _Unlikely_ throw std::runtime_error("failed to format string"); #else size_t offset = str.size();