Le 22/03/16 18:48, Rich Teer a écrit :
> On Tue, 22 Mar 2016, Richard PALO wrote:
> 
>> What I'm seeing is an infinite loop of the following (using /usr/bin/head):
>>> truss  -u libc head -c 1 /dev/zero
>>> ...
>>> /1@1:       -> libc:fgets(0x8047680, 0x400, 0x80622b0, 0x0)
>>> /1: read(3, "\0\0\0\0\0\0\0\0\0\0\0\0".., 8192)     = 8192
>>> /1@1:       <- libc:fgets() = 0x8047680
>>> /1@1:       -> libc:strlen()
>>> /1@1:       <- libc:strlen() = 0
>>> /1@1:       -> libc:printf()
>>> /1@1:       <- libc:printf() = 0
> 
> Hmm, if I had to hazard a guess, I say that head is using the return
> value from either strlen() or printf() to count the number of characters.
> Given an infinite stream of zeros, both of these functions would always
> return 0, so head's termination claues of 1 character (-c 1) is never met.
> 
> I'd say it's a bug in head, which should presumably use the return value
> from read() to keep track of how many bytes it has read.
> 


exactly, the strlen() @ 
http://src.illumos.org/source/xref/illumos-gate/usr/src/cmd/head/head.c#180 is 
not correct:
> 
> 173static void
> 174copyout(off_t cnt, int isline)
> 175{
> 176   char lbuf[BUFSIZ];
> 177   size_t len;
> 178
> 179   while (cnt > 0 && fgets(lbuf, sizeof (lbuf), input) != 0) {
> 180           len = strlen(lbuf);
> 181           if (isline) {
> 182                   (void) printf("%s", lbuf);
> 183                   /*
> 184                    * only count as a line if buffer read ends with newline
> 185                    */
> 186                   if (len > 0) {
> 187                           if (lbuf[len - 1] == '\n') {
> 188                                   (void) fflush(stdout);
> 189                                   cnt--;
> 190                           }
> 191                   }
> 192           } else {
> 193                   if (len > cnt) {
> 194                           lbuf[cnt] = '\0';
> 195                           len = cnt;
> 196                   }
> 197                   (void) printf("%s", lbuf);
> 198                   cnt -= len;
> 199                   (void) fflush(stdout);
> 200           }
> 201   }
> 202}

This should probably be rewritten to use fread().

-- 
Richard PALO



-------------------------------------------
illumos-discuss
Archives: https://www.listbox.com/member/archive/182180/=now
RSS Feed: https://www.listbox.com/member/archive/rss/182180/21175430-2e6923be
Modify Your Subscription: 
https://www.listbox.com/member/?member_id=21175430&id_secret=21175430-6a77cda4
Powered by Listbox: http://www.listbox.com

Reply via email to