From 38587a377124f54d63e43a345df1459d2f545af2 Mon Sep 17 00:00:00 2001 From: doomfrawen Date: Sun, 9 Aug 2026 09:16:07 -0700 Subject: [PATCH 1/3] Fix MinGW/Windows build bug and ACR122U PC/SC reconnect issue - contrib/win32/stdlib.c: setenv()/unsetenv() declared 'char *str[32]' (an array of 32 uninitialized pointers) instead of 'char str[32]' (a buffer). Under older GCC this only warned; GCC >= 14 rejects it as an incompatible pointer type, and even when it compiles the original code writes through a garbage pointer. - libnfc/drivers/acr122_pcsc.c: acr122_pcsc_close() disconnected with SCARD_LEAVE_CARD, which on an ACR122U talking through Windows' inbox CCID driver (WUDFUsbccidDriver) leaves the reader's internal session state such that the *next* SCardConnect from any process fails with 'No ACR122 firmware received' until the device is physically replugged. Disconnecting with SCARD_UNPOWER_CARD forces a clean depower/reset of the card interface on close, matching what a physical replug does, and the reader is immediately usable again by the next open. --- contrib/win32/stdlib.c | 4 ++-- libnfc/drivers/acr122_pcsc.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/win32/stdlib.c b/contrib/win32/stdlib.c index 5a1bd5604..19b7d691d 100644 --- a/contrib/win32/stdlib.c +++ b/contrib/win32/stdlib.c @@ -39,7 +39,7 @@ int setenv(const char *name, const char *value, int overwrite) { char *env = getenv(name); if ((env && overwrite) || (!env)) { - char *str[32]; + char str[32]; strcpy(str, name); strcat(str, "="); strcat(str, value); @@ -50,7 +50,7 @@ int setenv(const char *name, const char *value, int overwrite) void unsetenv(const char *name) { - char *str[32]; + char str[32]; strcpy(str, name); strcat(str, "="); putenv(str); diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index f5cc02e25..5c7082a89 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -317,7 +317,7 @@ acr122_pcsc_close(nfc_device *pnd) { pn53x_idle(pnd); - SCardDisconnect(DRIVER_DATA(pnd)->hCard, SCARD_LEAVE_CARD); + SCardDisconnect(DRIVER_DATA(pnd)->hCard, SCARD_UNPOWER_CARD); acr122_pcsc_free_scardcontext(); pn53x_data_free(pnd); From 746cccda48391aace433cca054710a677c6671c4 Mon Sep 17 00:00:00 2001 From: doomfrawen Date: Thu, 19 Nov 2026 00:03:39 -0700 Subject: [PATCH 2/3] Handle Windows environment variables without fixed-size buffers Address the overflow and overwrite=0 feedback on nfc-tools/libnfc#765, using the _putenv_s approach suggested in maksimtech/libnfc#2. Validate arguments before calling the CRT and preserve void unsetenv. Add hardware-free regression checks for MinGW64 and UCRT64, and limit the existing ACR122 PC/SC power-down change to Windows. Assisted-by: Codex --- .github/workflows/windows-stdlib.yml | 33 +++++++ contrib/win32/stdlib.c | 28 +++--- libnfc/drivers/acr122_pcsc.c | 4 + test/test_win32_stdlib.c | 141 +++++++++++++++++++++++++++ 4 files changed, 193 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/windows-stdlib.yml create mode 100644 test/test_win32_stdlib.c diff --git a/.github/workflows/windows-stdlib.yml b/.github/workflows/windows-stdlib.yml new file mode 100644 index 000000000..19652ce24 --- /dev/null +++ b/.github/workflows/windows-stdlib.yml @@ -0,0 +1,33 @@ +name: Windows environment regression + +on: [push, pull_request] + +permissions: + contents: read + +jobs: + test: + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + include: + - sys: MINGW64 + package: mingw-w64-x86_64-gcc + - sys: UCRT64 + package: mingw-w64-ucrt-x86_64-gcc + steps: + - uses: actions/checkout@v4 + - uses: msys2/setup-msys2@v2 + with: + msystem: ${{ matrix.sys }} + install: ${{ matrix.package }} + - name: Compile Windows compatibility code with warnings as errors + shell: msys2 {0} + run: | + gcc -std=c99 -Wall -Wextra -Werror -I. \ + test/test_win32_stdlib.c contrib/win32/stdlib.c \ + -o test_win32_stdlib.exe + - name: Run without NFC hardware + shell: msys2 {0} + run: ./test_win32_stdlib.exe diff --git a/contrib/win32/stdlib.c b/contrib/win32/stdlib.c index 19b7d691d..4314c629a 100644 --- a/contrib/win32/stdlib.c +++ b/contrib/win32/stdlib.c @@ -34,24 +34,26 @@ // Handle platform specific includes #include "contrib/windows.h" -//There is no setenv()and unsetenv() in windows,but we can use putenv() instead. +#include +#include + +// Use the Windows CRT without a fixed-size name=value buffer. int setenv(const char *name, const char *value, int overwrite) { - char *env = getenv(name); - if ((env && overwrite) || (!env)) { - char str[32]; - strcpy(str, name); - strcat(str, "="); - strcat(str, value); - return putenv(str); + if (!name || !*name || strchr(name, '=') || !value) { + return -1; + } + if (!overwrite && getenv(name)) { + return 0; } - return -1; + return _putenv_s(name, value) == 0 ? 0 : -1; } void unsetenv(const char *name) { - char str[32]; - strcpy(str, name); - strcat(str, "="); - putenv(str); + if (!name || !*name || strchr(name, '=')) { + return; + } + // An empty value removes the variable in the Windows CRT. + _putenv_s(name, ""); } diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index 5c7082a89..7b186900d 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -317,7 +317,11 @@ acr122_pcsc_close(nfc_device *pnd) { pn53x_idle(pnd); +#if defined (_WIN32) SCardDisconnect(DRIVER_DATA(pnd)->hCard, SCARD_UNPOWER_CARD); +#else + SCardDisconnect(DRIVER_DATA(pnd)->hCard, SCARD_LEAVE_CARD); +#endif acr122_pcsc_free_scardcontext(); pn53x_data_free(pnd); diff --git a/test/test_win32_stdlib.c b/test/test_win32_stdlib.c new file mode 100644 index 000000000..7802226b3 --- /dev/null +++ b/test/test_win32_stdlib.c @@ -0,0 +1,141 @@ +/*- + * Regression checks for the Windows setenv()/unsetenv() replacements. + * Licensed under the GNU Lesser General Public License, version 3 or later. + * + * Run on Windows without NFC hardware or libusb: + * gcc -std=c99 -Wall -Wextra -Werror -I. test/test_win32_stdlib.c \ + * contrib/win32/stdlib.c -o test_win32_stdlib.exe + * ./test_win32_stdlib.exe + */ + +#ifndef _WIN32 +#error These tests require the Windows CRT. +#endif + +#include +#include +#include + +int setenv(const char *name, const char *value, int overwrite); +void unsetenv(const char *name); + +static int failures; +static int checks; + +#define CHECK(condition) do { \ + checks++; \ + if (!(condition)) { \ + fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ + failures++; \ + } \ +} while (0) + +static int +value_is(const char *name, const char *expected) +{ + const char *value = getenv(name); + return value && strcmp(value, expected) == 0; +} + +static void +test_overwrite(void) +{ + const char *name = "NFC_ENV_TEST"; + unsetenv(name); + CHECK(setenv(name, "first", 0) == 0); + CHECK(value_is(name, "first")); + CHECK(setenv(name, "second", 0) == 0); + CHECK(value_is(name, "first")); + CHECK(setenv(name, "second", 1) == 0); + CHECK(value_is(name, "second")); + CHECK(setenv(name, "third", -1) == 0); + CHECK(value_is(name, "third")); + unsetenv(name); + CHECK(getenv(name) == NULL); + unsetenv(name); + CHECK(getenv(name) == NULL); +} + +static void +test_long_strings(void) +{ + char name[81]; + char value[1025]; + memset(name, 'N', sizeof(name) - 1); + name[sizeof(name) - 1] = '\0'; + memset(value, 'v', sizeof(value) - 1); + value[sizeof(value) - 1] = '\0'; + + CHECK(setenv(name, "short", 1) == 0); + CHECK(value_is(name, "short")); + unsetenv(name); + CHECK(getenv(name) == NULL); + + CHECK(setenv("NFC_ENV_TEST", value, 1) == 0); + CHECK(value_is("NFC_ENV_TEST", value)); + unsetenv("NFC_ENV_TEST"); + CHECK(getenv("NFC_ENV_TEST") == NULL); + + CHECK(setenv(name, value, 1) == 0); + CHECK(value_is(name, value)); + CHECK(setenv(name, "ignored", 0) == 0); + CHECK(value_is(name, value)); + unsetenv(name); + CHECK(getenv(name) == NULL); +} + +static void +test_argument_copy(void) +{ + char name[] = "NFC_ENV_COPY"; + char value[] = "left=right"; + CHECK(setenv(name, value, 1) == 0); + memset(name, 'x', sizeof(name) - 1); + memset(value, 'y', sizeof(value) - 1); + CHECK(value_is("NFC_ENV_COPY", "left=right")); + unsetenv("NFC_ENV_COPY"); + CHECK(getenv("NFC_ENV_COPY") == NULL); +} + +static void +test_invalid_arguments(void) +{ + CHECK(setenv("NFC_ENV_TEST", "keep", 1) == 0); + CHECK(setenv(NULL, "value", 1) == -1); + CHECK(setenv("", "value", 1) == -1); + CHECK(setenv("NFC_ENV_TEST=bad", "value", 1) == -1); + CHECK(setenv("NFC_ENV_TEST", NULL, 1) == -1); + CHECK(setenv("NFC_ENV_TEST", NULL, 0) == -1); + unsetenv(NULL); + unsetenv(""); + unsetenv("NFC_ENV_TEST=bad"); + CHECK(value_is("NFC_ENV_TEST", "keep")); + unsetenv("NFC_ENV_TEST"); + CHECK(getenv("NFC_ENV_TEST") == NULL); +} + +static void +test_windows_empty_value(void) +{ + // Preserve the Windows CRT convention: an empty value removes the entry. + CHECK(setenv("NFC_ENV_TEST", "keep", 1) == 0); + CHECK(setenv("NFC_ENV_TEST", "", 0) == 0); + CHECK(value_is("NFC_ENV_TEST", "keep")); + CHECK(setenv("NFC_ENV_TEST", "", 1) == 0); + CHECK(getenv("NFC_ENV_TEST") == NULL); +} + +int +main(int argc, char **argv) +{ + test_overwrite(); + // This mode also runs safely against the previous 32-byte implementation. + if (!(argc == 2 && strcmp(argv[1], "--overwrite-only") == 0)) { + test_long_strings(); + test_argument_copy(); + test_invalid_arguments(); + test_windows_empty_value(); + } + printf("%d checks, %d failures\n", checks, failures); + return failures ? EXIT_FAILURE : EXIT_SUCCESS; +} From 0154327bd9360f5f28af15877da729ee650baada Mon Sep 17 00:00:00 2001 From: doomfrawen Date: Thu, 19 Nov 2026 00:05:20 -0700 Subject: [PATCH 3/3] Avoid unrelated Windows socket macro overrides in CRT helpers The environment helpers use only stdlib.h and string.h. Including the broad platform header redefines modern MinGW errno constants and fails the new warnings-as-errors compile check. --- contrib/win32/stdlib.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/contrib/win32/stdlib.c b/contrib/win32/stdlib.c index 4314c629a..6b91980b7 100644 --- a/contrib/win32/stdlib.c +++ b/contrib/win32/stdlib.c @@ -31,9 +31,6 @@ * @brief Windows System compatibility */ -// Handle platform specific includes -#include "contrib/windows.h" - #include #include