New submission from STINNER Victor <victor.stin...@haypocalc.com>:

mmap, buffer, bytearray, string and unicode objects set the char buffer 
callback (bf_getcharbuffer). The bytearray object sets also the release buffer 
callback (bf_releasebuffer).

In Python 2.7, PyObject_AsCharBuffer() accepts bytearray objects, whereas the 
"t#" format of PyArg_Parse functions rejects byte bytearray objects (expect an 
"pinned buffer object").

In Python 3.2, PyObject_AsCharBuffer() releases the buffer.

PyObject_AsCharBuffer() documentation (in 2.7 and 3.2) says that the function 
only accepts read-only objects.

Something is wrong here. If the caller doesn't hold a kind of lock, the object 
cannot be protected against futher modifications. The caller has to ensure that 
the object is not modifiable until it finishs to use the char* pointer.

I think that it would be safer to respect the documentation: 
PyObject_AsCharBuffer() should only accept read-only objects. The most 
important change is that functions using PyObject_AsCharBuffer() will not 
accept bytearray objects anymore.

Attached patch (for Python 2.7) changes PyObject_AsCharBuffer() to reject 
modifiable objects. It removes also the character buffer callback from the 
bytearray type. To avoid breaking compatibility too much, I patched int(), 
long() and float() to still support bytearray objects.

Examples of functions rejecting bytearray with the patch:
 - int(), long(), float(), complex()
 - many str methods: split, partition, rpartition, rsplit, index, find, count, 
translate, replace, startswith, endswith
 - writelines() of file objects (eg. sys.stdout.writelines)
 - writelines() method of a bz2 file

--

My patch breaks backward compatibility, and I don't know that it is acceptable 
in Python 2.7.

I will write a similar patch for Python 3.2.

----------
components: Interpreter Core
files: PyObject_AsCharBuffer-2.7.patch
keywords: patch
messages: 113895
nosy: haypo, pitrou
priority: normal
severity: normal
status: open
title: PyObject_AsCharBuffer() should only accept read-only objects
versions: Python 2.7, Python 3.2
Added file: http://bugs.python.org/file18523/PyObject_AsCharBuffer-2.7.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue9602>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to