From c9df829f07533c26c41c2faebc0465a788b08aef Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Thu, 13 Mar 2008 13:18:45 +0100 Subject: [PATCH] Use the available vsnprintf replacement instead of rolling our own. But we still have to cater for the strangeness that on Windows the size parameter is the number of characters to write, not the size of the buffer. Signed-off-by: Johannes Sixt --- Makefile | 2 ++ compat/mingw.c | 34 ---------------------------------- compat/mingw.h | 3 --- compat/snprintf.c | 13 +++++++++++-- 4 files changed, 13 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 9023eff0bb..ea59a15989 100644 --- a/Makefile +++ b/Makefile @@ -550,10 +550,12 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_C99_FORMAT = YesPlease NO_STRTOUMAX = YesPlease NO_MKDTEMP = YesPlease + SNPRINTF_RETURNS_BOGUS = YesPlease NO_SVN_TESTS = YesPlease NO_PERL_MAKEMAKER = YesPlease NO_POSIX_ONLY_PROGRAMS = YesPlease COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat + COMPAT_CFLAGS += -DSNPRINTF_SIZE_CORR=1 COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" COMPAT_OBJS += compat/mingw.o compat/fnmatch.o compat/regex.o EXTLIBS += -lws2_32 diff --git a/compat/mingw.c b/compat/mingw.c index 6733727380..7c8fd0e158 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -847,40 +847,6 @@ int mingw_rename(const char *pold, const char *pnew) return -1; } -#undef vsnprintf -/* Note that the size parameter specifies the available space, i.e. - * includes the trailing NUL byte; but Windows's vsnprintf expects the - * number of characters to write without the trailing NUL. - */ - -/* This is out of line because it uses alloca() behind the scenes, - * which must not be called in a loop (alloca() reclaims the allocations - * only at function exit). - */ -static int try_vsnprintf(size_t size, const char *fmt, va_list args) -{ - char buf[size]; /* gcc-ism */ - return vsnprintf(buf, size-1, fmt, args); -} - -int mingw_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) -{ - int len; - if (size > 0) { - len = vsnprintf(buf, size-1, fmt, args); - if (len >= 0) - return len; - } - /* ouch, buffer too small; need to compute the size */ - if (size < 250) - size = 250; - do { - size *= 4; - len = try_vsnprintf(size, fmt, args); - } while (len < 0); - return len; -} - struct passwd *getpwuid(int uid) { static char user_name[100]; diff --git a/compat/mingw.h b/compat/mingw.h index abac3f2caf..c7db345d58 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -174,9 +174,6 @@ int mingw_fstat(int fd, struct mingw_stat *buf); static inline int mingw_stat(const char *file_name, struct mingw_stat *buf) { return mingw_lstat(file_name, buf); } -int mingw_vsnprintf(char *buf, size_t size, const char *fmt, va_list args); -#define vsnprintf mingw_vsnprintf - pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env); void mingw_execvp(const char *cmd, char *const *argv); #define execvp mingw_execvp diff --git a/compat/snprintf.c b/compat/snprintf.c index dbfc2d6b6e..480b66f94e 100644 --- a/compat/snprintf.c +++ b/compat/snprintf.c @@ -1,12 +1,21 @@ #include "../git-compat-util.h" +/* + * The size parameter specifies the available space, i.e. includes + * the trailing NUL byte; but Windows's vsnprintf expects the + * number of characters to write without the trailing NUL. + */ +#ifndef SNPRINTF_SIZE_CORR +#define SNPRINTF_SIZE_CORR 0 +#endif + #undef vsnprintf int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap) { char *s; int ret; - ret = vsnprintf(str, maxsize, format, ap); + ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap); if (ret != -1) return ret; @@ -20,7 +29,7 @@ int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap) if (! str) break; s = str; - ret = vsnprintf(str, maxsize, format, ap); + ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap); } free(s); return ret; -- 2.11.4.GIT