Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d70539a

Browse files
committed
Be more robust around bytes for e[st]#? formats.
1 parent 6262cc7 commit d70539a

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

Python/getargs.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
915915
PyObject *s;
916916
int recode_strings;
917917
Py_ssize_t size;
918-
char *ptr;
918+
const char *ptr;
919919

920920
/* Get 'e' parameter: the encoding name */
921921
encoding = (const char *)va_arg(*p_va, const char *);
@@ -941,11 +941,13 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
941941
arg, msgbuf, bufsize);
942942

943943
/* Encode object */
944-
if (!recode_strings && PyString_Check(arg)) {
944+
if (!recode_strings &&
945+
(PyString_Check(arg) || PyBytes_Check(arg))) {
945946
s = arg;
946947
Py_INCREF(s);
947-
size = PyString_GET_SIZE(s);
948-
ptr = PyString_AS_STRING(s);
948+
if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
949+
return converterr("(AsCharBuffer failed)",
950+
arg, msgbuf, bufsize);
949951
}
950952
else {
951953
PyObject *u;
@@ -973,6 +975,8 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
973975
}
974976
size = PyBytes_GET_SIZE(s);
975977
ptr = PyBytes_AS_STRING(s);
978+
if (ptr == NULL)
979+
ptr = "";
976980
}
977981

978982
/* Write output; output is guaranteed to be 0-terminated */

0 commit comments

Comments
 (0)