Tcl Source Code

View Ticket
Login
Ticket UUID: 3067036
Title: Appending to 0-length byte array causes busy loop.
Type: Bug Version: obsolete: 8.6b1.1
Submitter: sbron Created on: 2010-09-15 17:52:43
Subsystem: 12. ByteArray Object Assigned To: dkf
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2010-09-16 05:14:11
Resolution: Fixed Closed By: dkf
    Closed on: 2010-09-15 22:14:11
Description:
The code below goes into a busy loop with 100% cpu usage:

set f [open /dev/null]
chan configure $f -blocking 0 -translation binary
set str [read $f 2]
append str [binary format a* foo]

The problem is in TclAppendBytesToByteArray(). This function tries to calculate the needed memory by doubling the currently allocated space until it is big enough. That method fails miserably when starting with an allocated space of size 0, as is the case in the code above.
User Comments: dkf added on 2010-09-16 05:14:11:

allow_comments - 1

Fixed in HEAD by jumping straight to enough space in the zero-allocated-before case.

dgp added on 2010-09-16 01:14:20:
FWIW, I think it would be good to cover as many of
these growth algorithms as possible with just one (set
of) macro templates.

andreas_kupries added on 2010-09-16 00:58:11:
Interesting. I remember seeing a different place where memory doubling was done which did things a bit differently: It doubled the length of the "requested" space, not the current one. As 'requested > allocated' this could never be 0. ... Remembering more, that was somewhere in appending to a list.

I guess the "appending to string/bytearray" should be rewritten to the same scheme.

sbron added on 2010-09-16 00:52:44:

File Added - 386772: tclBinary.c.patch

Attachments: