Skip to content

Fix MinGW build bug and ACR122U PC/SC reconnect issue on Windows - #765

Open
doomfrawen wants to merge 3 commits into
nfc-tools:masterfrom
doomfrawen:windows-mingw-fixes
Open

doomfrawen wants to merge 3 commits into
nfc-tools:masterfrom
doomfrawen:windows-mingw-fixes

Conversation

@doomfrawen

@doomfrawen doomfrawen commented Aug 9, 2026 •

Copy link
Copy Markdown

I've updated the Windows environment helpers and limited the ACR122U power-down change to Windows.

  • I replaced the pointer-array/fixed-size-buffer implementation of setenv() and unsetenv() with _putenv_s, following Maksim's feedback and the approach in maksimtech/libnfc#2. An existing variable with overwrite=0 now returns success without changing its value. I also added checks for invalid names and null values before calling the CRT, while keeping the existing void unsetenv() signature and Windows empty-value removal behavior.
  • I limited SCARD_UNPOWER_CARD on close to _WIN32. Other platforms still use SCARD_LEAVE_CARD.
  • I added Windows regression checks and CI for MinGW64 and UCRT64. These checks don't require NFC hardware.

For the reader issue, I was using a genuine ACS ACR122U on Windows 10 with the inbox CCID driver (WUDFUsbccidDriver). I built libnfc through MSYS2 MinGW64 with -DLIBNFC_DRIVER_ACR122_PCSC=ON. On subsequent opens, I got No ACR122 firmware received, Error: 00000001 and had to unplug and reconnect the reader before I could use it again. Powering down the card on close allowed subsequent runs. The error comes from the firmware request, so I can't conclude from that alone that SCardConnect failed. Microsoft documents SCARD_UNPOWER_CARD as powering down the card; that isn't the same as unplugging the USB reader.

Validation at 0154327:

  • Windows CI on my fork passed 37 checks on each of MinGW64 and UCRT64, compiling the actual compatibility source with -Wall -Wextra -Werror.
  • I covered long names/values, overwrite behavior, argument copying, invalid inputs, removal, and the Windows empty-value convention.
  • I also ran git diff --check, which passed.

I haven't retested the reader for this update, and I don't think I still have the original before/after logs. I haven't included the exact Windows build or driver/firmware versions. The tests I've added cover the Windows environment helpers, not the full library or NFC hardware. The upstream workflow still needs maintainer approval.

- 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.
maksimtech added a commit to maksimtech/libnfc that referenced this pull request Oct 22, 2026
The Windows job reaches the compiler and fails in contrib/win32/stdlib.c
(char *str[32] passed to strcpy, nfc-tools#765). Keep it visible but
non-blocking until that fix is merged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
maksimtech added a commit to maksimtech/libnfc that referenced this pull request Oct 22, 2026
contrib/win32/stdlib.c declared char *str[32], an array of pointers, and
passed it to strcpy/putenv: an error with current GCC, and a 32-byte buffer
could overflow with a longer name and value. Use _putenv_s, which copies its
arguments, and return what POSIX specifies when overwrite is 0 and the
variable already exists. Include <string.h> and <stdlib.h> explicitly.

Refs nfc-tools#765

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@maksimtech

Copy link
Copy Markdown

Tested this on a clean Windows runner (MSYS2 MinGW64, GCC 16.2), and the contrib/win32/stdlib.c part is confirmed:

  • on current master the build fails with error: passing argument 1 of 'strcpy' from incompatible pointer type [-Wincompatible-pointer-types] (char *str[32]);
  • with only the type fixed, the 32-byte buffer is still too small for a longer name+value, and setenv(name, value, 0) on an existing variable returns -1 where POSIX returns 0.

I fixed it with _putenv_s (copies its arguments) and added a Windows-only test (test/software/test_win32_stdlib.c: set/unset, the overwrite flag, a 40-char name and 200-char value). Red before, green after, on the real Windows CI: maksimtech#2

I did not cover the SCardDisconnect(..., SCARD_UNPOWER_CARD) change in acr122_pcsc.c: it changes behaviour with a real ACR122U on Windows and I have no way to test that here. If you can describe the failure it fixes (reader model, OS, log), that part can be looked at on its own.

(Test runs done with Claude Code assistance; I reviewed the results.)

Address the overflow and overwrite=0 feedback on nfc-tools#765,
using the _putenv_s approach suggested in maksimtech#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
doomfrawen added a commit to doomfrawen/mfoc-hardnested that referenced this pull request Nov 19, 2026
Apply the _putenv_s approach discussed in nfc-tools/libnfc#765 and
maksimtech/libnfc#2 to the same compatibility helpers here.
Return success when overwrite=0 preserves an existing value and reject
invalid arguments before calling the CRT.

Add hardware-free Windows regression checks for MinGW64 and UCRT64.

Assisted-by: Codex
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants