From 282c47d7cc9e460f48b323445bac5126ae03c876 Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Mon, 19 Aug 2024 19:02:14 +0200 Subject: [PATCH] hopefully fix CodeQL warning about char* => WCHAR* cast --- src/EbookDoc.cpp | 6 ++++-- src/utils/StrUtil.cpp | 7 +++++++ src/utils/StrUtil.h | 1 + src/utils/StrconvUtil.cpp | 7 +++---- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/EbookDoc.cpp b/src/EbookDoc.cpp index 23c789a190af..518a439f8e6d 100644 --- a/src/EbookDoc.cpp +++ b/src/EbookDoc.cpp @@ -87,7 +87,8 @@ static TempStr DecodeTextToUtf8Temp(const char* s, bool isXML = false) { } if (str::StartsWith(s, UTF16_BOM)) { s += 2; - return ToUtf8Temp((WCHAR*)s); + WCHAR* ws = str::ToWCHAR(s); + return ToUtf8Temp(ws); } if (str::StartsWith(s, UTF16BE_BOM)) { // convert from utf16 big endian to utf16 @@ -98,7 +99,8 @@ static TempStr DecodeTextToUtf8Temp(const char* s, bool isXML = false) { int idx = i * 2; std::swap(tmp[idx], tmp[idx + 1]); } - return ToUtf8Temp((WCHAR*)s); + WCHAR* ws = str::ToWCHAR(s); + return ToUtf8Temp(ws); } uint codePage = isXML ? GetCodepageFromPI(s) : CP_ACP; if (CP_ACP == codePage && IsValidUtf8(s)) { diff --git a/src/utils/StrUtil.cpp b/src/utils/StrUtil.cpp index 66844f9bb5fe..fbe040e6fdf3 100644 --- a/src/utils/StrUtil.cpp +++ b/src/utils/StrUtil.cpp @@ -1964,6 +1964,13 @@ bool IsNonCharacter(WCHAR c) { return c >= 0xFFFE || (c & ~1) == 0xDFFE || (0xFDD0 <= c && c <= 0xFDEF); } +// hack: to fool CodeQL which doesn't approve of char* => WCHAR* casts +// and doesn't allow any way to disable that warning +WCHAR* ToWCHAR(const char* s) { + void* d = (void*)s; + return (WCHAR*)d; +} + // return true if s1 == s2, case sensitive bool Eq(const WCHAR* s1, const WCHAR* s2) { if (s1 == s2) { diff --git a/src/utils/StrUtil.h b/src/utils/StrUtil.h index e33566e663ea..1a8f1afa1296 100644 --- a/src/utils/StrUtil.h +++ b/src/utils/StrUtil.h @@ -254,6 +254,7 @@ bool IsNonCharacter(WCHAR c); size_t TransCharsInPlace(WCHAR* str, const WCHAR* oldChars, const WCHAR* newChars); WCHAR* Replace(const WCHAR* s, const WCHAR* toReplace, const WCHAR* replaceWith); +WCHAR* ToWCHAR(const char* s); } // namespace str namespace url { diff --git a/src/utils/StrconvUtil.cpp b/src/utils/StrconvUtil.cpp index 50746cad67a8..e655a0159b35 100644 --- a/src/utils/StrconvUtil.cpp +++ b/src/utils/StrconvUtil.cpp @@ -153,15 +153,14 @@ char* UnknownToUtf8Temp(const char* s) { if (str::StartsWith(s, UTF16BE_BOM)) { // convert from utf16 big endian to utf16 s += 2; - int n = str::Leni((WCHAR*)s); + WCHAR* ws = str::ToWCHAR(s); + int n = str::Leni(ws); char* tmp = (char*)s; for (int i = 0; i < n; i++) { int idx = i * 2; std::swap(tmp[idx], tmp[idx + 1]); } - // codeql complains about char* => WCHAR* cast - void* d = (void*)s; - return ToUtf8Temp((const WCHAR*)d); + return ToUtf8Temp(ws); } // if s is valid utf8, leave it alone