Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: ad6ee8a7a07513226fd458c7907070e16c542b69
Ticket: 504642ffffffffffffffffffffffffffffffffff
Problem in gets w/custom channels
User & Date: bgriffin on 2002-01-16 22:48:26
Changes

  1. assignee changed to: "andreas_kupries"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
        * Tcl Version: 8.3.4
        * OS Platform and Version:  Windows NT 4.0
        * Problem Behavior:
    
    "gets stdin" crashes
    
        * Expected Behavior
    Shouldn't crash...
    
        * Concise Code Sample (if applicable)
    
    Sorry, but I don't have time to put together a concise
    testcase, but I'll describe the problem as I understand
    it and include the patch I made to fix it.
    
    For our Windows application I've defined a console very
    similar to the builtin Tk console.  This console
    supports both stdout and stdin.  These console channels
     execute Tcl code (similar to TkConsolePrint) to
    perform the I/O operation.
    
    The code in Tcl_GetsObjCmd() obtains the ResultObj,
    then passes this Obj to Tcl_GetsObj().  Tcl_GetsObj
    then extracts the bytes pointer from the Obj (line
    3104) and holds onto it until later.
    
    Then the channel Input Proc is called and returns the
    requested data. (Uh Oh!)
    
    Next, some arithmetic is done between the Obj->bytes
    pointer and the saved pointer (from before the call to
    the Input Proc) and at this point bad things happen!
    
    The problem is that this Obj is not reference counted
    and is really owned by the interp.  Down inside the
    channel Input Proc while executing some Tcl code, the
    interp decides to free the Obj and replace it!  The
    Gets code has no way to know this.
    
    To fix this problem I change Tcl_GetsObjCmd to always
    create a NewObj, forget using the interps Obj.  This
    seems safer to me since you really have no clue what's
    going to happen inside the Channel code.
    
    Here's the changes to TcIOCmd.c core-8-3-4 (revision:
    1.7.2.1):
    
    cvs diff -p tclIOCmd.c
    Index: tclIOCmd.c
    ===================================================================
    RCS file: /cvsroot/tcl/tcl/generic/tclIOCmd.c,v
    retrieving revision 1.7.2.1
    diff -p -r1.7.2.1 tclIOCmd.c
    *** tclIOCmd.c  2001/08/06 22:24:11     1.7.2.1
    --- tclIOCmd.c  2002/01/16 22:05:57
    *************** Tcl_GetsObjCmd(dummy, interp, objc, objv
    *** 228,243 ****
              return TCL_ERROR;
          }
      
    !     if (objc == 3) {
    !       /*
    !        * Variable gets line, interp get bytecount.
    !        */
    ! 
    !       linePtr = Tcl_NewObj();
    !     }
    !     else {
    !       linePtr = Tcl_GetObjResult(interp);
    !     }
      
          lineLen = Tcl_GetsObj(chan, linePtr);
          if (lineLen < 0) {
    --- 228,234 ----
              return TCL_ERROR;
          }
      
    !     linePtr = Tcl_NewObj();
      
          lineLen = Tcl_GetsObj(chan, linePtr);
          if (lineLen < 0) {
    *************** Tcl_GetsObjCmd(dummy, interp, objc, objv
    *** 261,266 ****
    --- 252,259 ----
            resultPtr = Tcl_GetObjResult(interp);
            Tcl_SetIntObj(resultPtr, lineLen);
              return TCL_OK;
    +     } else {
    +       Tcl_SetObjResult(interp, linePtr);
          }
          return TCL_OK;
      }
    
    
  4. foundin changed to: "obsolete: 8.3.4"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "bgriffin"
  9. subsystem changed to: "24. Channel Commands"
  10. title changed to: "Problem in gets w/custom channels"
  11. type changed to: "Bug"