From f755999c0e5b5b7d92114e94a760211f63b22b96 Mon Sep 17 00:00:00 2001 From: mikee47 Date: Sun, 3 Nov 2019 14:10:50 +0000 Subject: [PATCH] Fix `getHostAppDir()`, broken in #1913. --- Sming/Arch/Host/Components/hostlib/hostlib.c | 8 +++++--- Sming/Arch/Host/Components/hostlib/hostlib.h | 9 +++++++-- Sming/Arch/Host/Components/spi_flash/flashmem.cpp | 9 ++++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Sming/Arch/Host/Components/hostlib/hostlib.c b/Sming/Arch/Host/Components/hostlib/hostlib.c index 0e53f2ef07..c94357b75a 100644 --- a/Sming/Arch/Host/Components/hostlib/hostlib.c +++ b/Sming/Arch/Host/Components/hostlib/hostlib.c @@ -29,10 +29,10 @@ int msleep(unsigned ms) return nanosleep(&req, &rem); } -void getHostAppDir(char* path, size_t bufSize) +size_t getHostAppDir(char* path, size_t bufSize) { if(path == NULL ||bufSize == 0) { - return; + return 0; } size_t len; @@ -46,7 +46,9 @@ void getHostAppDir(char* path, size_t bufSize) #endif path[len] = '\0'; char* p = strrchr(path, sep); - if(p) { + if(p != NULL) { p[1] = '\0'; + len = 1 + p - path; } + return len; } diff --git a/Sming/Arch/Host/Components/hostlib/hostlib.h b/Sming/Arch/Host/Components/hostlib/hostlib.h index 94172cecb3..c2e52a10f5 100644 --- a/Sming/Arch/Host/Components/hostlib/hostlib.h +++ b/Sming/Arch/Host/Components/hostlib/hostlib.h @@ -55,8 +55,13 @@ extern "C" { int msleep(unsigned ms); -// Include trailing path separator -void getHostAppDir(char* path, size_t bufSize); +/** + * @brief Get directory where application is executing from + * @param path Receives directory path, including trailing path separator + * @param bufSize + * @retval size_t Number of characters written, excluding NUL + */ +size_t getHostAppDir(char* path, size_t bufSize); #ifdef __cplusplus } diff --git a/Sming/Arch/Host/Components/spi_flash/flashmem.cpp b/Sming/Arch/Host/Components/spi_flash/flashmem.cpp index 4eb3374259..2dfba0694d 100644 --- a/Sming/Arch/Host/Components/spi_flash/flashmem.cpp +++ b/Sming/Arch/Host/Components/spi_flash/flashmem.cpp @@ -24,7 +24,7 @@ static int flashFile = -1; static size_t flashFileSize = 0x400000U; static char flashFileName[256]; -static const char* defaultFlashFileName = "flash.bin"; +static const char defaultFlashFileName[] = "flash.bin"; #define SPI_FLASH_SEC_SIZE 4096 @@ -42,8 +42,11 @@ bool host_flashmem_init(FlashmemConfig& config) strncpy(flashFileName, config.filename, sizeof(flashFileName)); flashFileName[sizeof(flashFileName) - 1] = '\0'; } else { - getHostAppDir(flashFileName, sizeof(flashFileName)); - strcpy(flashFileName, defaultFlashFileName); + size_t len = getHostAppDir(flashFileName, sizeof(flashFileName)); + if(len + sizeof(defaultFlashFileName) > sizeof(flashFileName)) { + return false; + } + memcpy(&flashFileName[len], defaultFlashFileName, sizeof(defaultFlashFileName)); config.filename = flashFileName; }