summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-11-14 13:25:34 (GMT)
committerGitHub <noreply@github.com>2022-11-14 13:25:34 (GMT)
commit3fe7d7c020a8b2d395a58bfafbe689ee36f7fe30 (patch)
tree379b635e70647b91d26456c2dc3ff58e970bfeba
parentc340cbb7f74bc99eaf72d6a4ef5b4d504d8519c8 (diff)
downloadcpython-3fe7d7c020a8b2d395a58bfafbe689ee36f7fe30.zip
cpython-3fe7d7c020a8b2d395a58bfafbe689ee36f7fe30.tar.gz
cpython-3fe7d7c020a8b2d395a58bfafbe689ee36f7fe30.tar.bz2
gh-99426: Use PyUnicode_FromFormat() and PyErr_Format() instead of sprintf (GH-99427)
-rw-r--r--Modules/_ctypes/callproc.c24
-rw-r--r--Modules/_gdbmmodule.c6
-rw-r--r--Modules/pyexpat.c4
3 files changed, 9 insertions, 25 deletions
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 520a0f9..a6c005b 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -1880,7 +1880,6 @@ POINTER(PyObject *self, PyObject *cls)
PyObject *result;
PyTypeObject *typ;
PyObject *key;
- char *buf;
result = PyDict_GetItemWithError(_ctypes_ptrtype_cache, cls);
if (result) {
@@ -1890,18 +1889,11 @@ POINTER(PyObject *self, PyObject *cls)
return NULL;
}
if (PyUnicode_CheckExact(cls)) {
- const char *name = PyUnicode_AsUTF8(cls);
- if (name == NULL)
- return NULL;
- buf = PyMem_Malloc(strlen(name) + 3 + 1);
- if (buf == NULL)
- return PyErr_NoMemory();
- sprintf(buf, "LP_%s", name);
+ PyObject *name = PyUnicode_FromFormat("LP_%U", cls);
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
- "s(O){}",
- buf,
+ "N(O){}",
+ name,
&PyCPointer_Type);
- PyMem_Free(buf);
if (result == NULL)
return result;
key = PyLong_FromVoidPtr(result);
@@ -1911,16 +1903,12 @@ POINTER(PyObject *self, PyObject *cls)
}
} else if (PyType_Check(cls)) {
typ = (PyTypeObject *)cls;
- buf = PyMem_Malloc(strlen(typ->tp_name) + 3 + 1);
- if (buf == NULL)
- return PyErr_NoMemory();
- sprintf(buf, "LP_%s", typ->tp_name);
+ PyObject *name = PyUnicode_FromFormat("LP_%s", typ->tp_name);
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
- "s(O){sO}",
- buf,
+ "N(O){sO}",
+ name,
&PyCPointer_Type,
"_type_", cls);
- PyMem_Free(buf);
if (result == NULL)
return result;
key = Py_NewRef(cls);
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c
index e8469da..4e8acde 100644
--- a/Modules/_gdbmmodule.c
+++ b/Modules/_gdbmmodule.c
@@ -675,7 +675,6 @@ dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
return NULL;
}
for (flags++; *flags != '\0'; flags++) {
- char buf[40];
switch (*flags) {
#ifdef GDBM_FAST
case 'f':
@@ -693,9 +692,8 @@ dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
break;
#endif
default:
- PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.",
- *flags);
- PyErr_SetString(state->gdbm_error, buf);
+ PyErr_Format(state->gdbm_error,
+ "Flag '%c' is not supported.", (unsigned char)*flags);
return NULL;
}
}
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index 3fe4259..70222ad 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -1365,9 +1365,7 @@ xmlparse_buffer_size_setter(xmlparseobject *self, PyObject *v, void *closure)
/* check maximum */
if (new_buffer_size > INT_MAX) {
- char errmsg[100];
- sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
- PyErr_SetString(PyExc_ValueError, errmsg);
+ PyErr_Format(PyExc_ValueError, "buffer_size must not be greater than %i", INT_MAX);
return -1;
}