Tk Library Source Code

View Ticket
Login
Ticket UUID: 718985
Title: Potential infinite loop
Type: Bug Version: None
Submitter: dkf Created on: 2003-04-10 13:11:42
Subsystem: irc Assigned To: andreas_kupries
Priority: 8 Severity:
Status: Closed Last Modified: 2003-04-14 13:39:29
Resolution: Accepted Closed By: andreas_kupries
    Closed on: 2003-04-14 06:39:29
Description:
The Tcllib IRC module is capable of going into an
infinite loop if the IRC server closes the socket
unexpectedly.

The problem is that the GetEvent procedure in irc.tcl
doesn't make any attempt to close the socket once [eof]
has been received, and [gets] does not generate an
error when you try to read from a remotely-closed
socket (it just retrieves an empty string and returns
-1).  Hence if the socket enters the [eof] state
through premature closure by the remote end, the Tcl
end never gets closed; the fileevent handler returns
with the socket in the eof state and gets immediately
reinvoked (becaused part-closed sockets are always
readable; the underlying read() calls don't block,
since they can definitely return that no data was
available.)

I suggest this version of that routine instead (which
is mostly just reordering):

  proc GetEvent { } {
     variable linedata
     variable sock
     array set linedata {}
     if {[catch {
        gets $sock line
     } err]} {
        close $sock
        ${irc::log}::error \
                "Error receiving from network: $err"
        return
     }
     # Since we're using blocking sockets, testing the
     # result of [gets] is sufficient to detect EOF
     if {$err < 0} {
        if {[info exists dispatch(EOF)]} {
           $dispatch(EOF)
        }
        close $sock
     }
     if {[string match :* $line]} {
        DispatchServerEvent [string range $line 1 end]
     } else {
        # Should this command get an empty string on
        # end-of-file?  If not, add a return after
        # the close above.
        DispatchServerCmd $line
     }
  }
User Comments: andreas_kupries added on 2003-04-14 13:39:29:
Logged In: YES 
user_id=75003

Committed to head.

andreas_kupries added on 2003-04-12 04:43:53:
Logged In: YES 
user_id=75003

Reassigning to me as David is out of reach for a while given 
his move to Italy.