From 6f28e2a3e69de720418a056afa01ed7951fd5fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Tue, 26 Sep 2023 09:38:47 +0200 Subject: [PATCH 1/3] Install report hook in StoreAPI --- msix/storeapi/dllmain.cpp | 46 +++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/msix/storeapi/dllmain.cpp b/msix/storeapi/dllmain.cpp index 938f1e3f7..e21996a2b 100644 --- a/msix/storeapi/dllmain.cpp +++ b/msix/storeapi/dllmain.cpp @@ -1,19 +1,43 @@ // dllmain.cpp : Defines the entry point for the DLL application. #include "framework.hpp" -BOOL APIENTRY DllMain( HMODULE hModule, - DWORD ul_reason_for_call, - LPVOID lpReserved - ) -{ - switch (ul_reason_for_call) - { +#ifdef _MSC_VER +#include + +#include +#include + +int DebugReportHook(int reportType, char* message, int* returnValue) { + const auto type = [=]() -> std::string_view { + switch (reportType) { + case _CRT_WARN: + return "[WARNING]"; + case _CRT_ERROR: + return "[ERROR]"; + case _CRT_ASSERT: + return "[ASSERT]"; + default: + return "[UNKNOWN]"; + } + }(); + + std::cerr << type << ' ' << message << std::endl; + throw std::runtime_error(message); +} +#else +#define _CrtSetReportHook(x) +#endif // _MSC_VER + +BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, + LPVOID lpReserved) { + _CrtSetReportHook(DebugReportHook); + + switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: - break; - } - return TRUE; + break; + } + return TRUE; } - From c0d5d6a4a730f65405885e40fad5783ec18f31b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Tue, 26 Sep 2023 09:50:18 +0200 Subject: [PATCH 2/3] Expand coverage of try-catch so that assertion errors are always caught --- msix/storeapi/StoreApi.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/msix/storeapi/StoreApi.cpp b/msix/storeapi/StoreApi.cpp index 378327ce8..bc07ff508 100644 --- a/msix/storeapi/StoreApi.cpp +++ b/msix/storeapi/StoreApi.cpp @@ -46,16 +46,16 @@ void logError(std::string_view functionName, std::string_view errMsg) { Int GetSubscriptionExpirationDate(const char* productID, std::int64_t* expirationUnix) { - if (auto err = validateArg(productID, MaxProductIdLen); - err != StoreApi::ErrorCode::None) { - return toInt(err); - } + try { + if (auto err = validateArg(productID, MaxProductIdLen); + err != StoreApi::ErrorCode::None) { + return toInt(err); + } - if (expirationUnix == nullptr) { - return toInt(StoreApi::ErrorCode::NullOutputPtr); - } + if (expirationUnix == nullptr) { + return toInt(StoreApi::ErrorCode::NullOutputPtr); + } - try { StoreApi::ServerStoreService service{}; *expirationUnix = service.CurrentExpirationDate(productID); @@ -75,16 +75,16 @@ Int GetSubscriptionExpirationDate(const char* productID, Int GenerateUserJWT(const char* accessToken, char** userJWT, std::uint64_t* userJWTLen) { - if (auto err = validateArg(accessToken, MaxTokenLen); - err != StoreApi::ErrorCode::None) { - return toInt(err); - } + try { + if (auto err = validateArg(accessToken, MaxTokenLen); + err != StoreApi::ErrorCode::None) { + return toInt(err); + } - if (userJWT == nullptr || userJWTLen == nullptr) { - return toInt(StoreApi::ErrorCode::NullOutputPtr); - } + if (userJWT == nullptr || userJWTLen == nullptr) { + return toInt(StoreApi::ErrorCode::NullOutputPtr); + } - try { StoreApi::ServerStoreService service{}; auto user = service.CurrentUserInfo(); const std::string jwt = service.GenerateUserJwt(accessToken, user); From 63c6867368c6e605c17ebbea1649b169e72b489c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Tue, 26 Sep 2023 15:30:39 +0200 Subject: [PATCH 3/3] Avoid cerr stuttering This also considerably simplifies the code. You may be concerned that I no longer print the reportType. That is because the message already contains it. --- msix/storeapi/dllmain.cpp | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/msix/storeapi/dllmain.cpp b/msix/storeapi/dllmain.cpp index e21996a2b..27d4bea0f 100644 --- a/msix/storeapi/dllmain.cpp +++ b/msix/storeapi/dllmain.cpp @@ -1,36 +1,19 @@ // dllmain.cpp : Defines the entry point for the DLL application. +#include + #include "framework.hpp" #ifdef _MSC_VER #include - -#include -#include - -int DebugReportHook(int reportType, char* message, int* returnValue) { - const auto type = [=]() -> std::string_view { - switch (reportType) { - case _CRT_WARN: - return "[WARNING]"; - case _CRT_ERROR: - return "[ERROR]"; - case _CRT_ASSERT: - return "[ASSERT]"; - default: - return "[UNKNOWN]"; - } - }(); - - std::cerr << type << ' ' << message << std::endl; - throw std::runtime_error(message); -} #else -#define _CrtSetReportHook(x) +constexpr void _CrtSetReportHook(auto) {} #endif // _MSC_VER BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - _CrtSetReportHook(DebugReportHook); + _CrtSetReportHook([](int reportType, char* message, int* returnValue) -> int { + throw std::runtime_error(message); + }); switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: