From 387b71e7821265ef47f8a92d8467a14400ccc0d9 Mon Sep 17 00:00:00 2001 From: tomwillow Date: Mon, 23 Dec 2024 00:33:01 +0800 Subject: [PATCH] uncompleted add -WX compile option --- CMakeLists.txt | 1 + src/Control/TMenu.cpp | 4 ++-- src/Core/Core.cpp | 12 +++++++++++- src/Core/Detect.cpp | 1 + src/Core/Exceptions.h | 5 +++-- src/Core/LineBreaks.cpp | 2 +- src/Core/Vietnamese.cpp | 2 +- src/Core/Vietnamese.h | 2 +- src/SmartCharsetConverter/CLIHandler.cpp | 8 ++++---- src/SmartCharsetConverter/DialogMain.cpp | 2 ++ src/SmartCharsetConverter/DialogMain.h | 7 ++++--- 11 files changed, 31 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a6e6fa..c8b3ca7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ project(SmartCharsetConverter) add_definitions(-DUNICODE -D_UNICODE) add_compile_options("$<$:/utf-8>") +add_compile_options(-WX) if(MSVC) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MT") diff --git a/src/Control/TMenu.cpp b/src/Control/TMenu.cpp index 3ed3d8b..382b35c 100644 --- a/src/Control/TMenu.cpp +++ b/src/Control/TMenu.cpp @@ -40,7 +40,7 @@ void TMenu::InsertItem(int posId, int newItemid, const std::wstring &s) noexcept menuItemInfo.cbSize = sizeof(MENUITEMINFO); menuItemInfo.wID = newItemid; menuItemInfo.fMask = MIIM_ID | MIIM_STRING | MIIM_SUBMENU; - menuItemInfo.cch = s.length() + 1; + menuItemInfo.cch = static_cast(s.length() + 1); menuItemInfo.dwTypeData = const_cast(s.c_str()); menuItemInfo.hSubMenu = hMenu; @@ -72,7 +72,7 @@ void TMenu::SetItemTextByPositionOrId(bool byPosition, int posOrId, const std::w menuItemInfo.cbSize = sizeof(MENUITEMINFO); menuItemInfo.fMask = MIIM_STRING; - menuItemInfo.cch = ws.size(); + menuItemInfo.cch = static_cast(ws.size()); menuItemInfo.dwTypeData = const_cast(ws.data()); BOOL ok = SetMenuItemInfo(hMenu, posOrId, byPosition, &menuItemInfo); assert(ok); diff --git a/src/Core/Core.cpp b/src/Core/Core.cpp index 875086a..f28d8e7 100644 --- a/src/Core/Core.cpp +++ b/src/Core/Core.cpp @@ -49,8 +49,17 @@ std::u16string Decode(std::string_view src, CharsetCode code) { ucnv_setToUCallBack(conv.get(), UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL, &err); DealWithUCNVError(err); + if (cap > std::numeric_limits::max()) { + throw MyRuntimeError(MessageId::STRING_LENGTH_OUT_OF_LIMIT); + } + + if (src.size() > std::numeric_limits::max()) { + throw MyRuntimeError(MessageId::STRING_LENGTH_OUT_OF_LIMIT); + } + // 解码 - int retLen = ucnv_toUChars(conv.get(), target.data(), cap, src.data(), src.size(), &err); + int retLen = ucnv_toUChars(conv.get(), target.data(), static_cast(cap), src.data(), + static_cast(src.size()), &err); target.resize(retLen); DealWithUCNVError(err); @@ -72,6 +81,7 @@ std::u16string DecodeToLimitBytes(std::string_view src, uint64_t maxInputBytes, output = Decode(std::string_view(src.data(), use_bytes), code); break; } catch (const TruncatedCharFoundError &err) { + (err); if (use_bytes != src.size()) { use_bytes--; continue; diff --git a/src/Core/Detect.cpp b/src/Core/Detect.cpp index fd7597a..a8b2ebd 100644 --- a/src/Core/Detect.cpp +++ b/src/Core/Detect.cpp @@ -94,6 +94,7 @@ std::tuple DetectByCED(const char *buf, int len) { code = ToCharsetCode(string_to_wstring(EncodingName(encoding))); } catch (const std::runtime_error &err) { + (err); if (is_reliable) { throw; } diff --git a/src/Core/Exceptions.h b/src/Core/Exceptions.h index 398a0a8..d153c10 100644 --- a/src/Core/Exceptions.h +++ b/src/Core/Exceptions.h @@ -82,7 +82,8 @@ class TruncatedCharFoundError : UCNVError { class ConvertError : public MyRuntimeError { public: - ConvertError(std::string content, int position, viet::Encoding srcEncoding, viet::Encoding destEncoding) noexcept + ConvertError(std::string content, std::size_t position, viet::Encoding srcEncoding, + viet::Encoding destEncoding) noexcept : MyRuntimeError(MessageId::VIETNAMESE_CONVERT_ERROR, fmt::format(MessageIdToBasicString(MessageId::VIETNAMESE_CONVERT_ERROR), to_string(srcEncoding), to_string(srcEncoding), position, content)), @@ -96,7 +97,7 @@ class ConvertError : public MyRuntimeError { private: viet::Encoding srcEncoding; viet::Encoding destEncoding; - int position; + std::size_t position; std::string content; }; diff --git a/src/Core/LineBreaks.cpp b/src/Core/LineBreaks.cpp index 1a6e972..13e9570 100644 --- a/src/Core/LineBreaks.cpp +++ b/src/Core/LineBreaks.cpp @@ -3,7 +3,7 @@ #include -// LineBreaks͵ַӳ +// LineBreaks类型到字符串的映射表 const doublemap lineBreaksMap = { {LineBreaks::CRLF, TEXT("CRLF")}, {LineBreaks::LF, TEXT("LF")}, {LineBreaks::CR, TEXT("CR")}, {LineBreaks::EMPTY, TEXT("")}, {LineBreaks::MIX, TEXT("N/A(Mixed)")}, {LineBreaks::UNKNOWN, TEXT("Unknown")}}; diff --git a/src/Core/Vietnamese.cpp b/src/Core/Vietnamese.cpp index 0e1d0fa..e7a8cde 100644 --- a/src/Core/Vietnamese.cpp +++ b/src/Core/Vietnamese.cpp @@ -351,7 +351,7 @@ void CheckInit() noexcept { assert(Initialized() && "viet module is not initialized"); } -bool CheckEncoding(const char *str, int len, Encoding encoding) noexcept { +bool CheckEncoding(const char *str, std::size_t len, Encoding encoding) noexcept { CheckInit(); if (encoding == Encoding::VPS || encoding == Encoding::VISCII) { const std::unordered_map *dict = nullptr; diff --git a/src/Core/Vietnamese.h b/src/Core/Vietnamese.h index 5f52de9..fd160eb 100644 --- a/src/Core/Vietnamese.h +++ b/src/Core/Vietnamese.h @@ -71,7 +71,7 @@ inline Encoding to_encoding(std::string_view sv) noexcept { */ void Init() noexcept; -bool CheckEncoding(const char *str, int len, Encoding encoding) noexcept; +bool CheckEncoding(const char *str, std::size_t len, Encoding encoding) noexcept; bool CheckEncoding(const std::string &str, Encoding encoding) noexcept; /** diff --git a/src/SmartCharsetConverter/CLIHandler.cpp b/src/SmartCharsetConverter/CLIHandler.cpp index 7e8f3d4..c1b9f5c 100644 --- a/src/SmartCharsetConverter/CLIHandler.cpp +++ b/src/SmartCharsetConverter/CLIHandler.cpp @@ -72,8 +72,7 @@ int CLIMain(const std::vector &args) noexcept { } } catch (const std::runtime_error &err) { - MessageBoxA(NULL, err.what(), "Error", MB_OK | MB_ICONERROR); - return -1; + MessageBoxW(NULL, utf8_to_wstring(err.what()).c_str(), L"Error", MB_OK | MB_ICONERROR); } }); @@ -100,7 +99,7 @@ int CLIMain(const std::vector &args) noexcept { std::vector inputPathes; int state = 0; - for (int i = 1; i < args.size(); ++i) { + for (std::size_t i = 1; i < args.size(); ++i) { std::wstring arg = args[i]; switch (state) { case 0: @@ -193,6 +192,7 @@ int CLIMain(const std::vector &args) noexcept { try { core.SetOutputCharset(ToCharsetCode(arg)); } catch (const std::runtime_error &err) { + (err); ssErr << L"错误:未能识别的字符集名称:" << arg << L"\n"; ssErr << L"提示:使用--help charset可以查看支持的字符集名称。\n"; } @@ -354,7 +354,7 @@ int CLIMain(const std::vector &args) noexcept { } int success = 0, failed = 0; - int total = inputFileNames.size(); + int total = static_cast(inputFileNames.size()); for (int i = 0; i < total; i++) { AddAndConvertOneFile(i + 1, total, inputFileNames[i], success, failed); } diff --git a/src/SmartCharsetConverter/DialogMain.cpp b/src/SmartCharsetConverter/DialogMain.cpp index cfff275..3bd87fd 100644 --- a/src/SmartCharsetConverter/DialogMain.cpp +++ b/src/SmartCharsetConverter/DialogMain.cpp @@ -57,8 +57,10 @@ DialogMain::DialogMain(const std::vector &filenames) : inputFilena languageService = std::make_unique(option); } catch (const nlohmann::json::exception &err) { + (err); throw; } catch (const std::exception &err) { + (err); throw; } } diff --git a/src/SmartCharsetConverter/DialogMain.h b/src/SmartCharsetConverter/DialogMain.h index ab1d483..607ad08 100644 --- a/src/SmartCharsetConverter/DialogMain.h +++ b/src/SmartCharsetConverter/DialogMain.h @@ -125,12 +125,13 @@ class DialogMain : public CDialogImpl { const int SELECT_LANUAGE_ID_CONST = 20000; const int SELECT_LANUAGE_ID_START = SELECT_LANUAGE_ID_CONST + 0; int GetSelectLanguageIdEnd() noexcept { - return SELECT_LANUAGE_ID_START + languageService->GetLanguageArray().size(); + return static_cast(SELECT_LANUAGE_ID_START + languageService->GetLanguageArray().size()); } int LanguageNameToCommandId(const std::string &languageName) noexcept { - return SELECT_LANUAGE_ID_START + std::distance(languageService->GetLanguagesTable().begin(), - languageService->GetLanguagesTable().find(languageName)); + return static_cast(SELECT_LANUAGE_ID_START + + std::distance(languageService->GetLanguagesTable().begin(), + languageService->GetLanguagesTable().find(languageName))); } std::string CommandIdToLanguageName(int id) noexcept {