Skip to content

Commit 6adf19b

Browse files
committed
Check Py{Bytes,String}_AsStringAndSize() for failure
PyBytes_AsStringAndSize() and PyString_AsStringAndSize() were not being checked for failure. Closes swig#1349.
1 parent 666752d commit 6adf19b

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Lib/python/pyhead.swg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ SWIG_Python_str_AsChar(PyObject *str)
4343
if (str) {
4444
char *cstr;
4545
Py_ssize_t len;
46-
PyBytes_AsStringAndSize(str, &cstr, &len);
46+
if (PyBytes_AsStringAndSize(str, &cstr, &len) == -1)
47+
return NULL;
4748
newstr = (char *) malloc(len+1);
49+
if (!newstr)
50+
return NULL;
4851
memcpy(newstr, cstr, len+1);
4952
Py_XDECREF(str);
5053
}

Lib/python/pystrings.swg

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
3232
if (alloc)
3333
*alloc = SWIG_NEWOBJ;
3434
%#endif
35-
PyBytes_AsStringAndSize(obj, &cstr, &len);
35+
if (PyBytes_AsStringAndSize(obj, &cstr, &len) == -1)
36+
return SWIG_TypeError;
3637
%#else
37-
PyString_AsStringAndSize(obj, &cstr, &len);
38+
if (PyString_AsStringAndSize(obj, &cstr, &len) == -1)
39+
return SWIG_TypeError;
3840
%#endif
3941
if (cptr) {
4042
if (alloc) {

0 commit comments

Comments
 (0)