Skip to content

Fix Windows/MinGW build: missing err.h, uninitialized pointer in stdlib.c - #29

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

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

Conversation

@doomfrawen

@doomfrawen doomfrawen commented Aug 9, 2026 •

Copy link
Copy Markdown

I ran into missing err.h support and invalid writes in the Windows environment helpers while building for native Windows with MSYS2 MinGW64.

  • I removed the unused/redundant err.h includes and added a Windows warnx() shim for the diagnostic macros.
  • I replaced the char *str[32] implementation of setenv() and unsetenv() with _putenv_s, removing the fixed-size intermediate buffer as well. 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 added Windows regression checks and CI for MinGW64 and UCRT64. These checks don't require NFC hardware.

For the environment helpers, I followed the feedback on nfc-tools/libnfc#765 and the approach in maksimtech/libnfc#2.

Validation at 25b312a:

  • 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.
  • In the separate Docker build matrix, I found Ubuntu archive connection failures in the Linux/386 jobs during dependency installation, before source compilation.

The tests I've added cover the Windows environment helpers, not a full application build. I haven't tested NFC hardware or the VS/clang-cl build for this update. The upstream workflows still need maintainer approval.

…ib.c

- src/nfc-utils.h/.c and src/windows.h include <err.h>, a BSD/glibc-only
  header not shipped by MinGW-w64, so the GNU-toolchain build (autoreconf
  + gcc, as opposed to the VS2019/clang-cl path this repo also supports)
  fails with 'err.h: No such file or directory'. windows.h doesn't call
  anything from it, so that include is simply dropped. nfc-utils.h's
  DBG/WARN/ERR macros do use warnx(), so on _WIN32 this adds a small
  static inline warnx() shim instead of the header, and drops the
  redundant second #include <err.h> in nfc-utils.c.

- src/stdlib.c's setenv()/unsetenv() declare 'char *str[32]' (an array
  of 32 uninitialized pointers) and then do strcpy(*str, name) etc,
  which dereferences the first (uninitialized/garbage) pointer in that
  array and writes through it - undefined behavior, not just a
  compile-time type mismatch. Changed to 'char str[32]' (an actual
  32-byte buffer) and dropped the erroneous dereferences.
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