mirror of
https://github.com/git/git.git
synced 2026-03-13 10:23:30 +01:00
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 <johannes.sixt@telecom.at>
This commit is contained in:
2
Makefile
2
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
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user