9front - general discussion about 9front
 help / color / mirror / Atom feed
* pipe: bug or feature?
@ 2017-03-28  1:42 arisawa
  2017-03-28  2:27 ` [9front] " Alex Musolino
  0 siblings, 1 reply; 3+ messages in thread
From: arisawa @ 2017-03-28  1:42 UTC (permalink / raw)
  To: 9front

Hello,

I was playing with an experimental program on pipe and met with a problem which I don’t understand.

the program reads a file and writes it back to one end of pipe and then reads it from another end of pipe.
the buffer for writing pipe is named buf0, and for reading pipe is named buf.
and I found the program does not finish unless sizeof(buf) > sizeof(buf0).
is this a bug or a feature of pipe?

Kenji Arisawa

=== BEGIN a.c ===
#include <u.h>
#include <libc.h>

char *argv0;

void
usage(void)
{
	fprint(2,"usage: %s file\n",argv0);
	exits("usage");
}

void
main(int argc, char *argv[])
{
	int fd,pfd[2];
	char buf[256];
	char buf0[256];
	/* need to be sizeof(buf) > sizeof(buf0)
	 * but this condition is very curious to me */
	int n;
	char *file;
	argv0 = argv[0];
	argc--;argv++;
	USED(argc);
	if(argv[0] == nil)
		usage();
	file = argv[0];
	fd = open(file,OREAD);
	if(fd < 0)
		sysfatal("no such file");

	if(pipe(pfd) < 0)
		sysfatal("pipe error");
	print("pfd: %d %d\n",pfd[0],pfd[1]);

	while((n = read(fd,buf0,sizeof(buf0))) > 0){
		print("read: %d %s\n",n,file);
		n = write(pfd[1],buf0,n);
		print("write: %d\n",n);
	}
	close(pfd[1]);
	while((n = read(pfd[0],buf,sizeof(buf))) > 0){
		buf[n] = 0;
		print("%d %s\n",n,buf);
	}
	print("%d\n",n);
		
	exits(nil);
}
=== END a.c ===



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9front] pipe: bug or feature?
  2017-03-28  1:42 pipe: bug or feature? arisawa
@ 2017-03-28  2:27 ` Alex Musolino
  2017-03-28  3:33   ` arisawa
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Musolino @ 2017-03-28  2:27 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 333 bytes --]

>
> is this a bug or a feature of pipe?
>

It is a bug in your code.  When you do buf[n] = 0 in the last while
loop, you are writing past the end of buf.  This causes pfd[0] to
become 0 and so the next read is actually from stdin.  Making buf
larger "fixes" the problem because n=256 becomes a valid index.

--
Cheers,
Alex Musolino

[-- Attachment #2: Type: text/html, Size: 687 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9front] pipe: bug or feature?
  2017-03-28  2:27 ` [9front] " Alex Musolino
@ 2017-03-28  3:33   ` arisawa
  0 siblings, 0 replies; 3+ messages in thread
From: arisawa @ 2017-03-28  3:33 UTC (permalink / raw)
  To: 9front

OK! you are right!

I was careless.
thanks.

Kenji Arisawa

> 2017/03/28 11:27、Alex Musolino <musolinoa@gmail.com> のメール:
> 
> is this a bug or a feature of pipe?
> 
> It is a bug in your code.  When you do buf[n] = 0 in the last while
> loop, you are writing past the end of buf.  This causes pfd[0] to
> become 0 and so the next read is actually from stdin.  Making buf
> larger "fixes" the problem because n=256 becomes a valid index.
> 
> --
> Cheers,
> Alex Musolino



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-03-28  3:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-28  1:42 pipe: bug or feature? arisawa
2017-03-28  2:27 ` [9front] " Alex Musolino
2017-03-28  3:33   ` arisawa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).