From fd082901f1aff03ce8221a2bad58b70bf2d95fd9 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 5 Aug 2013 13:26:11 +0200 Subject: [PATCH] test: add windows-only snprintf() function Should fix the build after 96f32a2 inadvertently broke it. There is no snprintf() on Windows because, hey, it's a C99 addition and the people from Redmond, WA are still firmly stuck in 1989. --- test/task.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/task.h b/test/task.h index 6f96a4ff0..e050dca0c 100644 --- a/test/task.h +++ b/test/task.h @@ -153,4 +153,32 @@ enum test_status { return TEST_SKIP; \ } while (0) +#ifdef _WIN32 + +#include + +/* Emulate snprintf() on Windows, _snprintf() doesn't zero-terminate the buffer + * on overflow... + */ +static int snprintf(char* buf, size_t len, const char* fmt, ...) { + va_list ap; + int n; + + va_start(ap, fmt); + n = _vsprintf_p(buf, len, fmt, ap); + va_end(ap); + + /* It's a sad fact of life that no one ever checks the return value of + * snprintf(). Zero-terminating the buffer hopefully reduces the risk + * of gaping security holes. + */ + if (n < 0) + if (len > 0) + buf[0] = '\0'; + + return n; +} + +#endif + #endif /* TASK_H_ */