Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2002-03-26
01:29 • Ticket [465765ffff] zero-length write considered harmful status still Closed... artifact: 7fb81c611e user: andreas_kupries
00:59 • Ticket [465765ffff]: 6 changes artifact: c9f287b38c user: andreas_kupries
2002-02-05
06:52 • Closed ticket [465765ffff]. artifact: fdaedb5890 user: andreas_kupries
2002-02-02
10:32 • Ticket [465765ffff]: 4 changes artifact: 356d2adb85 user: nobody
2002-02-01
05:29 • Ticket [465765ffff]: 4 changes artifact: 56e9523e81 user: andreas_kupries
05:24 • Ticket [465765ffff]: 4 changes artifact: 8d00ad9c04 user: dgp
04:55 • Ticket [465765ffff]: 4 changes artifact: 186b26832f user: andreas_kupries
04:48 • Ticket [465765ffff]: 4 changes artifact: bbc5ef2aa5 user: dgp
04:38 • Ticket [465765ffff]: 4 changes artifact: 6c1cc66a41 user: andreas_kupries
04:34 • Ticket [465765ffff]: 4 changes artifact: b61bea772d user: dgp
04:28 • Ticket [465765ffff]: 4 changes artifact: 9f9422cc8e user: andreas_kupries
04:18 • Ticket [465765ffff]: 4 changes artifact: e2da35393c user: dgp
2001-09-29
00:55 • Ticket [465765ffff]: 4 changes artifact: 7bcd7c74b1 user: andreas_kupries
00:53 • Ticket [465765ffff]: 4 changes artifact: 073ddf574f user: andreas_kupries
00:51 • Ticket [465765ffff]: 4 changes artifact: 1a4705e6af user: dgp
00:34 • Ticket [465765ffff]: 4 changes artifact: 5b25d837aa user: hobbs
00:31 • Ticket [465765ffff]: 4 changes artifact: 85f9e5655c user: andreas_kupries
00:28 • Ticket [465765ffff]: 4 changes artifact: 9a92de1305 user: andreas_kupries
00:28 • Ticket [465765ffff]: 4 changes artifact: de561baa91 user: andreas_kupries
00:21 • Ticket [465765ffff]: 4 changes artifact: 37f39ff3fc user: andreas_kupries
2001-09-28
07:03 • Ticket [465765ffff]: 5 changes artifact: 6f13699d1d user: andreas_kupries
04:02 • Ticket [465765ffff]: 4 changes artifact: 37c2a599bd user: dgp
03:26 • Ticket [465765ffff]: 5 changes artifact: 2a8a0ddfe5 user: andreas_kupries
02:18 • Ticket [465765ffff]: 6 changes artifact: b58bce329c user: vincentdarley
2001-09-27
19:10 • New ticket [465765ffff]. artifact: 4890c3ff0b user: daletalcott

zero-length write considered harmful

Closed / Fixed465765ffffffffffffffffffffffffffffffffffCreated 25 years agoUpdated 25 years ago
Descriptionplain2001-09-27 19:10:06
Tcl 8.3.3, OS Platform AIX 4.3.3

There is an unfortunate interaction between tcl and 
Unix systems that implement the Single Unix 
specification.  In particular, the Single Unix 
specification for writing to/reading from a STREAM 
device requires that a zero-length write on one 
side show up as a zero-length read on the other.  
That is, a zero-length write gets interpreted as EOF 
when read.

http://www.opengroup.org/onlinepubs/007908799/
xsh/write.html
http://www.opengroup.org/onlinepubs/007908799/
xsh/read.html

This is a problem for tcl because when tcl flushes 
its channel buffers, if there is nothing in the buffer, 
tcl ends up performing a zero-length write.  If the 
channel happens to be connected to a STREAMS 
file, this appears as EOF to the other side of the 
STREAM.

This causes grief for several programs, including 
script, sshd, and expect.  It has the interesting 
effect that, if you are logged on via SSH and run the 
tcl test suite, you get logged off! during io.test.

Fortunately, there are not that many OS's that 
implement pseudo-tty's on STREAMS, but IBM's 
AIX 4.3.3 is one.

One can argue that the spec is wrong for changing 
what used to be a harmless action (writing 
nothing) into something that now has a meaning.  
However, this is probably a losing battle.  I suggest 
that tcl be modified to avoid zero-length writes.

The following patch changes the Unix 
FileOutputProc to avoid zero-length writes:

--- unix/DRT/tclUnixChan.c      Tue Apr  3 17:54:39 
2001
+++ unix/tclUnixChan.c  Thu Sep 27 14:04:58 2001
@@ -423,6 +423,8 @@
     int written;
 
     *errorCodePtr = 0;
+    if (toWrite == 0)
+       return 0;                       /* avoid zero-length write 
*/
     written = write(fsPtr->fd, buf, (size_t) toWrite);
     if (written > -1) {
         return written;

This has been tested only to the extent that the tcl 
tests now run on AIX without logging me off.
vincentdarleyplain2001-09-28 02:18:41
Logged In: YES 
user_id=32170

This is really a channels problem, and not in my area of 
understanding or expertise.  Assigned to Andreas.
andreas_kupriesplain2001-09-28 03:26:42
Logged In: YES 
user_id=75003

I believe that this is something the drivers should not 
need to be aware about. In other words, I believe that this 
should be fixed in the generic part of the I/O system.

I'll try this and report back.
dgpplain2001-09-28 04:02:34
Logged In: YES 
user_id=80530

Ah.  So that explains it!  And here I was
blaming a bug in SSH.

I've also been bitten by this, trying to
work with Tcl through SSH on an AIX machine.

I defer to AK's judgment on the solution.
andreas_kupriesplain2001-09-28 07:03:27
Logged In: YES 
user_id=75003

Don, after doing a simple patch to avoid 0-write's I get 
segfaults on the AS linux box too. I will see later if the 
same happens on sparky and my system at home.

Stacktrace:

0  0x40095e8b in chunk_alloc (ar_ptr=0x4012ad40, nb=32) at 
malloc.c:2875
#1  0x400955ae in __libc_malloc (bytes=21) at malloc.c:2696
#2  0x80f50c9 in TclpAlloc (nbytes=21)
    at ../tcl/unix/../generic/tclAlloc.c:672
#3  0x80686dc in Tcl_Alloc (size=21)
    at ../tcl/unix/../generic/tclCkalloc.c:983
#4  0x80a33ec in AllocStringEntry (tablePtr=0x8160958, 
keyPtr=0x8158460)
    at ../tcl/unix/../generic/tclHash.c:927
#5  0x80a2d28 in Tcl_CreateHashEntry (tablePtr=0x8160958,
    key=0x8158460 "http", newPtr=0xbfffaf4c)
    at ../tcl/unix/../generic/tclHash.c:450
#6  0x80d7dde in TclSetElementOfIndexedArray 
(interp=0x810fa78,
    localIndex=3, elemPtr=0x818e848, newValuePtr=0x819ca98, 
flags=512)
    at ../tcl/unix/../generic/tclVar.c:1746
#7  0x808f68a in TclExecuteByteCode (interp=0x810fa78, 
codePtr=0x816cde8)
    at ../tcl/unix/../generic/tclExecute.c:1251
#8  0x806479a in Tcl_EvalObjEx (interp=0x810fa78, 
objPtr=0x81952c8,
    flags=0) at ../tcl/unix/../generic/tclBasic.c:2951
#9  0x80cb139 in TclObjInterpProc (clientData=0x8163eb0, 
interp=0x810fa78,
    objc=2, objv=0x8112080) 
at ../tcl/unix/../generic/tclProc.c:1075
#10 0x808d968 in TclExecuteByteCode (interp=0x810fa78, 
codePtr=0x81990a8)
    at ../tcl/unix/../generic/tclExecute.c:869
#11 0x806479a in Tcl_EvalObjEx (interp=0x810fa78, 
objPtr=0x81a2f08,
    flags=131072) at ../tcl/unix/../generic/tclBasic.c:2951
#12 0x80ad988 in TclChannelEventScriptInvoker 
(clientData=0x81a96c0,
    mask=2) at ../tcl/unix/../generic/tclIO.c:7084
#13 0x80ad319 in Tcl_NotifyChannel (channel=0x819a3e8, 
mask=2)
    at ../tcl/unix/../generic/tclIO.c:6620
#14 0x80e4452 in FileHandlerEventProc (evPtr=0x815abd0, 
flags=-3)
    at ../tcl/unix/../unix/tclUnixNotfy.c:624
andreas_kupriesplain2001-09-29 00:21:36
Logged In: YES 
user_id=75003

The seg.fault goes away with the fix for bug
[#465494] "I/O segfaults in test suite".

IOW, it was the same bug, as I suspected.
andreas_kupriesplain2001-09-29 00:28:49
Logged In: YES 
user_id=75003

Enclosed is the patch I made.

Jeff pointed out that there are possibly subtle problems 
with the patch as is: When writing to an OS channel an 
error like EPIPE can be returned.

The specification and the manpage are not clear if this can 
happen for a zero-length write too. If that is so the patch 
changes the tcl level semantics of something like [puts -
nonewline $chan ""]. Before the patch this command can 
fail, afterwards it won't (assuming a valid $chan).

This is something any patch to avoid zero-length writes can 
run into.

Opinions ?
andreas_kupriesplain2001-09-29 00:28:50
File Added - 11348: 465765.diff
andreas_kupriesplain2001-09-29 00:31:27
File Deleted - 11348: 



File Added - 11349: 465765.diff.1
hobbsplain2001-09-29 00:34:42
Logged In: YES 
user_id=72656

The enclosed patch is definitely not correct.  If you look 
at the TcpOutputProc in tclUnixChan.c, you'll see that it 
is possible for errorCode to be set even before a write is 
attempted.  Also, errorCode will be used when written == 0, 
but it was never set.  Thus, at least errorCode must also 
be set, but perhaps we are still overlooking possible valid 
side-effects (like acceptable errors returned with write 0).
dgpplain2001-09-29 00:51:45
Logged In: YES 
user_id=80530

I sometimes use [catch {puts -nonewline $socket ""}]
as a way to test that a socket connection is valid
without actually writing to it.  It sounds like this
patch will turn that command into a no-op?
andreas_kupriesplain2001-09-29 00:53:44
Logged In: YES 
user_id=75003

with regard to my own patch the handling of errorCode is 
this: In FlushChannel the errorCode is initialized to zero, 
but not in Tcl_WriteRaw. In both cases it won't be used 
for 'written == 0'.

Question: Is it possible to query the OS if a there is a 
STREAM behind a file descriptor ? If yes avoidance of zero-
length writes could be restricted to this specific 
situation.
andreas_kupriesplain2001-09-29 00:55:29
Logged In: YES 
user_id=75003

Regarding Don's question: Correct, any patch to avoid zero-
length writes (either on all channels or possibly streams-
based ones = pipes, sockets) will change the given 
statement into a full no-op.
dgpplain2002-02-01 04:18:56
Logged In: YES 
user_id=80530

Can we tackle this problem now at the VFS level?
That could prevent zero-length writes to disk
that seem to be the problem, while preserving
the zero-length write testing to sockets.
andreas_kupriesplain2002-02-01 04:28:39
Logged In: YES 
user_id=75003

How could the VFS help in this ?
dgpplain2002-02-01 04:34:22
Logged In: YES 
user_id=80530

Now that I look at it more carefully, I guess it can't.
The intersection of VFS and channels is only
Tcl_FSOpenFileChannel(), and that has no control
on zero-length writes.  I guess the responsibility
has to be pushed out to the channel drivers themselves.
andreas_kupriesplain2002-02-01 04:38:58
Logged In: YES 
user_id=75003

So you are willing to change
[catch {puts -nonewline $chan ""}]
into a true no-op, provided that chan points to a socket o 
pipe ?
dgpplain2002-02-01 04:48:21
Logged In: YES 
user_id=80530

The opposite, I think.

The {puts -nonewline $sock ""} is actually useful
for sockets for testing whether they're connected.

For other types of channels, I think I don't care
whether it's a no-op.

If I had a reliable, cross-platform, and reasonably
quick (do not suggest [fconfigure $sock -peername])
alternative way of testing socket connectedness for
writing, I might not mind that changing either.
andreas_kupriesplain2002-02-01 04:55:12
Logged In: YES 
user_id=75003

But isn't it the sockets which are STREAM based and thus 
have trouble with the zero-length write ? And thus have to 
test and make it a no-op ?
dgpplain2002-02-01 05:24:03
Logged In: YES 
user_id=80530

Let me put it this way.  If I add the line

if (toWrite == 0) return 0;

to the routine FileOutputProc() in unix/tclUnixChan.c,
then my issues on AIX systems are solved, yet I don't
think I've lost existing behavior on sockets.  (correct?)
andreas_kupriesplain2002-02-01 05:29:17
Logged In: YES 
user_id=75003

I am not sure. Can a file be based on a STREAM ?
If so, then yes, this should solve the problem.

Actually this could be what the submitter proposed
long ago.
nobodyplain2002-02-02 10:32:34
Logged In: NO 

If you want to be conservative with the fix, you
could use

if (toWrite == 0 && isatty(fsPtr->fd)) return 0;

or similar to avoid _only_ the effect of logging out
the user.  (Sockets are not ttys.)

I just lost five hours of my life on this braindamage and 
want them back.
andreas_kupriesplain2002-02-05 06:52:13
Logged In: YES 
user_id=75003

Committed to head.
andreas_kupriesplain2002-03-26 00:59:50
File Deleted - 11349: 

Logged In: YES 
user_id=75003

Deleting the attached patch because that is not the patch 
which was comitted.
andreas_kupriesplain2002-03-26 01:29:33
Logged In: YES 
user_id=75003

The submitted patch to the code can be viewed via
http://cvs.sourceforge.net/cgi-
bin/viewcvs.cgi/tcl/tcl/unix/tclUnixChan.c.diff?
r1=text&tr1=1.27&r2=text&tr2=1.28&diff_format=h
Log in (anonymous is fine) to add a comment.