gh-99426: Use PyUnicode_FromFormat() and PyErr_Format() instead of sprintf (GH-99427)

This commit is contained in:
Serhiy Storchaka 2022-11-14 15:25:34 +02:00 committed by GitHub
parent c340cbb7f7
commit 3fe7d7c020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 25 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}