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

Skip to content

Commit d586756

Browse files
committed
weakref_repr(), proxy_repr(): Conversion of sprintf() to
PyOS_snprintf() for buffer overrun avoidance.
1 parent e5c492d commit d586756

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

Objects/weakrefobject.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ weakref_repr(PyWeakReference *self)
131131
{
132132
char buffer[256];
133133
if (PyWeakref_GET_OBJECT(self) == Py_None) {
134-
sprintf(buffer, "<weakref at %lx; dead>",
135-
(long)(self));
134+
PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %lx; dead>",
135+
(long)(self));
136136
}
137137
else {
138-
sprintf(buffer, "<weakref at %#lx; to '%.50s' at %#lx>",
139-
(long)(self), PyWeakref_GET_OBJECT(self)->ob_type->tp_name,
140-
(long)(PyWeakref_GET_OBJECT(self)));
138+
PyOS_snprintf(buffer, sizeof(buffer),
139+
"<weakref at %#lx; to '%.50s' at %#lx>",
140+
(long)(self),
141+
PyWeakref_GET_OBJECT(self)->ob_type->tp_name,
142+
(long)(PyWeakref_GET_OBJECT(self)));
141143
}
142144
return PyString_FromString(buffer);
143145
}
@@ -265,9 +267,10 @@ static PyObject *
265267
proxy_repr(PyWeakReference *proxy)
266268
{
267269
char buf[160];
268-
sprintf(buf, "<weakref at %p to %.100s at %p>", proxy,
269-
PyWeakref_GET_OBJECT(proxy)->ob_type->tp_name,
270-
PyWeakref_GET_OBJECT(proxy));
270+
PyOS_snprintf(buf, sizeof(buf),
271+
"<weakref at %p to %.100s at %p>", proxy,
272+
PyWeakref_GET_OBJECT(proxy)->ob_type->tp_name,
273+
PyWeakref_GET_OBJECT(proxy));
271274
return PyString_FromString(buf);
272275
}
273276

0 commit comments

Comments
 (0)