From 2bc8ce7c9c5d5244048b5ade9091370fb99313ad Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Tue, 21 Oct 2025 10:04:26 -0700 Subject: [PATCH] Fix warnings with _MSC_VER and _MSC_VER --- src/delta.c | 33 +++++++++++- tools/bin-assemble/bin-assemble.c | 83 ++++++++++++++++++++++--------- 2 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/delta.c b/src/delta.c index 3c33a0aded..878f85ca1d 100644 --- a/src/delta.c +++ b/src/delta.c @@ -23,7 +23,6 @@ #include #include - #define ESC 0x7f @@ -174,6 +173,8 @@ int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len) #include #include #include +#include /* INT_MAX */ +#include /* PRIu32 */ static uint32_t wolfboot_sector_size = 0; @@ -181,10 +182,21 @@ int wb_diff_get_sector_size(void) { uint32_t sec_sz = 0; char *env_sector_size = NULL; +#if defined(_MSC_VER) /* MSVC only, not _WIN32 that includes MSYS2/MinGW */ + char* dup = NULL; + size_t len = 0; + if ((_dupenv_s(&dup, &len, "WOLFBOOT_SECTOR_SIZE") == 0) && dup) { + env_sector_size = dup; + } +#else env_sector_size = getenv("WOLFBOOT_SECTOR_SIZE"); +#endif if (!env_sector_size) { fprintf(stderr, "Please set the WOLFBOOT_SECTOR_SIZE environment variable in\n" "order to sign a delta update.\n"); +#if defined(_MSC_VER) + free(dup); +#endif exit(6); } else { sec_sz = atoi(env_sector_size); @@ -193,11 +205,28 @@ int wb_diff_get_sector_size(void) sec_sz = strtol(env_sector_size, NULL, 16); if (errno != 0) { fprintf(stderr, "Invalid WOLFBOOT_SECTOR_SIZE value\n"); +#if defined(_MSC_VER) + free(dup); +#endif exit(6); } } } - return sec_sz; + +#if defined(_MSC_VER) + free(dup); +#endif + + if (sec_sz == 0) { + fprintf(stderr, "WOLFBOOT_SECTOR_SIZE cannot be 0\n"); + exit(6); + } + if (sec_sz > (uint32_t)INT_MAX) { + fprintf(stderr, "WOLFBOOT_SECTOR_SIZE (%" PRIu32 ") exceeds INT_MAX (%d)\n", + sec_sz, INT_MAX); + exit(6); + } + return (int)sec_sz; } int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, uint8_t *src_b, uint32_t len_b) diff --git a/tools/bin-assemble/bin-assemble.c b/tools/bin-assemble/bin-assemble.c index 5fddc2eccc..c65585bb7b 100644 --- a/tools/bin-assemble/bin-assemble.c +++ b/tools/bin-assemble/bin-assemble.c @@ -72,8 +72,25 @@ int binentry_address_compare(const void* a, const void* b) } } +#ifdef _WIN32 +/* 'strerror' has been explicitly marked deprecated */ +static const char* win_strerror(int err, char* buf, size_t bufsz) { + if (strerror_s(buf, bufsz, err) != 0) { + buf[0] = '\0'; + } + return buf; +} +#endif + int main(int argc, const char* argv[]) { +#ifdef _WIN32 + char errbuf[128] = { 0 }; + errno_t fe; +#endif const char* outname = NULL; + FILE* fo = NULL; + FILE* fi = NULL; + size_t i = 0; size_t num_entries = 0; binentry_t* entries = NULL; @@ -98,20 +115,25 @@ int main(int argc, const char* argv[]) { return EXIT_FAILURE; } - for (i=0; i entries[i].address) { fprintf(stderr, - "overlap with %s(end address 0x%zx) and %s (start address 0x%zx \n", - entries[i-1].fname, endaddr, - entries[i].fname, entries[i].address); + "overlap with %s(end address 0x%zx) and %s (start address 0x%zx \n", + entries[i - 1].fname, endaddr, + entries[i].fname, entries[i].address); err = 1; } if (err) { @@ -144,23 +166,39 @@ int main(int argc, const char* argv[]) { } // TODO: consider handling stdout "-" - - FILE* fo = fopen(outname, "wb"); - if (fo == NULL) { +#ifdef _WIN32 + fe = fopen_s(&fo, outname, "wb"); + if (fo == NULL || fe != 0) { fprintf(stderr, "opening %s failed %s\n", - outname, strerror(errno)); + outname, win_strerror((int)fe, errbuf, sizeof errbuf)); return EXIT_FAILURE; } +#else + fo = fopen(outname, "wb"); + if (fo == NULL) { + fprintf(stderr, "opening %s failed %s\n", outname, strerror(errno)); + return EXIT_FAILURE; + } +#endif cur_add = entries[0].address; for (i=0; i 0 && fillSz < INT_MAX) { /* fill until address - blocks then bytes */ @@ -194,7 +232,7 @@ int main(int argc, const char* argv[]) { cur_add += nw; if (nr != nw) { fprintf(stderr, - "Failed to wrote %zu bytes of the %zu bytes read from %s\n", + "Failed to write %zu bytes of the %zu bytes read from %s\n", nw, nr, entries[i].fname); return EXIT_FAILURE; } @@ -209,10 +247,9 @@ int main(int argc, const char* argv[]) { if (ferror(fo)) { fprintf(stderr, "error writing to %s\n", - entries[i].fname); + outname); return EXIT_FAILURE; } - } fprintf(stderr, "\tAdded %12zu bytes at 0x%08zx from %s\n",