Tk Source Code

Ticket Change Details
Login
Overview

Artifact ID: cc5ceef6064ac3d56e694cecf2d221ad5935afa2ef898054bc26c2729b6dd12f
Ticket: 40e4bf61988580b8ffaecd1d57a7087ba76b54d4
Entry: double free when textvariable set in validatecommand script
User & Date: chrstphrchvz 2020-05-22 12:12:47
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    Here is a double free bug that someone originally reported for Perl/Tk ([https://rt.cpan.org/Public/Bug/Display.html?id=102648]), but which I replicated in Tcl/Tk using both the original Perl/Tk example through Tcl::pTk, as well as a nearly equivalent Tcl syntax example:
    
    <pre>
    package require Tk
    
    set en_text {Type 'A' here}
    pack [entry .e \
        -textvariable en_text \
        -validate key \
        -validatecommand {
            if {"%S" eq {A}} {
                set en_text %P
                .e configure -validate key
            }
            return 1
        }
    ]
    </pre>
    
    Pressing 'A' causes the double free in this example. When the key is pressed, <code>InsertChars()</code> stores the existing <code>entryPtr-&gt;string</code> in the variable <code>string</code>. It then does the <code>EntryValidate()</code> &rarr; <code>EntryValidateChange()</code> to evaluate the validatecommand script. When the textvariable is set in the script, this invokes <code>EntryTextVarProc()</code> &rarr; <code>EntrySetValue()</code> which frees the existing <code>entryPtr-&gt;string</code>. After validation finishes, <code>EntrySet()</code> then tries to free the already-freed address in <code>string</code>.
    
    It's not obvious to me what should be done about this issue. Should this at least not lead to a crash/corruption, or is it believed that the documentation already firmly warned against mixing <code>-textvariable</code> and <code>-validatecommand</code> such that this issue is expected behavior? At a minimum, it preferably crashes rather than silently corrupts—i.e. panic if <code>string != entryPtr-&gt;string</code> just before <code>ckfree((char *)string)</code>.
    
    I am not aware of this issue affecting Ttk entry widgets.
    
  5. foundin changed to: "8.6.10"
  6. is_private changed to: "0"
  7. login: "chrstphrchvz"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Important"
  11. status changed to: "Open"
  12. submitter changed to: "chrstphrchvz"
  13. subsystem changed to: "07. [entry]"
  14. title changed to:
    Entry: double free when textvariable set in validatecommand script
    
  15. type changed to: "Bug"