delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2001/11/14/05:06:09

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sources DOT redhat DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sources DOT redhat DOT com>
List-Help: <mailto:cygwin-help AT sources DOT redhat DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT sources DOT redhat DOT com
Delivered-To: mailing list cygwin AT sources DOT redhat DOT com
Message-ID: <3BF24170.71D1CADF@syntrex.com>
Date: Wed, 14 Nov 2001 11:03:28 +0100
From: Pavel Tsekov <ptsekov AT syntrex DOT com>
X-Mailer: Mozilla 4.76 [en] (X11; U; Linux 2.4.2-2 i686)
X-Accept-Language: en
MIME-Version: 1.0
To: Shun-ichi GOTO <gotoh AT taiyo DOT co DOT jp>
CC: rreitz AT mssinternet DOT com, cygwin AT cygwin DOT com
Subject: Re: 1.3.5: write(2) system call fails with 0 length
References: <5 DOT 1 DOT 0 DOT 14 DOT 2 DOT 20011113215143 DOT 03327810 AT pop DOT mssinternet DOT com> <20011114 DOT 135353 DOT 111406103 DOT gotoh AT taiyo DOT co DOT jp>

--------------1463BCC87CB0C48DEB95DDE7
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Shun-ichi GOTO wrote:
> 
> >>>>> at Tue, 13 Nov 2001 21:58:32 -0600
> >>>>> Randy Reitz <rreitz AT mssinternet DOT com> said,>
> 
> > Here is a test program....
> 
> It's strange test.
> Do like this, and get result: "2nd write() failed: Bad address".
> So you should give valid pointer as 2nd argument of 2nd write() call.
> for example, write (fd, buf, 0);
> 

It's not so strange if you think about it :) If a length of 0 is given 
there is no reason to check the pointer for being valid IMO.

However here is what the linux man page says about write - havent
checked
any other source which may be more confident though:

RETURN VALUE
       On  success,  the  number of bytes written are returned (zero
indicates nothing was written).
       On error, -1 is returned, and errno is set appropriately.  If
count is zero and the file
       descriptor refers to a regular file, 0 will  be  returned without
causing any other effect.
       For a special file, the results are not portable.

So at least linux just checks for valid fd when the len is 0.

And the testcase which Randy posted is just fine when run on Linux of
course
if the file exists.

[ptsekov AT enigma Src]$ ~/Src/write_test
ret=0, errno=0

If the file does not exists at the time the testcase is run i get:

[ptsekov AT enigma Src]$ ~/Src/write_test
ret=-1, errno=9 (Bad file descriptor)


Attached is a simple patch which moves the buffer check after the 
check for valid file descriptor and the check for zero len.


2001-11-14  Pavel Tsekov  <ptsekov AT syntrex DOT com>

* syscalls.cc (_write): Check if the third argument of _write() is zero.
If the first argument of _write() is a valid file descriptor and the
third
argument is zero - return 0 without doing anything else.
Perform a validation check on the second argument of _write() only if
the
_write() is requested on a valid file descriptor and the length of the
buffer being written is not zero.
--------------1463BCC87CB0C48DEB95DDE7
Content-Type: text/plain; charset=us-ascii;
 name="syscalls.cc.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="syscalls.cc.diff"

--- syscalls.cc.ORIG	Wed Nov 14 10:33:03 2001
+++ syscalls.cc	Wed Nov 14 10:36:41 2001
@@ -355,15 +355,21 @@ _read (int fd, void *ptr, size_t len)
 extern "C" ssize_t
 _write (int fd, const void *ptr, size_t len)
 {
-  if (__check_invalid_read_ptr_errno (ptr, len))
-    return -1;
-
   int res = -1;
 
   sigframe thisframe (mainthread);
   cygheap_fdget cfd (fd);
   if (cfd < 0)
     goto done;
+
+  if (len == 0)
+    {
+      res = 0;
+      goto done;
+    }
+
+  if (__check_invalid_read_ptr_errno (ptr, len))
+    return -1;
 
   /* Could block, so let user know we at least got here.  */
   if (fd == 1 || fd == 2)


--------------1463BCC87CB0C48DEB95DDE7
Content-Type: text/plain; charset=us-ascii

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/
--------------1463BCC87CB0C48DEB95DDE7--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019