From 1afd22ef2289241f27e291d9fe6709a46b859caa Mon Sep 17 00:00:00 2001 From: doomfrawen Date: Sun, 9 Aug 2026 09:24:59 -0700 Subject: [PATCH] Fix Windows/MinGW build: missing err.h, uninitialized pointer in stdlib.c - src/nfc-utils.h/.c and src/windows.h include , 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 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. --- src/nfc-utils.c | 1 - src/nfc-utils.h | 15 ++++++++++++++- src/stdlib.c | 18 +++++++++--------- src/windows.h | 1 - 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/nfc-utils.c b/src/nfc-utils.c index 5c0a264..f5b13b1 100644 --- a/src/nfc-utils.c +++ b/src/nfc-utils.c @@ -37,7 +37,6 @@ * @brief Provide some examples shared functions like print, parity calculation, options parsing. */ #include -#include #include "nfc-utils.h" diff --git a/src/nfc-utils.h b/src/nfc-utils.h index a0a599e..b46b019 100644 --- a/src/nfc-utils.h +++ b/src/nfc-utils.h @@ -43,7 +43,20 @@ # include # include -# include +# ifdef _WIN32 +# include +# include +static inline void warnx(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); +} +# else +# include +# endif # include /** diff --git a/src/stdlib.c b/src/stdlib.c index 9a0a512..0dfa02f 100644 --- a/src/stdlib.c +++ b/src/stdlib.c @@ -40,19 +40,19 @@ int setenv(const char *name, const char *value, int overwrite) { char *env = getenv(name); if ((env && overwrite) || (!env)) { - char *str[32]; - strcpy(*str, name); - strcat(*str, "="); - strcat(*str, value); - return _putenv(*str); + char str[32]; + strcpy(str, name); + strcat(str, "="); + strcat(str, value); + return _putenv(str); } return -1; } void unsetenv(const char *name) { - char *str[32]; - strcpy(*str, name); - strcat(*str, "="); - _putenv(*str); + char str[32]; + strcpy(str, name); + strcat(str, "="); + _putenv(str); } diff --git a/src/windows.h b/src/windows.h index 2a9e722..134ca99 100644 --- a/src/windows.h +++ b/src/windows.h @@ -35,7 +35,6 @@ # include # include -# include # if defined (__MINGW32__) /* * Cheating here on the snprintf to incorporate the format argument