From 7b96e2958da4f5a4a1ed345b99f2e958bf49269a Mon Sep 17 00:00:00 2001 From: rc-swag <58423624+rc-swag@users.noreply.github.com> Date: Sat, 23 Nov 2024 18:13:10 +1000 Subject: [PATCH 1/4] feat(windows): load keyboard file to memorary blob --- windows/src/engine/keyman32/K32_load.cpp | 37 +++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/windows/src/engine/keyman32/K32_load.cpp b/windows/src/engine/keyman32/K32_load.cpp index c6bd1d4dfe1..2801d449f01 100644 --- a/windows/src/engine/keyman32/K32_load.cpp +++ b/windows/src/engine/keyman32/K32_load.cpp @@ -29,6 +29,7 @@ */ #include "pch.h" +#include BOOL GetKeyboardFileName(LPSTR kbname, LPSTR buf, int nbuf) { @@ -78,13 +79,41 @@ BOOL LoadlpKeyboard(int i) _td->lpKeyboards[i].lpCoreKeyboardState = NULL; } - char buf[256]; - if (!GetKeyboardFileName(_td->lpKeyboards[i].Name, buf, 255)) { + char kmx_filename[256]; + if (!GetKeyboardFileName(_td->lpKeyboards[i].Name, kmx_filename, 255)) { return_SendDebugExit(FALSE); } - PWCHAR keyboardPath = strtowstr(buf); - km_core_status err_status = km_core_keyboard_load(keyboardPath, &_td->lpKeyboards[i].lpCoreKeyboard); + FILE* kmx_file; + errno_t status = fopen_s(&kmx_file, kmx_filename, "rb"); + if (!status) { + SendDebugMessageFormat("Problem reading opening kmx_file %s status [%d].", kmx_filename, status); + return_SendDebugExit(FALSE); + } + + fseek(kmx_file, 0, SEEK_END); + size_t length = ftell(kmx_file); + fseek(kmx_file, 0, SEEK_SET); + void* buffer = malloc(length); + + if (!buffer) { + fclose(kmx_file); + return_SendDebugExit(FALSE); + } + + if (fread(buffer, 1, length, kmx_file) != length) { + SendDebugMessageFormat("Problem reading entire kmx_file %s.", kmx_filename); + fclose(kmx_file); + free(buffer); + return_SendDebugExit(FALSE); + } + + fclose(kmx_file); + PWCHAR keyboardPath = strtowstr(kmx_filename); + km_core_status err_status = km_core_keyboard_load_from_blob(keyboardPath, buffer, length, &_td->lpKeyboards[i].lpCoreKeyboard); + + free(buffer); + if (err_status != KM_CORE_STATUS_OK) { SendDebugMessageFormat("km_core_keyboard_load failed for %ls with error status [%d]", keyboardPath, err_status); delete[] keyboardPath; From f3e5c1bc77f09a552fc90e9ddc41cacdeb42a8f0 Mon Sep 17 00:00:00 2001 From: rc-swag <58423624+rc-swag@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:03:58 +1000 Subject: [PATCH 2/4] feat(windows): add exiterror goto plus extra checks Adds ExitError goto as there where too many paths requiring similar clean up. Checks around reading the file. --- windows/src/engine/keyman32/K32_load.cpp | 58 ++++++++++++++++-------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/windows/src/engine/keyman32/K32_load.cpp b/windows/src/engine/keyman32/K32_load.cpp index 2801d449f01..f54cf60abe4 100644 --- a/windows/src/engine/keyman32/K32_load.cpp +++ b/windows/src/engine/keyman32/K32_load.cpp @@ -84,40 +84,52 @@ BOOL LoadlpKeyboard(int i) return_SendDebugExit(FALSE); } - FILE* kmx_file; + FILE* kmx_file = nullptr; + PWCHAR keyboardPath = nullptr; + void* buffer = nullptr; errno_t status = fopen_s(&kmx_file, kmx_filename, "rb"); + if (!status) { SendDebugMessageFormat("Problem reading opening kmx_file %s status [%d].", kmx_filename, status); return_SendDebugExit(FALSE); } - fseek(kmx_file, 0, SEEK_END); + if (fseek(kmx_file, 0, SEEK_END) < 0 ) { + SendDebugMessageFormat("Problem seeking to end of kmx_file %s.", kmx_filename); + goto ExitError; + } + size_t length = ftell(kmx_file); - fseek(kmx_file, 0, SEEK_SET); - void* buffer = malloc(length); + + if (length < 0) + { + SendDebugMessageFormat("Problem determining length of kmx_file %s.", kmx_filename); + goto ExitError; + } + + rewind(kmx_file); + + buffer = malloc(length); if (!buffer) { - fclose(kmx_file); - return_SendDebugExit(FALSE); + SendDebugMessageFormat("Problem allocating buffer for reading kmx_file %s.", kmx_filename); + goto ExitError; } if (fread(buffer, 1, length, kmx_file) != length) { SendDebugMessageFormat("Problem reading entire kmx_file %s.", kmx_filename); - fclose(kmx_file); - free(buffer); - return_SendDebugExit(FALSE); + goto ExitError; } fclose(kmx_file); - PWCHAR keyboardPath = strtowstr(kmx_filename); + keyboardPath = strtowstr(kmx_filename); km_core_status err_status = km_core_keyboard_load_from_blob(keyboardPath, buffer, length, &_td->lpKeyboards[i].lpCoreKeyboard); free(buffer); if (err_status != KM_CORE_STATUS_OK) { SendDebugMessageFormat("km_core_keyboard_load failed for %ls with error status [%d]", keyboardPath, err_status); - delete[] keyboardPath; - return_SendDebugExit(FALSE); + goto ExitError; } delete[] keyboardPath; @@ -134,22 +146,30 @@ BOOL LoadlpKeyboard(int i) if (err_status != KM_CORE_STATUS_OK) { SendDebugMessageFormat("km_core_state_create failed with error status [%d]", err_status); - // Dispose of the keyboard to leave us in a consistent state - ReleaseKeyboardMemoryCore(&_td->lpKeyboards[i].lpCoreKeyboard); - return_SendDebugExit(FALSE); + goto ExitError; } // Register callback? err_status = km_core_keyboard_get_imx_list(_td->lpKeyboards[i].lpCoreKeyboard, &_td->lpKeyboards[i].lpIMXList); if (err_status != KM_CORE_STATUS_OK) { SendDebugMessageFormat("km_core_keyboard_get_imx_list failed with error status [%d]", err_status); - // Dispose of the keyboard to leave us in a consistent state - ReleaseKeyboardMemoryCore(&_td->lpKeyboards[i].lpCoreKeyboard); - return_SendDebugExit(FALSE); + goto ExitError; } LoadDLLs(&_td->lpKeyboards[i]); - LoadKeyboardOptionsRegistrytoCore(&_td->lpKeyboards[i], _td->lpKeyboards[i].lpCoreKeyboardState); return_SendDebugExit(TRUE); +ExitError: + if (kmx_file) { + fclose(kmx_file); + } + if (buffer) { + free(buffer); + } + if (keyboardPath != nullptr) { + delete[] keyboardPath; + } + // Dispose of the keyboard to leave us in a consistent state + ReleaseKeyboardMemoryCore(&_td->lpKeyboards[i].lpCoreKeyboard); + return_SendDebugExit(FALSE); } From 5fd2b1aa5af62a01c157e292b5c5173a129bbaef Mon Sep 17 00:00:00 2001 From: rc-swag <58423624+rc-swag@users.noreply.github.com> Date: Wed, 27 Nov 2024 01:43:55 +1000 Subject: [PATCH 3/4] feat(windows): fix reading error status --- windows/src/engine/keyman32/K32_load.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/windows/src/engine/keyman32/K32_load.cpp b/windows/src/engine/keyman32/K32_load.cpp index f54cf60abe4..6e770cc73af 100644 --- a/windows/src/engine/keyman32/K32_load.cpp +++ b/windows/src/engine/keyman32/K32_load.cpp @@ -89,8 +89,8 @@ BOOL LoadlpKeyboard(int i) void* buffer = nullptr; errno_t status = fopen_s(&kmx_file, kmx_filename, "rb"); - if (!status) { - SendDebugMessageFormat("Problem reading opening kmx_file %s status [%d].", kmx_filename, status); + if (status) { + SendDebugMessageFormat("Problem opening kmx_file %s status [%d].", kmx_filename, status); return_SendDebugExit(FALSE); } From adf2313a8b298f0c73e7015c42c78cd61eb3918e Mon Sep 17 00:00:00 2001 From: rc-swag <58423624+rc-swag@users.noreply.github.com> Date: Wed, 27 Nov 2024 12:25:41 +1000 Subject: [PATCH 4/4] feat(windows): self nit on whitespace --- windows/src/engine/keyman32/K32_load.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/windows/src/engine/keyman32/K32_load.cpp b/windows/src/engine/keyman32/K32_load.cpp index 6e770cc73af..6ad3fc90b2a 100644 --- a/windows/src/engine/keyman32/K32_load.cpp +++ b/windows/src/engine/keyman32/K32_load.cpp @@ -108,7 +108,6 @@ BOOL LoadlpKeyboard(int i) } rewind(kmx_file); - buffer = malloc(length); if (!buffer) {