Skip to content

Commit 1ed64f5

Browse files
author
neal.norwitz
committed
Use sizeof(buffer) instead of duplicating the constants to ensure they won't
be wrong. The real change is to pass (bufsz - 1) to PyOS_ascii_formatd and 1 to strncat. strncat copies n+1 bytes from src (not dest). Reported by Klocwork #58. git-svn-id: http://svn.python.org/projects/python/trunk@50679 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent bdbd3cb commit 1ed64f5

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Objects/complexobject.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,16 @@ complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
274274
{
275275
char format[32];
276276
if (v->cval.real == 0.) {
277-
PyOS_snprintf(format, 32, "%%.%ig", precision);
278-
PyOS_ascii_formatd(buf, bufsz, format, v->cval.imag);
279-
strncat(buf, "j", bufsz);
277+
PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
278+
PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
279+
strncat(buf, "j", 1);
280280
} else {
281281
char re[64], im[64];
282282
/* Format imaginary part with sign, real part without */
283-
PyOS_snprintf(format, 32, "%%.%ig", precision);
284-
PyOS_ascii_formatd(re, 64, format, v->cval.real);
285-
PyOS_snprintf(format, 32, "%%+.%ig", precision);
286-
PyOS_ascii_formatd(im, 64, format, v->cval.imag);
283+
PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
284+
PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
285+
PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
286+
PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
287287
PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im);
288288
}
289289
}

0 commit comments

Comments
 (0)