Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,6 @@ POINTER(PyObject *self, PyObject *cls)
PyObject *result;
PyTypeObject *typ;
PyObject *key;
char *buf;

result = PyDict_GetItemWithError(_ctypes_ptrtype_cache, cls);
if (result) {
Expand All @@ -1895,18 +1894,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);
Expand All @@ -1916,16 +1908,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;
Py_INCREF(cls);
Expand Down
6 changes: 2 additions & 4 deletions Modules/_gdbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,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':
Expand All @@ -695,9 +694,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;
}
}
Expand Down
4 changes: 1 addition & 3 deletions Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down