Skip to content

fix(win32): setenv()/unsetenv() do not compile and can overflow - #2

Merged
maksimtech merged 3 commits into
masterfrom
fix/win32-setenv
Oct 22, 2026
Merged

maksimtech merged 3 commits into
masterfrom
fix/win32-setenv

Conversation

@maksimtech

Copy link
Copy Markdown
Owner

Fixes the MinGW build failure that the new Windows CI job exposed. Related upstream PR: nfc-tools#765 (by @doomfrawen), whose contrib/win32/stdlib.c change this builds on.

Problem

contrib/win32/stdlib.c declares char *str[32] (an array of pointers) and passes it to strcpy/putenv: an error with current GCC (-Wincompatible-pointer-types, fatal since GCC 14). Even with the type fixed, a 32-byte buffer overflows for longer name+value, and setenv(name, value, 0) on an existing variable returned -1 where POSIX returns 0.

Change

Use _putenv_s (copies its arguments), return POSIX results, include <string.h>/<stdlib.h> explicitly.

Not included from nfc-tools#765: the SCardDisconnect(..., SCARD_UNPOWER_CARD) change in acr122_pcsc.c. It is a behaviour change on real ACR122U hardware that I cannot test, so it stays a separate question.

TDD

First commit adds a Windows-only test and makes the Windows job strict: it must be red. Second commit is the fix: it must be green.

🤖 Generated with Claude Code

maksimtech and others added 3 commits October 22, 2026 17:57
Windows lacks setenv() and unsetenv(); contrib/win32/stdlib.c provides them
and does not compile with current GCC (char *str[32] passed to strcpy,
nfc-tools#765). Add a Windows-only test that builds that file and
checks set/unset, the overwrite flag and a long name and value. The Windows
CI job no longer ignores failures.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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>
pn53x-diagnose, pn53x-sam and pn53x-tamashell use libnfc internals that the
DLL does not export (#3).

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

Copy link
Copy Markdown

@maksimtech
maksimtech merged commit 344c95a into master Oct 22, 2026
10 of 11 checks passed
@maksimtech
maksimtech deleted the fix/win32-setenv branch October 22, 2026 16:23
doomfrawen added a commit to doomfrawen/libnfc that referenced this pull request Nov 19, 2026
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
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