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; PyObject *result;
PyTypeObject *typ; PyTypeObject *typ;
PyObject *key; PyObject *key;
char *buf;
result = PyDict_GetItemWithError(_ctypes_ptrtype_cache, cls); result = PyDict_GetItemWithError(_ctypes_ptrtype_cache, cls);
if (result) { if (result) {
@ -1890,18 +1889,11 @@ POINTER(PyObject *self, PyObject *cls)
return NULL; return NULL;
} }
if (PyUnicode_CheckExact(cls)) { if (PyUnicode_CheckExact(cls)) {
const char *name = PyUnicode_AsUTF8(cls); PyObject *name = PyUnicode_FromFormat("LP_%U", cls);
if (name == NULL)
return NULL;
buf = PyMem_Malloc(strlen(name) + 3 + 1);
if (buf == NULL)
return PyErr_NoMemory();
sprintf(buf, "LP_%s", name);
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type), result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
"s(O){}", "N(O){}",
buf, name,
&PyCPointer_Type); &PyCPointer_Type);
PyMem_Free(buf);
if (result == NULL) if (result == NULL)
return result; return result;
key = PyLong_FromVoidPtr(result); key = PyLong_FromVoidPtr(result);
@ -1911,16 +1903,12 @@ POINTER(PyObject *self, PyObject *cls)
} }
} else if (PyType_Check(cls)) { } else if (PyType_Check(cls)) {
typ = (PyTypeObject *)cls; typ = (PyTypeObject *)cls;
buf = PyMem_Malloc(strlen(typ->tp_name) + 3 + 1); PyObject *name = PyUnicode_FromFormat("LP_%s", typ->tp_name);
if (buf == NULL)
return PyErr_NoMemory();
sprintf(buf, "LP_%s", typ->tp_name);
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type), result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
"s(O){sO}", "N(O){sO}",
buf, name,
&PyCPointer_Type, &PyCPointer_Type,
"_type_", cls); "_type_", cls);
PyMem_Free(buf);
if (result == NULL) if (result == NULL)
return result; return result;
key = Py_NewRef(cls); key = Py_NewRef(cls);

View File

@ -675,7 +675,6 @@ dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
return NULL; return NULL;
} }
for (flags++; *flags != '\0'; flags++) { for (flags++; *flags != '\0'; flags++) {
char buf[40];
switch (*flags) { switch (*flags) {
#ifdef GDBM_FAST #ifdef GDBM_FAST
case 'f': case 'f':
@ -693,9 +692,8 @@ dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
break; break;
#endif #endif
default: default:
PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.", PyErr_Format(state->gdbm_error,
*flags); "Flag '%c' is not supported.", (unsigned char)*flags);
PyErr_SetString(state->gdbm_error, buf);
return NULL; return NULL;
} }
} }

View File

@ -1365,9 +1365,7 @@ xmlparse_buffer_size_setter(xmlparseobject *self, PyObject *v, void *closure)
/* check maximum */ /* check maximum */
if (new_buffer_size > INT_MAX) { if (new_buffer_size > INT_MAX) {
char errmsg[100]; PyErr_Format(PyExc_ValueError, "buffer_size must not be greater than %i", INT_MAX);
sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
PyErr_SetString(PyExc_ValueError, errmsg);
return -1; return -1;
} }