Skip to content

Commit

Permalink
hopefully fix CodeQL warning about char* => WCHAR* cast
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Aug 19, 2024
1 parent fed1314 commit 282c47d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/EbookDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand Down
7 changes: 7 additions & 0 deletions src/utils/StrUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/StrUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions src/utils/StrconvUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 282c47d

Please sign in to comment.