Tk Source Code

View Ticket
Login
Ticket UUID: 745965
Title: Potential read of deallocated mem?
Type: Bug Version: obsolete: 8.5a0
Submitter: dkf Created on: 2003-05-30 09:21:59
Subsystem: 18. [text] Assigned To: vincentdarley
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2003-07-08 22:14:10
Resolution: Fixed Closed By: vincentdarley
    Closed on: 2003-07-08 15:14:10
Description:
As I was building a test version of Tk, I noticed that
in (version 1.16 of) tkTextDisp.c the variable 'flags'
at line 2247 was unused in the function DisplayText. 
Further investigation of that code suddenly had an
unused variable indicated to me that there seems to be
a potential for trying to read from a deallocated
structure, which could happen if the Tcl_Release call
at line 2253, when testing for whether a delete had
happened on line 2254.  Is this really the case?  It
seems unfortunate if it is so, but if it can be proved
to be not a problem, then perhaps the Tcl_Preserve /
Tcl_Release pair are not needed (though I suspect they
are, given that they are around something that can
trigger events/callbacks...)
User Comments: vincentdarley added on 2003-07-08 22:14:10:
Logged In: YES 
user_id=32170

Jeff has fixed this. (Bug #767009)

dkf added on 2003-05-30 20:12:41:
Logged In: YES 
user_id=79902

That's what I suspect too, and yet it's not an easy fault to
trigger from what I can see.  Your fix that you suggested at
12:13 looks pretty good to me BTW, but I agree that it would
be best to have a test first.  I tried the following
(together with moving the mouse into the tag) and it
*didn't* trigger a reported memory fault:
  pack [text .t]
  .t insert 1.0 "abc " {} def click
  .t tag conf click -foreground blue -underlin 1
  .t tag bind click <Enter> {.t insert 1.0 598263598263456}
  .t tag bind click <Leave> {destroy .t}

Mind you, perhaps you need a tool like Purify to see the
fault, with things working when you can rely on the old (and
horrible) trick of relying on memory not getting overwritten
immediately after free()...

vincentdarley added on 2003-05-30 18:13:10:
Logged In: YES 
user_id=32170

On second thought, maybe what is needed is to move 
the Tcl_Release to after the test (and get rid of the 
setting 'flags' and possibly tkwin):

if ((textPtr->tkwin == NULL) || (textPtr->flags & 
DESTROYED)) {
    Tcl_Release((ClientData) textPtr);
    goto end;
}
Tcl_Release((ClientData) textPtr);

It certainly seems, if the comment above is correct, that 
the entire tkwin could disappear, which means one 
ought to be able to write a test case for this and trigger 
a segfault.

vincentdarley added on 2003-05-30 18:10:04:
Logged In: YES 
user_id=32170

Looks like that code ought to read:

if (tkwin == NULL || flags & destroyed) { goto end; }

So, I think you are right.