Issue2324

classification
Title: StringIndexOutOfBoundsException using cStringIO.StringIO
Type: Severity: normal
Components: Library Versions: Jython 2.7
Milestone: Jython 2.7.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: jmadden, zyasoft
Priority: urgent Keywords:

Created on 2015-04-13.19:07:54 by jmadden, last changed 2015-04-23.03:56:38 by zyasoft.

Messages
msg9810 (view) Author: Jason Madden (jmadden) Date: 2015-04-13.19:07:54
This shows up in testing ZODB under Jython. ZODB creates a StringIO object and repeatedly uses it to pickle objects, resetting it each time. Given the write sequence of calls, this results in a StringIndexOutOfBoundsException.

>>> from cStringIO import StringIO
>>> before = 'cpersistent.mapping\nPersistentMapping\nq\x00.}q\x01U\x04dataq\x02}q\x03U\x04testq\x04(U\x08\x00\x00\x00\x00\x00\x00\x00\x01q\x05h\x00tq\x06Qss.'
>>> sio.write(before)
>>> sio.truncate()
>>> sio.seek(0)
strs = ['cpersistent.mapping\nPersistentMapping\n',
... 'q\x00',
... '.',
... '}',
... 'q\x01',
... 'U\x04data',
... 'q\x02',
... '}',
... 'q\x03',
... '(',
... 'K\x00',
... '(',
... 'U\x08\x00\x00\x00\x00\x00\x00\x00\x02',
... 'q\x04',
... 'h\x00',
... 't',
... 'q\x05',
... 'Q',
... 'K\x01',
... '(',
... 'U\x08\x00\x00\x00\x00\x00\x00\x00\x03']
>>> for i in strs:
...  sio.write(i)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
	at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:828)
	at java.lang.StringBuilder.replace(StringBuilder.java:262)
	at org.python.modules.cStringIO$StringIO.write(cStringIO.java:375)
	at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)

java.lang.StringIndexOutOfBoundsException: java.lang.StringIndexOutOfBoundsException: start > end

The exception happens on the very last string in the list.
msg9817 (view) Author: Jim Baker (zyasoft) Date: 2015-04-13.21:51:21
Wow, ZODB? That's crazy talk on Jython! ;)

I will take a look and see if we can fix quickly, since that would be very cool.
msg9819 (view) Author: Jason Madden (jmadden) Date: 2015-04-13.21:55:42
ZODB does have a workaround in using `io.BytesIO`, so it's not super-urgent from that perspective (although I think StringIO is probably faster?). It does seem like a general bug though that anyone could hit.
msg9830 (view) Author: Jim Baker (zyasoft) Date: 2015-04-14.03:56:03
So it's a problem in using seek. and how that interacts with a previous allocation of bytes in terms of determining additional capacity.
msg9839 (view) Author: Jim Baker (zyasoft) Date: 2015-04-14.15:27:58
Easy fix - the replacement, then subsequent append of the StringBuilder is just not computing correct parameters. Fix forthcoming.
msg9858 (view) Author: Jim Baker (zyasoft) Date: 2015-04-15.15:11:20
Fixed as of https://hg.python.org/jython/rev/ce136d0b598f
msg9860 (view) Author: Jason Madden (jmadden) Date: 2015-04-15.15:54:03
Thank you. I can confirm that ce136d0b598f does fix the ZODB tests.
History
Date User Action Args
2015-04-23 03:56:38zyasoftsetstatus: pending -> closed
2015-04-15 15:54:03jmaddensetmessages: + msg9860
2015-04-15 15:11:21zyasoftsetstatus: open -> pending
resolution: accepted -> fixed
messages: + msg9858
2015-04-14 23:33:46zyasoftsetpriority: high -> urgent
milestone: Jython 2.7.0
2015-04-14 15:27:58zyasoftsetmessages: + msg9839
2015-04-14 03:56:03zyasoftsetmessages: + msg9830
2015-04-13 21:55:43jmaddensetmessages: + msg9819
2015-04-13 21:51:21zyasoftsetpriority: high
assignee: zyasoft
resolution: accepted
messages: + msg9817
nosy: + zyasoft
2015-04-13 19:07:54jmaddencreate