Commit c2f7725c authored by Thomas Heller's avatar Thomas Heller

Fix bug #1646630: ctypes.string_at(buf, 0) and ctypes.wstring_at(buf, 0)

returned string up to the first NUL character.
parent b7b2b4ee
...@@ -480,7 +480,7 @@ def cast(obj, typ): ...@@ -480,7 +480,7 @@ def cast(obj, typ):
return _cast(obj, obj, typ) return _cast(obj, obj, typ)
_string_at = CFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr) _string_at = CFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
def string_at(ptr, size=0): def string_at(ptr, size=-1):
"""string_at(addr[, size]) -> string """string_at(addr[, size]) -> string
Return the string at addr.""" Return the string at addr."""
...@@ -492,7 +492,7 @@ except ImportError: ...@@ -492,7 +492,7 @@ except ImportError:
pass pass
else: else:
_wstring_at = CFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr) _wstring_at = CFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
def wstring_at(ptr, size=0): def wstring_at(ptr, size=-1):
"""wstring_at(addr[, size]) -> string """wstring_at(addr[, size]) -> string
Return the string at addr.""" Return the string at addr."""
......
...@@ -14,6 +14,7 @@ class MemFunctionsTest(unittest.TestCase): ...@@ -14,6 +14,7 @@ class MemFunctionsTest(unittest.TestCase):
self.failUnlessEqual(string_at(result), "Hello, World") self.failUnlessEqual(string_at(result), "Hello, World")
self.failUnlessEqual(string_at(result, 5), "Hello") self.failUnlessEqual(string_at(result, 5), "Hello")
self.failUnlessEqual(string_at(result, 16), "Hello, World\0\0\0\0") self.failUnlessEqual(string_at(result, 16), "Hello, World\0\0\0\0")
self.failUnlessEqual(string_at(result, 0), "")
def test_memset(self): def test_memset(self):
a = create_string_buffer(1000000) a = create_string_buffer(1000000)
...@@ -54,6 +55,7 @@ class MemFunctionsTest(unittest.TestCase): ...@@ -54,6 +55,7 @@ class MemFunctionsTest(unittest.TestCase):
self.failUnlessEqual(wstring_at(a), "Hello, World") self.failUnlessEqual(wstring_at(a), "Hello, World")
self.failUnlessEqual(wstring_at(a, 5), "Hello") self.failUnlessEqual(wstring_at(a, 5), "Hello")
self.failUnlessEqual(wstring_at(a, 16), "Hello, World\0\0\0\0") self.failUnlessEqual(wstring_at(a, 16), "Hello, World\0\0\0\0")
self.failUnlessEqual(wstring_at(a, 0), "")
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
...@@ -152,6 +152,9 @@ Core and builtins ...@@ -152,6 +152,9 @@ Core and builtins
Library Library
------- -------
- Bug #1646630: ctypes.string_at(buf, 0) and ctypes.wstring_at(buf, 0)
returned string up to the first NUL character.
- Patch #957003: Implement smtplib.LMTP. - Patch #957003: Implement smtplib.LMTP.
- Patch #1481079: add support for HTTP_REFERER to CGIHTTPServer. - Patch #1481079: add support for HTTP_REFERER to CGIHTTPServer.
......
...@@ -4537,9 +4537,9 @@ create_comerror(void) ...@@ -4537,9 +4537,9 @@ create_comerror(void)
#endif #endif
static PyObject * static PyObject *
string_at(const char *ptr, Py_ssize_t size) string_at(const char *ptr, int size)
{ {
if (size == 0) if (size == -1)
return PyString_FromString(ptr); return PyString_FromString(ptr);
return PyString_FromStringAndSize(ptr, size); return PyString_FromStringAndSize(ptr, size);
} }
...@@ -4624,7 +4624,7 @@ cast(void *ptr, PyObject *src, PyObject *ctype) ...@@ -4624,7 +4624,7 @@ cast(void *ptr, PyObject *src, PyObject *ctype)
static PyObject * static PyObject *
wstring_at(const wchar_t *ptr, int size) wstring_at(const wchar_t *ptr, int size)
{ {
if (size == 0) if (size == -1)
size = wcslen(ptr); size = wcslen(ptr);
return PyUnicode_FromWideChar(ptr, size); return PyUnicode_FromWideChar(ptr, size);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment