diff --git a/mongoose.c b/mongoose.c index 7f2ef6ee9882fc428b54f1d3cfdd185cc1c08efb..02d91336856f21576135b6addd5e1592fd5775dd 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1936,12 +1936,24 @@ int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) { *buf = NULL; /* LCOV_EXCL_START */ while (len < 0) { MG_FREE(*buf); + if (size == 0) { + size = 5; + } size *= 2; - if ((*buf = (char *) MG_MALLOC(size)) == NULL) break; + if ((*buf = (char *) MG_MALLOC(size)) == NULL) { + len = -1; + break; + } va_copy(ap_copy, ap); - len = vsnprintf(*buf, size, fmt, ap_copy); + len = vsnprintf(*buf, size - 1, fmt, ap_copy); va_end(ap_copy); } + + /* + * Microsoft version of vsnprintf() is not always null-terminated, so put + * the terminator manually + */ + (*buf)[len] = 0; /* LCOV_EXCL_STOP */ } else if (len >= (int) size) { /* Standard-compliant code path. Allocate a buffer that is large enough. */ diff --git a/mongoose.h b/mongoose.h index 3b56e263e296163911a2951b6007b4e151deee45..6b8b80262c6cd59ab3ef18163df6adaaebedfea5 100644 --- a/mongoose.h +++ b/mongoose.h @@ -210,6 +210,12 @@ #include <windows.h> #include <process.h> +#if _MSC_VER < 1700 +typedef int bool; +#else +#include <stdbool.h> +#endif + #if defined(_MSC_VER) && _MSC_VER >= 1800 #define strdup _strdup #endif @@ -323,9 +329,16 @@ typedef struct _stati64 cs_stat_t; #define MG_NET_IF MG_NET_IF_SOCKET #endif -int rmdir(const char *dirname); unsigned int sleep(unsigned int seconds); +/* https://stackoverflow.com/questions/16647819/timegm-cross-platform */ +#define timegm _mkgmtime + +#define gmtime_r(a, b) \ + do { \ + *(b) = *gmtime(a); \ + } while (0) + #endif /* CS_PLATFORM == CS_P_WINDOWS */ #endif /* CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_ */ #ifdef MG_MODULE_LINES @@ -374,6 +387,7 @@ unsigned int sleep(unsigned int seconds); #include <pthread.h> #include <signal.h> #include <stdarg.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -493,6 +507,7 @@ typedef struct stat cs_stat_t; #include <fcntl.h> #include <inttypes.h> #include <machine/endian.h> +#include <stdbool.h> #include <stdint.h> #include <string.h> #include <sys/stat.h> @@ -536,6 +551,7 @@ typedef struct stat cs_stat_t; #include <fcntl.h> #include <inttypes.h> #include <machine/endian.h> +#include <stdbool.h> #include <string.h> #include <sys/stat.h> #include <sys/time.h> @@ -642,6 +658,7 @@ int inet_pton(int af, const char *src, void *dst); #include <ctype.h> #include <errno.h> #include <inttypes.h> +#include <stdbool.h> #include <stdint.h> #include <string.h> #include <time.h>