Skip to content

Commit 3fe7d7c

Browse files
gh-99426: Use PyUnicode_FromFormat() and PyErr_Format() instead of sprintf (GH-99427)
1 parent c340cbb commit 3fe7d7c

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

Modules/_ctypes/callproc.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,6 @@ POINTER(PyObject *self, PyObject *cls)
18801880
PyObject *result;
18811881
PyTypeObject *typ;
18821882
PyObject *key;
1883-
char *buf;
18841883

18851884
result = PyDict_GetItemWithError(_ctypes_ptrtype_cache, cls);
18861885
if (result) {
@@ -1890,18 +1889,11 @@ POINTER(PyObject *self, PyObject *cls)
18901889
return NULL;
18911890
}
18921891
if (PyUnicode_CheckExact(cls)) {
1893-
const char *name = PyUnicode_AsUTF8(cls);
1894-
if (name == NULL)
1895-
return NULL;
1896-
buf = PyMem_Malloc(strlen(name) + 3 + 1);
1897-
if (buf == NULL)
1898-
return PyErr_NoMemory();
1899-
sprintf(buf, "LP_%s", name);
1892+
PyObject *name = PyUnicode_FromFormat("LP_%U", cls);
19001893
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
1901-
"s(O){}",
1902-
buf,
1894+
"N(O){}",
1895+
name,
19031896
&PyCPointer_Type);
1904-
PyMem_Free(buf);
19051897
if (result == NULL)
19061898
return result;
19071899
key = PyLong_FromVoidPtr(result);
@@ -1911,16 +1903,12 @@ POINTER(PyObject *self, PyObject *cls)
19111903
}
19121904
} else if (PyType_Check(cls)) {
19131905
typ = (PyTypeObject *)cls;
1914-
buf = PyMem_Malloc(strlen(typ->tp_name) + 3 + 1);
1915-
if (buf == NULL)
1916-
return PyErr_NoMemory();
1917-
sprintf(buf, "LP_%s", typ->tp_name);
1906+
PyObject *name = PyUnicode_FromFormat("LP_%s", typ->tp_name);
19181907
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
1919-
"s(O){sO}",
1920-
buf,
1908+
"N(O){sO}",
1909+
name,
19211910
&PyCPointer_Type,
19221911
"_type_", cls);
1923-
PyMem_Free(buf);
19241912
if (result == NULL)
19251913
return result;
19261914
key = Py_NewRef(cls);

Modules/_gdbmmodule.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,6 @@ dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
675675
return NULL;
676676
}
677677
for (flags++; *flags != '\0'; flags++) {
678-
char buf[40];
679678
switch (*flags) {
680679
#ifdef GDBM_FAST
681680
case 'f':
@@ -693,9 +692,8 @@ dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
693692
break;
694693
#endif
695694
default:
696-
PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.",
697-
*flags);
698-
PyErr_SetString(state->gdbm_error, buf);
695+
PyErr_Format(state->gdbm_error,
696+
"Flag '%c' is not supported.", (unsigned char)*flags);
699697
return NULL;
700698
}
701699
}

Modules/pyexpat.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,9 +1365,7 @@ xmlparse_buffer_size_setter(xmlparseobject *self, PyObject *v, void *closure)
13651365

13661366
/* check maximum */
13671367
if (new_buffer_size > INT_MAX) {
1368-
char errmsg[100];
1369-
sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
1370-
PyErr_SetString(PyExc_ValueError, errmsg);
1368+
PyErr_Format(PyExc_ValueError, "buffer_size must not be greater than %i", INT_MAX);
13711369
return -1;
13721370
}
13731371

0 commit comments

Comments
 (0)