Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 212511ba6408f594c7da396858e963ff77a178d4
Ticket: 218549ffffffffffffffffffffffffffffffffff
Mac socket code can corrupt data on fcopy
User & Date: nobody on 2000-10-26 01:02:01
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 1005 Bug
    Version: 8.0.4
    SubmitDate: '1998-12-22'
    LastModified: '1999-03-22'
    Severity: MED
    Status: Released
    Submitter: welch
    ChangedBy: stanton
    OS: Macintosh
    OSVersion: 8.1
    Machine: Power PC
    FixedDate: '1999-03-22'
    FixedInVersion: 8.0.6
    ClosedDate: '2000-10-25'
    
    
    Name:
    
    Mark Roseman
    
    
    
    ReproducibleScript:
    
    Just a simple fcopy of a large file from a Mac to another machine
    
    across a slow or high latency network.  Happens somewhat sporadically,
    
    but larger files and slower networks help reproduce it.
    
    
    
    
    
    
    
    ObservedBehavior:
    
    File will be corrupted.  Specifically, if the fcopy routine is writing
    
    buffers B1, B2, B3 etc., the stream sent over the network will sometimes
    
    go out with one buffer repeated twice, e.g. B1 B3 B3 B4.
    
    
    
    While the MacTcl socket code guards against actually sending out a new
    
    buffer while a write is in progress, this isn't sufficient for the 
    
    MacTCP routines.  The TCPSend call does not copy the buffers when they
    
    are received from the caller, but stores the original pointer.  If before
    
    the asynchronous call is complete, the buffer stored at that address is
    
    modified, the TCPSend call will use the new contents of the buffer, not
    
    the original.
    
    
    
    The Tcl fcopy command reuses a single buffer when transferring data between
    
    the two channels, so this problem will occur.  On fast connections the 
    
    asynchronous call usually completes quickly enough that this condition does 
    
    not appear.  However, on slower connections this will show up.
    
    
    
    The attached patch avoids this problem by copying data from the caller's
    
    buffer into a private buffer.
    
    
    
    
    
    
    
    DesiredBehavior:
    
    Files sent correctly over the network.
    
    
    
    
    
    
    
    Patch:
    
    *** /home/groupkit/tcl8.0.4/mac/tclMacSock.c    Mon Sep 14 12:40:06 1998
    
    --- tclMacSock.c        Thu Dec 17 14:49:02 1998
    
    ***************
    
    *** 82,87 ****
    
    --- 82,90 ----
    
          rdsEntry rdsarray[5+1];      /* Array used when cleaning out recieve
    
                                        * buffers on a closing socket. */
    
          Tcl_Channel channel;         /* Channel associated with this socket. */
    
    +     int writeBufferSize;           /* Size of buffer to hold data for
    
    +                                     *  asynchronous writes. */
    
    +     void *writeBuffer;             /* Buffer for async write data. */
    
          struct TcpState *nextPtr;    /* The next socket on the global socket
    
                                        * list. */
    
      } TcpState;
    
    ***************
    
    *** 1210,1217 ****
    
                if (toWrite < amount) {
    
                    amount = toWrite;
    
                }
    
                statePtr->dataSegment[0].length = amount;
    
    -           statePtr->dataSegment[0].ptr = buf;
    
                statePtr->dataSegment[1].length = 0;
    
                InitMacTCPParamBlock(&statePtr->pb, TCPSend);
    
                statePtr->pb.ioCompletion = completeUPP;
    
    --- 1213,1234 ----
    
                if (toWrite < amount) {
    
                    amount = toWrite;
    
                }
    
    +
    
    +           /* We need to copy the data, otherwise the caller may overwrite
    
    +              * the buffer in the middle of our asynchronous call
    
    +              */
    
    +           if (amount>statePtr->writeBufferSize) {
    
    +               /* need to grow write buffer */
    
    +               if (statePtr->writeBuffer!=(void *)NULL) {
    
    +                   ckfree(statePtr->writeBuffer);
    
    +               }
    
    +               statePtr->writeBuffer = (void *)ckalloc(amount);
    
    +               statePtr->writeBufferSize = amount;
    
    +           }
    
    +           memcpy(statePtr->writeBuffer, buf, amount);
    
    +           statePtr->dataSegment[0].ptr = statePtr->writeBuffer;
    
    +
    
                statePtr->dataSegment[0].length = amount;
    
                statePtr->dataSegment[1].length = 0;
    
                InitMacTCPParamBlock(&statePtr->pb, TCPSend);
    
                statePtr->pb.ioCompletion = completeUPP;
    
    ***************
    
    *** 1479,1484 ****
    
    --- 1496,1503 ----
    
          statePtr->watchMask = 0;
    
          statePtr->acceptProc = (Tcl_TcpAcceptProc *) NULL;
    
          statePtr->acceptProcData = (ClientData) NULL;
    
    +     statePtr->writeBuffer = (void *)NULL;
    
    +     statePtr->writeBufferSize = 0;
    
          statePtr->nextPtr = socketList;
    
          socketList = statePtr;
    
          return statePtr;
    
    ***************
    
    *** 1515,1520 ****
    
    --- 1534,1542 ----
    
                    break;
    
                }
    
            }
    
    +     }
    
    +     if (statePtr->writeBuffer!=(void *)NULL) {
    
    +       ckfree(statePtr->writeBuffer);
    
          }
    
          ckfree((char *) statePtr);
    
      }
    
    
    
    
    
    
    
    PatchFiles:
    
    tclMacSock.c
    
    
    
    
    
    
    
    
    
    
    
  4. foundin changed to: "obsolete: 8.0.4"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "69. Other"
  10. title changed to: "Mac socket code can corrupt data on fcopy"
  11. type changed to: "Bug"