Fix bugs in new printf implementation: use buffer size in _snprintf and check for the right failure return value of streamout_char.

svn path=/trunk/; revision=50251
This commit is contained in:
Timo Kreuzer 2011-01-01 13:50:07 +00:00
parent 90ac7fad2f
commit d50bc6057e
2 changed files with 14 additions and 14 deletions

View file

@ -23,7 +23,7 @@ _snprintf(char *buffer, size_t count, const char *format, ...)
stream._base = buffer; stream._base = buffer;
stream._ptr = stream._base; stream._ptr = stream._base;
stream._charbuf = 0; stream._charbuf = 0;
stream._bufsiz = (unsigned long)-1; stream._bufsiz = count;
stream._cnt = stream._bufsiz; stream._cnt = stream._bufsiz;
stream._flag = 0; stream._flag = 0;
stream._tmpfname = 0; stream._tmpfname = 0;
@ -31,7 +31,7 @@ _snprintf(char *buffer, size_t count, const char *format, ...)
va_start(argptr, format); va_start(argptr, format);
result = streamout(&stream, format, argptr); result = streamout(&stream, format, argptr);
va_end(argptr); va_end(argptr);
*stream._ptr = '\0'; *stream._ptr = '\0';
return result; return result;
} }

View file

@ -109,7 +109,7 @@ format_float(
digits = digits_u; digits = digits_u;
case _T('g'): case _T('g'):
if (exponent < -4 || exponent >= precision) goto case_e; if (exponent < -4 || exponent >= precision) goto case_e;
break; break;
case _T('E'): case _T('E'):
digits = digits_u; digits = digits_u;
@ -118,7 +118,7 @@ format_float(
fpval /= pow(10., exponent); fpval /= pow(10., exponent);
val32 = exponent >= 0 ? exponent : -exponent; val32 = exponent >= 0 ? exponent : -exponent;
// FIXME: handle length of exponent field: // FIXME: handle length of exponent field:
// http://msdn.microsoft.com/de-de/library/0fatw238%28VS.80%29.aspx // http://msdn.microsoft.com/de-de/library/0fatw238%28VS.80%29.aspx
num_digits = 3; num_digits = 3;
while (num_digits--) while (num_digits--)
@ -175,7 +175,7 @@ format_float(
{ {
fpval *= pow(10., precision); fpval *= pow(10., precision);
val64 = (__int64)(fpval + 0.5); val64 = (__int64)(fpval + 0.5);
while (num_digits-- > 0) while (num_digits-- > 0)
{ {
*--(*string) = digits[val64 % 10]; *--(*string) = digits[val64 % 10];
@ -297,11 +297,11 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
if (chr == _T('\0')) break; if (chr == _T('\0')) break;
/* Check for 'normal' character or double % */ /* Check for 'normal' character or double % */
if ((chr != _T('%')) || if ((chr != _T('%')) ||
(chr = *format++) == _T('%')) (chr = *format++) == _T('%'))
{ {
/* Write the character to the stream */ /* Write the character to the stream */
if ((written = streamout_char(stream, chr)) == -1) return -1; if ((written = streamout_char(stream, chr)) == 0) return -1;
written_all += written; written_all += written;
continue; continue;
} }
@ -344,13 +344,13 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
if (chr == '.') if (chr == '.')
{ {
chr = *format++; chr = *format++;
if (chr == _T('*')) if (chr == _T('*'))
{ {
precision = va_arg(argptr, int); precision = va_arg(argptr, int);
chr = *format++; chr = *format++;
} }
else else
{ {
precision = 0; precision = 0;
while (chr >= _T('0') && chr <= _T('9')) while (chr >= _T('0') && chr <= _T('9'))
@ -578,7 +578,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
{ {
for (; padding > 0; padding--) for (; padding > 0; padding--)
{ {
if ((written = streamout_char(stream, _T(' '))) == -1) return -2; if ((written = streamout_char(stream, _T(' '))) == 0) return -1;
written_all += written; written_all += written;
} }
} }
@ -595,7 +595,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
if ((flags & FLAG_ALIGN_LEFT) == 0) precision += padding; if ((flags & FLAG_ALIGN_LEFT) == 0) precision += padding;
while (precision-- > 0) while (precision-- > 0)
{ {
if ((written = streamout_char(stream, _T('0'))) == -1) return -4; if ((written = streamout_char(stream, _T('0'))) == 0) return -1;
written_all += written; written_all += written;
} }
@ -611,7 +611,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
/* Optional right '0' padding */ /* Optional right '0' padding */
while (precision-- > 0) while (precision-- > 0)
{ {
if ((written = streamout_char(stream, _T('0'))) == -1) return -6; if ((written = streamout_char(stream, _T('0'))) == 0) return -1;
written_all += written; written_all += written;
len++; len++;
} }
@ -622,11 +622,11 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
{ {
while (padding-- > 0) while (padding-- > 0)
{ {
if ((written = streamout_char(stream, _T(' '))) == -1) return -7; if ((written = streamout_char(stream, _T(' '))) == 0) return -1;
written_all += written; written_all += written;
} }
} }
} }
if (written == -1) return -8; if (written == -1) return -8;