SWIG_AsWCharPtrAndSize improper operation if cptr NULL

SF bug #1327

This doesn't have any noticeable effect with the usage of
SWIG_AsWCharPtrAndSize as shipped by SWIG, but could be a problem if a
user is using this function with cptr equal to zero and psize is non-zero
 - the length would be incorrectly set due to the call to PyUnicode_GetSize
failing.
This commit is contained in:
William S Fulton 2013-07-02 18:47:00 +01:00
commit ace8fcd972
3 changed files with 15 additions and 3 deletions

View file

@ -38,6 +38,14 @@ wchar_t* test_cvalue(wchar_t* x) {
} }
wchar_t* test_wchar_overload() {
return 0;
}
wchar_t* test_wchar_overload(wchar_t *x) {
return x;
}
std::wstring test_value(std::wstring x) { std::wstring test_value(std::wstring x) {
return x; return x;
} }

View file

@ -13,6 +13,12 @@ if li_std_wstring.test_ccvalue(x) != x:
if li_std_wstring.test_cvalue(x) != x: if li_std_wstring.test_cvalue(x) != x:
raise RuntimeError("bad string mapping") raise RuntimeError("bad string mapping")
if li_std_wstring.test_wchar_overload(x) != x:
raise RuntimeError("bad string mapping")
if li_std_wstring.test_wchar_overload("not unicode") != "not unicode":
raise RuntimeError("bad string mapping")
if li_std_wstring.test_value(x) != x: if li_std_wstring.test_value(x) != x:
print x, li_std_wstring.test_value(x) print x, li_std_wstring.test_value(x)
raise RuntimeError("bad string mapping") raise RuntimeError("bad string mapping")

View file

@ -18,9 +18,7 @@ SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc)
int isunicode = PyUnicode_Check(obj); int isunicode = PyUnicode_Check(obj);
%#if PY_VERSION_HEX < 0x03000000 %#if PY_VERSION_HEX < 0x03000000
if (!isunicode && PyString_Check(obj)) { if (!isunicode && PyString_Check(obj)) {
if (cptr) { obj = tmp = PyUnicode_FromObject(obj);
obj = tmp = PyUnicode_FromObject(obj);
}
isunicode = 1; isunicode = 1;
} }
%#endif %#endif