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 5a1bd5604..6b91980b7 100644 --- a/contrib/win32/stdlib.c +++ b/contrib/win32/stdlib.c @@ -31,27 +31,26 @@ * @brief Windows System compatibility */ -// Handle platform specific includes -#include "contrib/windows.h" +#include +#include -//There is no setenv()and unsetenv() in windows,but we can use putenv() instead. +// 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; } - return -1; + if (!overwrite && getenv(name)) { + return 0; + } + 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 f5cc02e25..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; +}