Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5c492d

Browse files
committed
formatfloat(), formatint(): Conversion of sprintf() to PyOS_snprintf()
for buffer overrun avoidance.
1 parent 312af42 commit e5c492d

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

Objects/unicodeobject.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5080,7 +5080,8 @@ formatfloat(Py_UNICODE *buf,
50805080
prec = 6;
50815081
if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
50825082
type = 'g';
5083-
sprintf(fmt, "%%%s.%d%c", (flags & F_ALT) ? "#" : "", prec, type);
5083+
PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c",
5084+
(flags & F_ALT) ? "#" : "", prec, type);
50845085
/* worst case length calc to ensure no buffer overrun:
50855086
fmt = %#.<prec>g
50865087
buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
@@ -5151,15 +5152,16 @@ formatint(Py_UNICODE *buf,
51515152
*/
51525153
if (x == 0 && (flags & F_ALT) && (type == 'x' || type == 'X')) {
51535154
/* Only way to know what the platform does is to try it. */
5154-
sprintf(fmt, type == 'x' ? "%#x" : "%#X", 0);
5155+
PyOS_snprintf(fmt, sizeof(fmt), type == 'x' ? "%#x" : "%#X", 0);
51555156
if (fmt[1] != (char)type) {
51565157
/* Supply our own leading 0x/0X -- needed under std C */
51575158
use_native_c_format = 0;
5158-
sprintf(fmt, "0%c%%#.%dl%c", type, prec, type);
5159+
PyOS_snprintf(fmt, sizeof(fmt), "0%c%%#.%dl%c", type, prec, type);
51595160
}
51605161
}
51615162
if (use_native_c_format)
5162-
sprintf(fmt, "%%%s.%dl%c", (flags & F_ALT) ? "#" : "", prec, type);
5163+
PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%dl%c",
5164+
(flags & F_ALT) ? "#" : "", prec, type);
51635165
return usprintf(buf, fmt, x);
51645166
}
51655167

0 commit comments

Comments
 (0)