Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Segfault in Tcl_ConvertElement()

Closed / Fixed3371644fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2011-07-19 17:54:37
The testcode at http://paste.tclers.tk/2502 causes tcl8.5.10 (release) and current head to segfault (dereferences garbage pointer).

gdb output:

Program received signal SIGSEGV, Segmentation fault.
0xb7f6e493 in TclConvertElement (
    src=0x8074d0d "O\270\267xO\270\267\200O\270\267`O\270\267", length=-2, 
    dst=0x805fa60 "\\#test\\\\\\\\\\\\", flags=4)
    at /xtra/src/tcl8.5.10/unix/../generic/tclUtil.c:1363
1363*p = *src;
(gdb) p p
$1 = 0x808d000 <Address 0x808d000 out of bounds>
(gdb) bt full
#0  0xb7f6e493 in TclConvertElement (
    src=0x8074d0d "O\270\267xO\270\267\200O\270\267`O\270\267", length=-2, 
    dst=0x805fa60 "\\#test\\\\\\\\\\\\", flags=4)
    at /usr/src/tcl8.5.10/unix/../generic/tclUtil.c:1363
        conversion = 4
        p = 0x808d000 <Address 0x808d000 out of bounds>
#1  0xb7f6e24a in Tcl_ConvertCountedElement (
    src=0x808d000 <Address 0x808d000 out of bounds>, length=-1, 
    dst=0x805fa60 "\\#test\\\\\\\\\\\\", flags=4)
    at /usr/src/tcl8.5.10/unix/../generic/tclUtil.c:1202
        numBytes = -1208557020
#2  0xb7f6e21e in Tcl_ConvertElement (
    src=0x8074d0d "O\270\267xO\270\267\200O\270\267`O\270\267", 
    dst=0x808d000 <Address 0x808d000 out of bounds>, flags=79)
    at /usr/src/tcl8.5.10/unix/../generic/tclUtil.c:1171
No locals.
#3  0x080487f8 in main (argc=1, argv=0xbffffa14) at test2.c:20
        flags = 4
        size = 12
        dst = 0x805fa60 "\\#test\\\\\\\\\\\\"
        src = 0x8048900 "#test\\\\\\"
        interp = 0x804c9e0

Valgrind:

==6136== Invalid write of size 1
==6136==    at 0x4137493: TclConvertElement (tclUtil.c:1363)
==6136==    by 0x4137249: Tcl_ConvertCountedElement (tclUtil.c:1202)
==6136==    by 0x413721D: Tcl_ConvertElement (tclUtil.c:1171)
==6136==    by 0x80487F7: main (test2.c:20)
==6136==  Address 0x437e16d is 0 bytes after a block of size 13 alloc'd
==6136==    at 0x4025018: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==6136==    by 0x80487C2: main (test2.c:18)
ferrieuxplain2011-07-20 01:02:12
Also repro on 8.6 HEAD. There, line 1264 of tclUtil.c has a suspicious "length--" applied to a length of -1.

Patch below fixes it and passes the test suite.

Index: generic/tclUtil.c
===================================================================
--- generic/tclUtil.c
+++ generic/tclUtil.c
@@ -1259,11 +1259,11 @@
        if (conversion == CONVERT_ESCAPE) {
            p[0] = '\\';
            p[1] = '#';
            p += 2;
            src++;
-           length--;
+           if (length >=0) length--;
        } else {
            conversion = CONVERT_BRACE;
        }
     }
ferrieuxplain2011-07-20 01:23:01
Fixed in 8.6 HEAD. Leaving open until someone backcommits to 8.5 (properly :P)
dgpplain2011-07-20 02:40:48
allow_comments - 1
Log in (anonymous is fine) to add a comment.