mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-02 22:46:30 +02:00
Fixed a possible W32 bufferoverflow
This commit is contained in:
parent
44d09ca58a
commit
e38f5c8326
14 changed files with 240 additions and 34 deletions
|
@ -1,5 +1,5 @@
|
|||
/* strgutil.c - string utilities
|
||||
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
||||
* Copyright (C) 1994, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
||||
*
|
||||
* This file is part of GnuPG.
|
||||
*
|
||||
|
@ -638,3 +638,100 @@ memicmp( const char *a, const char *b, size_t n )
|
|||
#endif
|
||||
|
||||
|
||||
#ifdef __MINGW32__
|
||||
/*
|
||||
* Like vsprintf but provides a pointer to malloc'd storage, which
|
||||
* must be freed by the caller (m_free). Taken from libiberty as
|
||||
* found in gcc-2.95.2 and a little bit modernized.
|
||||
* FIXME: Write a new CRT for W32.
|
||||
*/
|
||||
int
|
||||
vasprintf ( char **result, const char *format, va_list args)
|
||||
{
|
||||
const char *p = format;
|
||||
/* Add one to make sure that it is never zero, which might cause malloc
|
||||
to return NULL. */
|
||||
int total_width = strlen (format) + 1;
|
||||
va_list ap;
|
||||
|
||||
/* this is not really portable but works under Windows */
|
||||
memcpy ( &ap, &args, sizeof (va_list));
|
||||
|
||||
while (*p != '\0')
|
||||
{
|
||||
if (*p++ == '%')
|
||||
{
|
||||
while (strchr ("-+ #0", *p))
|
||||
++p;
|
||||
if (*p == '*')
|
||||
{
|
||||
++p;
|
||||
total_width += abs (va_arg (ap, int));
|
||||
}
|
||||
else
|
||||
{
|
||||
char *endp;
|
||||
total_width += strtoul (p, &endp, 10);
|
||||
p = endp;
|
||||
}
|
||||
if (*p == '.')
|
||||
{
|
||||
++p;
|
||||
if (*p == '*')
|
||||
{
|
||||
++p;
|
||||
total_width += abs (va_arg (ap, int));
|
||||
}
|
||||
else
|
||||
{
|
||||
char *endp;
|
||||
total_width += strtoul (p, &endp, 10);
|
||||
p = endp;
|
||||
}
|
||||
}
|
||||
while (strchr ("hlL", *p))
|
||||
++p;
|
||||
/* Should be big enough for any format specifier except %s
|
||||
and floats. */
|
||||
total_width += 30;
|
||||
switch (*p)
|
||||
{
|
||||
case 'd':
|
||||
case 'i':
|
||||
case 'o':
|
||||
case 'u':
|
||||
case 'x':
|
||||
case 'X':
|
||||
case 'c':
|
||||
(void) va_arg (ap, int);
|
||||
break;
|
||||
case 'f':
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'g':
|
||||
case 'G':
|
||||
(void) va_arg (ap, double);
|
||||
/* Since an ieee double can have an exponent of 307, we'll
|
||||
make the buffer wide enough to cover the gross case. */
|
||||
total_width += 307;
|
||||
|
||||
case 's':
|
||||
total_width += strlen (va_arg (ap, char *));
|
||||
break;
|
||||
case 'p':
|
||||
case 'n':
|
||||
(void) va_arg (ap, char *);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*result = m_alloc (total_width);
|
||||
if (*result != NULL)
|
||||
return vsprintf (*result, format, args);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /*__MINGW32__*/
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue