Skip to content

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

Open
doomfrawen wants to merge 1 commit into
nfc-tools:masterfrom
doomfrawen:windows-mingw-fixes
Open

Fix MinGW build bug and ACR122U PC/SC reconnect issue on Windows#765
doomfrawen wants to merge 1 commit into
nfc-tools:masterfrom
doomfrawen:windows-mingw-fixes

Conversation

@doomfrawen

Copy link
Copy Markdown

Two small, unrelated fixes found while building libnfc for native Windows via MSYS2 MinGW64 with -DLIBNFC_DRIVER_ACR122_PCSC=ON:

1. contrib/win32/stdlib.csetenv()/unsetenv() declare char *str[32] (an array of 32 uninitialized pointers) instead of char str[32] (a buffer), then strcpy/strcat into it. Older GCC only warned about the implicit pointer conversion; GCC 14+ rejects it outright, and even where it does compile this is a write through an uninitialized pointer.

2. libnfc/drivers/acr122_pcsc.cacr122_pcsc_close() calls SCardDisconnect(..., SCARD_LEAVE_CARD). On an ACR122U behind Windows' inbox CCID driver (WUDFUsbccidDriver), this leaves the reader's internal PC/SC session in a state where the next SCardConnect from any process fails with No ACR122 firmware received, Error: 00000001 until the device is physically unplugged and replugged. This reproduced 100% of the time across many runs (nfc-list, mfoc, mfcuk, mfoc-hardnested) on a genuine ACS ACR122U. Switching to SCARD_UNPOWER_CARD forces a full depower/reset of the card interface on close — electrically similar to a replug — and the reader is immediately reusable by the next process with no manual intervention.

Both changes are minimal and platform-scoped (Windows/MinGW only). Happy to adjust style/commit message if you'd like it split into two PRs instead.

- 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.
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.

1 participant