Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2004-02-07
03:29 • Ticket [405995ffff] Tcl_Ungets filling buffer wrongly status still Closed wi... artifact: 6ecade9997 user: jiangwu
2004-02-03
18:14 • Closed ticket [405995ffff]. artifact: 3ec0071ad7 user: dkf
18:14 • Open ticket [405995ffff]. artifact: 17ab7930d1 user: dkf
05:00 • Ticket [405995ffff]: 4 changes artifact: 8865bb97ab user: davygrvy
04:56 • Ticket [405995ffff]: 4 changes artifact: 97bea819dc user: dgp
04:42 • Ticket [405995ffff]: 4 changes artifact: 693784f873 user: hobbs
04:35 • Closed ticket [405995ffff]. artifact: 4cbad4271d user: davygrvy
04:31 • Ticket [405995ffff]: 4 changes artifact: 80796a4133 user: davygrvy
04:16 • Ticket [405995ffff]: 1 change artifact: 326e895f0e user: davygrvy
02:41 • Ticket [405995ffff]: 4 changes artifact: 46a22ff0c8 user: jiangwu
2001-07-20
01:24 • Ticket [405995ffff]: 4 changes artifact: 9376a32dd4 user: andreas_kupries
2001-07-17
01:51 • Ticket [405995ffff]: 1 change artifact: 048f8cfe9c user: andreas_kupries
2001-03-05
11:34 • New ticket [405995ffff]. artifact: a39b4c37da user: nobody

Tcl_Ungets filling buffer wrongly

Closed / Fixed405995ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 23 years ago
Descriptionplain2001-03-05 11:34:35
Tcl_Ungets puts the bytes into the beginning of the 
buffer, not staring at bufPtr->nextAdded.

diff -r tcl8.3.2/generic/tclIO.c 
tcl8.3.2.new/generic/tclIO.c
4553c4553
<         bufPtr->buf[i] = str[i];
---
>         bufPtr->buf[bufPtr->nextAdded++] = str[i];
4555d4554
<     bufPtr->nextAdded += len;
andreas_kupries Ticket [405995ffff]: 1 change [History diff]2001-07-17 01:51:46
andreas_kupriesplain2001-07-20 01:24:31
Logged In: YES 
user_id=75003

Doesn't this have to take "atEnd" into account too ?
jiangwuplain2004-02-03 02:41:54
Logged In: YES 
user_id=966384

I tried this patch on 8.4.4 and it seems to work.  Here is the 
diff against 8.4.4:

cvs diff -u tclIO.c
Index: tclIO.c
==============================================
=====================
RCS file: /home/cvs/starvine/cvsroot/tcl/generic/tclIO.c,v
retrieving revision 1.2
diff -u -r1.2 tclIO.c
--- tclIO.c     11 Oct 2003 07:02:47 -0000      1.2
+++ tclIO.c     2 Feb 2004 19:36:55 -0000
@@ -5041,9 +5041,8 @@

     bufPtr = AllocChannelBuffer(len);
     for (i = 0; i < len; i++) {
-        bufPtr->buf[i] = str[i];
+        bufPtr->buf[bufPtr->nextAdded++] = str[i];
     }
-    bufPtr->nextAdded += len;

     if (statePtr->inQueueHead == (ChannelBuffer *) NULL) {
         bufPtr->nextPtr = (ChannelBuffer *) NULL;
davygrvy Ticket [405995ffff]: 1 change [History diff]2004-02-03 04:16:53
davygrvyplain2004-02-03 04:31:20
Logged In: YES 
user_id=7549

atEnd is handled by the splice operation below this code.

Fix applied to HEAD: tclIO.c -r1.70
davygrvy Closed ticket [405995ffff]. [History diff]2004-02-03 04:35:34
hobbsplain2004-02-03 04:42:08
Logged In: YES 
user_id=72656

If the fix is correct, why not backport it to 8.4 as well?
dgpplain2004-02-03 04:56:24
Logged In: YES 
user_id=80530


Is there a test in the test suite
that fails without the fix, and
succeeds with it?
davygrvyplain2004-02-03 05:00:17
Logged In: YES 
user_id=7549

no there isn't
dkfplain2004-02-03 18:14:31
Logged In: YES 
user_id=79902

Reopening until a test is written.  Sorry to be tedious, but
it's the only way to stop silly mistakes in the future. 
(BTW, if it is awkward to test this with normal commands,
just slap some test code into tclTest.c and do it that way. :))
dkf Closed ticket [405995ffff]. [History diff]2004-02-03 18:14:55
jiangwuplain2004-02-07 03:29:42
Logged In: YES 
user_id=966384

There are no way to test this using normal Tcl command.  
Here is something I put together, which adds a "ungets" 
subcommand to "testchannel", then added a single test to 
the "io.test" to verify ungets.  This test fails without the fix 
and succeeds with the fix:

> cvs diff -u generic/tclTest.c tests/io.test
Index: generic/tclTest.c
==============================================
=====================
RCS file: /home/cvs/starvine/cvsroot/tcl/generic/tclTest.c,v
retrieving revision 1.2
diff -u -r1.2 tclTest.c
--- generic/tclTest.c   11 Oct 2003 07:02:48 -0000      1.2
+++ generic/tclTest.c   6 Feb 2004 20:26:53 -0000
@@ -291,6 +291,9 @@
 static Tcl_Channel     TestOpenFileChannelProc3 
_ANSI_ARGS_((
                            Tcl_Interp *interp, CONST char 
*fileName,
                            CONST char *modeString, int 
permissions));
+static int      UngetsChannelProc _ANSI_ARGS_((
+                Tcl_Interp *interp, Tcl_Channel channel,
+                CONST char *input, CONST char *addAtEnd));
 static int             TestpanicCmd _ANSI_ARGS_((ClientData 
dummy,
                            Tcl_Interp *interp, int argc, CONST char 
**argv));
 static int             TestparserObjCmd _ANSI_ARGS_
((ClientData dummy,
@@ -5158,6 +5161,46 @@
        return (NULL);
     }
 }
+
+
+/*
+ *--------------------------------------------------------
--------------
+ *
+ * UngetsChannelProc  --
+ *
+ *     Implements the "ungets ..." subcommand in 
TestChannelCmd.  Calls
+ *  the "Tcl_Ungets" C API with the given arguments.
+ *
+ * Results:
+ *     A integer value, as returned by Tcl_Ungets.
+ *
+ * Side effects:
+ *     None.
+ *
+ *--------------------------------------------------------
--------------
+ */
+static int
+UngetsChannelProc(interp, channel, input, addAtEnd)
+    Tcl_Interp  *interp;                /* Interpreter for error 
reporting */
+    Tcl_Channel channel;                /* Channel to push the 
data back */
+    CONST char  *input;                 /* String to push back  
*/
+    CONST char  *addAtEnd;              /* A integer flag, 1 or 
0 for whether
+                                           the string should be pushed 
back to
+                                           the end or the beginning */
+{
+    int inputLen = strlen(input);
+    int len, addAtEndFlag;
+
+    if (Tcl_GetInt(interp, addAtEnd, &addAtEndFlag) == 
TCL_ERROR) {
+        Tcl_AppendResult(interp, "expecting an integer flag for 
\"addAtEnd\"",
+                         (char *) NULL);
+        return TCL_ERROR;
+    }
+    len = Tcl_Ungets(channel, input, inputLen, addAtEndFlag);
+    Tcl_SetObjResult(interp, Tcl_NewIntObj(len));
+
+    return TCL_OK;
+}
 &#9792;
 /*
  *---------------------------------------------------------
-------------
@@ -5580,9 +5623,23 @@
        return Tcl_UnstackChannel(interp, chan);
     }

+    if ((cmdName[0] == 'u') && (strncmp(cmdName, "ungets", 
len) == 0)) {
+       /*
+        * Syntax: ungets channel string
+        */
+
+        if (argc != 5) {
+            Tcl_AppendResult(interp, "wrong # args: should be 
\"", argv[0],
+                             " ungets channel string atEnd\"", (char 
*) NULL);
+            return TCL_ERROR;
+        }
+
+        return UngetsChannelProc(interp, chan, argv[3], argv
[4]);
+    }
+
     Tcl_AppendResult(interp, "bad option \"", cmdName, "\": 
should be ",
             "cut, clearchannelhandlers, info, isshared, mode, 
open, "
-           "readable, splice, writable, transform, unstack",
+           "readable, splice, writable, transform, unstack, 
ungets",
             (char *) NULL);
     return TCL_ERROR;
 }
Index: tests/io.test
==============================================
=====================
RCS file: /home/cvs/starvine/cvsroot/tcl/tests/io.test,v
retrieving revision 1.2
diff -u -r1.2 io.test
--- tests/io.test       11 Oct 2003 07:02:50 -0000      1.2
+++ tests/io.test       6 Feb 2004 20:27:00 -0000
@@ -7109,8 +7109,19 @@
     list $x $result
 } {1 {gets {} catch {error writing "stdout": invalid 
argument}}}

+# Test Tcl_Ungets

-
+test io-61.1 {testing Tcl_Ungets, push data back at 
beginning} {testchannel} {
+    set f [open $path(test1) w]
+    puts $f "123456789"
+    close $f
+    set f [open $path(test1) r]
+    gets $f
+    testchannel ungets $f "123" 0
+    set x [gets $f]
+    close $f
+    set x
+} {123}

 # cleanup
 foreach file [list fooBar longfile script output test1 pipe 
my_script foo \
Log in (anonymous is fine) to add a comment.