diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index da03e40a7..5039ed236 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -86,9 +86,6 @@ jobs: windows-mingw: name: Windows MinGW CMake runs-on: windows-latest - # Known failure: contrib/win32/stdlib.c declares `char *str[32]` and passes it - # to strcpy (nfc-tools/libnfc#765). Remove this line once that is fixed. - continue-on-error: true defaults: run: shell: msys2 {0} @@ -111,7 +108,9 @@ jobs: cp /mingw64/lib/libusb0.dll.a build/LibUSB-Win32/lib/gcc/libusb.dll.a - name: Configure and build run: | - cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_SOFTWARE_TESTS=ON + # Examples are off: pn53x-diagnose, pn53x-sam and pn53x-tamashell do not link + # on Windows (maksimtech/libnfc#3) + cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_EXAMPLES=OFF -DBUILD_SOFTWARE_TESTS=ON cmake --build build - name: Run tests run: ctest --test-dir build --no-tests=error --output-on-failure diff --git a/contrib/win32/stdlib.c b/contrib/win32/stdlib.c index 5a1bd5604..2c82d73e0 100644 --- a/contrib/win32/stdlib.c +++ b/contrib/win32/stdlib.c @@ -34,24 +34,28 @@ // Handle platform specific includes #include "contrib/windows.h" -//There is no setenv()and unsetenv() in windows,but we can use putenv() instead. +#include +#include + +// There is no setenv() and unsetenv() in Windows, but _putenv_s() is there: it +// copies its arguments, so there is no fixed-size buffer to overflow. 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; + } + // POSIX: an existing variable is left alone, and that is not an error + 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 from the environment + _putenv_s(name, ""); } diff --git a/test/software/CMakeLists.txt b/test/software/CMakeLists.txt index bc3761c36..aac9ddab7 100644 --- a/test/software/CMakeLists.txt +++ b/test/software/CMakeLists.txt @@ -13,3 +13,9 @@ ADD_TEST(NAME no_hardware COMMAND test_no_hardware) # on the readers plugged into the machine running the tests. SET_TESTS_PROPERTIES(no_hardware PROPERTIES ENVIRONMENT "LIBNFC_AUTO_SCAN=false;LIBNFC_INTRUSIVE_SCAN=false;LIBNFC_LOG_LEVEL=0") + +IF(WIN32) + # setenv()/unsetenv() replacements that Windows lacks + ADD_EXECUTABLE(test_win32_stdlib test_win32_stdlib.c ${CMAKE_SOURCE_DIR}/contrib/win32/stdlib.c) + ADD_TEST(NAME win32_stdlib COMMAND test_win32_stdlib) +ENDIF(WIN32) diff --git a/test/software/test_win32_stdlib.c b/test/software/test_win32_stdlib.c new file mode 100644 index 000000000..e605fd089 --- /dev/null +++ b/test/software/test_win32_stdlib.c @@ -0,0 +1,91 @@ +/*- + * Free/Libre Near Field Communication (NFC) library + * + * Windows only: tests of the setenv()/unsetenv() replacements in + * contrib/win32/stdlib.c, which Windows lacks. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + */ + +#include +#include +#include + +#include "contrib/windows.h" + +static int failures = 0; + +#define CHECK(cond) \ + do { \ + if (!(cond)) { \ + fprintf(stderr, "%s:%d: check failed: %s\n", __FILE__, __LINE__, #cond); \ + failures++; \ + } \ + } while (0) + +static int +value_is(const char *name, const char *expected) +{ + const char *value = getenv(name); + return value != NULL && strcmp(value, expected) == 0; +} + +static void +test_set_and_unset(void) +{ + CHECK(setenv("LIBNFC_TEST_VAR", "first", 1) == 0); + CHECK(value_is("LIBNFC_TEST_VAR", "first")); + + unsetenv("LIBNFC_TEST_VAR"); + CHECK(getenv("LIBNFC_TEST_VAR") == NULL); +} + +/* POSIX: with overwrite == 0 an existing variable is left alone and the call succeeds. */ +static void +test_overwrite_flag(void) +{ + CHECK(setenv("LIBNFC_TEST_VAR", "first", 1) == 0); + + CHECK(setenv("LIBNFC_TEST_VAR", "second", 0) == 0); + CHECK(value_is("LIBNFC_TEST_VAR", "first")); + + CHECK(setenv("LIBNFC_TEST_VAR", "second", 1) == 0); + CHECK(value_is("LIBNFC_TEST_VAR", "second")); + + unsetenv("LIBNFC_TEST_VAR"); +} + +/* The names and values libnfc itself sets are short, but the function must not + * assume that: a 40-character name and a 200-character value. */ +static void +test_long_name_and_value(void) +{ + const char *name = "LIBNFC_TEST_A_VARIABLE_WITH_A_LONG_NAME"; + char value[201]; + memset(value, 'x', sizeof(value) - 1); + value[sizeof(value) - 1] = '\0'; + + CHECK(setenv(name, value, 1) == 0); + CHECK(value_is(name, value)); + + unsetenv(name); + CHECK(getenv(name) == NULL); +} + +int +main(void) +{ + test_set_and_unset(); + test_overwrite_flag(); + test_long_name_and_value(); + + if (failures) { + fprintf(stderr, "%d check(s) failed\n", failures); + return 1; + } + printf("all checks passed\n"); + return 0; +}