All of lore.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: "Thomas Weißschuh" <thomas@t-8ch.de>
Cc: Yuan Tan <tanyuan@tinylab.org>,
	falcon@tinylab.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 2/2] selftests/nolibc: add testcase for pipe.
Date: Sun, 30 Jul 2023 05:33:43 +0200	[thread overview]
Message-ID: <20230730033343.GB7339@1wt.eu> (raw)
In-Reply-To: <27bd9bc1-e7a5-4a81-91c9-2642feabb7ce@t-8ch.de>

On Sun, Jul 30, 2023 at 12:17:24AM +0200, Thomas Weißschuh wrote:
> > +	case 0:
> > +		close(pipefd[0]);
> > +		write(pipefd[1], msg, strlen(msg));
> 
> Isn't this missing to write trailing the 0 byte?

It depends if the other side expects to get the trailing 0.
In general it's better to avoid sending it since it's only
used for internal representation, and the other side must
be prepared to receive anything anyway.

> Also check the return value.

Indeed!

> > +		close(pipefd[1]);
> 
> Do we need to close the pipefds? The process is exiting anyways.

It's better to, because we could imagine looping over the tests for
example. Thus each test shoulld have as little impact as possible
on other tests.

> > +		exit(EXIT_SUCCESS);
> > +
> > +	default:
> > +		close(pipefd[1]);
> > +		read(pipefd[0], buf, 32);
> 
> Use sizeof(buf). Check return value == strlen(msg).
> 
> > +		close(pipefd[0]);
> > +		wait(NULL);
> 
> waitpid(pid, NULL, 0);
> 
> > +
> > +		if (strcmp(buf, msg))
> > +			return 1;
> > +		return 0;
> 
> return !!strcmp(buf, msg);

In fact before that we need to terminate the output buffer. If for any
reason the transfer fails (e.g. the syscall fails or transfers data at
another location or of another length, we could end up comparing past
the end of the buffer. Thus I suggest adding this immediately after the
read():

		buf[sizeof(buf) - 1] = 0;

Willy

  reply	other threads:[~2023-07-30  3:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-25 18:01 [PATCH 0/2] tools/nolibc: add pipe() and its testcase Yuan Tan
2023-07-25 18:01 ` [PATCH 1/2] tools/nolibc: add pipe() support Yuan Tan
2023-07-28 15:50   ` Zhangjin Wu
2023-07-28 19:17     ` Willy Tarreau
2023-07-29  8:37       ` Zhangjin Wu
2023-07-29 10:04         ` Willy Tarreau
2023-07-25 18:01 ` [PATCH 2/2] selftests/nolibc: add testcase for pipe Yuan Tan
2023-07-29 22:17   ` Thomas Weißschuh
2023-07-30  3:33     ` Willy Tarreau [this message]
2023-07-30  6:55       ` Thomas Weißschuh
2023-07-30  7:12         ` Willy Tarreau
2023-07-30  8:07           ` Thomas Weißschuh
2023-07-30 11:08             ` Willy Tarreau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230730033343.GB7339@1wt.eu \
    --to=w@1wt.eu \
    --cc=falcon@tinylab.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=tanyuan@tinylab.org \
    --cc=thomas@t-8ch.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.