Skip to content

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

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

Fix Windows/MinGW build: missing err.h, uninitialized pointer in stdlib.c#29
doomfrawen wants to merge 1 commit into
nfc-tools:masterfrom
doomfrawen:windows-mingw-fixes

Conversation

@doomfrawen

Copy link
Copy Markdown

Building mfoc-hardnested for native Windows via the GNU toolchain path (autoreconf -vis && ./configure && make, MSYS2 MinGW64) rather than the VS2019/clang-cl path hits two issues:

1. src/nfc-utils.h/.c, src/windows.h — include <err.h>, a BSD/glibc-only header not shipped by MinGW-w64, so the build 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 now-redundant second #include <err.h> in nfc-utils.c.

2. src/stdlib.csetenv()/unsetenv() declare char *str[32] (an array of 32 uninitialized pointers) and then do strcpy(*str, name) etc, which dereferences the first garbage pointer in that array and writes through it. This is undefined behavior / a likely crash, not just a compile-time type mismatch (worth noting: this is a different, worse variant of a bug also present in upstream libnfc's contrib/win32/stdlib.c, which just declares the array without dereferencing it and so only fails to compile rather than corrupting memory). Changed to char str[32] (an actual 32-byte stack buffer) and removed the erroneous * dereferences.

Both fixes are additive/platform-scoped and don't touch the existing VS/clang-cl build path or non-Windows behavior.

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