Commit e5c492d7 authored by Barry Warsaw's avatar Barry Warsaw

formatfloat(), formatint(): Conversion of sprintf() to PyOS_snprintf()

for buffer overrun avoidance.
parent 312af42b
...@@ -5080,7 +5080,8 @@ formatfloat(Py_UNICODE *buf, ...@@ -5080,7 +5080,8 @@ formatfloat(Py_UNICODE *buf,
prec = 6; prec = 6;
if (type == 'f' && (fabs(x) / 1e25) >= 1e25) if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
type = 'g'; type = 'g';
sprintf(fmt, "%%%s.%d%c", (flags & F_ALT) ? "#" : "", prec, type); PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c",
(flags & F_ALT) ? "#" : "", prec, type);
/* worst case length calc to ensure no buffer overrun: /* worst case length calc to ensure no buffer overrun:
fmt = %#.<prec>g fmt = %#.<prec>g
buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
...@@ -5151,15 +5152,16 @@ formatint(Py_UNICODE *buf, ...@@ -5151,15 +5152,16 @@ formatint(Py_UNICODE *buf,
*/ */
if (x == 0 && (flags & F_ALT) && (type == 'x' || type == 'X')) { if (x == 0 && (flags & F_ALT) && (type == 'x' || type == 'X')) {
/* Only way to know what the platform does is to try it. */ /* Only way to know what the platform does is to try it. */
sprintf(fmt, type == 'x' ? "%#x" : "%#X", 0); PyOS_snprintf(fmt, sizeof(fmt), type == 'x' ? "%#x" : "%#X", 0);
if (fmt[1] != (char)type) { if (fmt[1] != (char)type) {
/* Supply our own leading 0x/0X -- needed under std C */ /* Supply our own leading 0x/0X -- needed under std C */
use_native_c_format = 0; use_native_c_format = 0;
sprintf(fmt, "0%c%%#.%dl%c", type, prec, type); PyOS_snprintf(fmt, sizeof(fmt), "0%c%%#.%dl%c", type, prec, type);
} }
} }
if (use_native_c_format) if (use_native_c_format)
sprintf(fmt, "%%%s.%dl%c", (flags & F_ALT) ? "#" : "", prec, type); PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%dl%c",
(flags & F_ALT) ? "#" : "", prec, type);
return usprintf(buf, fmt, x); return usprintf(buf, fmt, x);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment