Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp3129015rwb; Sun, 30 Jul 2023 00:21:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlGnnUenYVs05mr2yCw+26vLUk1l7Zo5MUIlgICP2PPTbwxZNXAdnyV2rkm+TEtmRNKIcaOo X-Received: by 2002:a17:90a:b015:b0:268:1489:959e with SMTP id x21-20020a17090ab01500b002681489959emr5838955pjq.33.1690701716675; Sun, 30 Jul 2023 00:21:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690701716; cv=none; d=google.com; s=arc-20160816; b=VPzh0vWucWhIHa1WMqusZvB+dwlhw28LrxObckGdmKLFipisJprRobR32XepTz2ol0 Q7X0Pyul7VxF46XqtPUFNS3/tIvfgjzy1Aq8peiPh3P7YR+3ySm183UE69YqKjx2oLUj sGw5i1FP0BcPrdmsrgFlKpYoTiYlVoYA2BNCmJ/F6D5PCQ8D5ZSdChSDyNp1hkNi/2hm /l1WGWoHDN2O+kccnhtsGZcFv1OLLIW2TXtwOIN9rXbnB4rYBYVazckEFQ3YRvMbWDra 3DAY7ZQJf2FRyf5xrzZhotfGDNV57hW4B0wSqtnncO3jwkcLv9NG55QiAegc2ocv5zkT 4ulA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=ESAbnJZqdPlkO908/3bRU8cJQHxVBk1+VxtPenG9N5E=; fh=reTQtKv4WHzRhS6OElR4c5ve1y+8sIrINaYNuYT+ztw=; b=qEM5sTYtbcPBUl1/7fAoSJ+o/NhIWXzXzGCs7wZ+0kG9jId6c3OnNukdejojYkoqay 3f4//l+Im74nZXC/lKNx/2wzH3lmnQztZYXrn7uWQjSjxXQKeOB31RmGXiNEPPHlnJe8 li2VAUsEXBUzB7bu9fb0plXA47k/HoWtjioe+c9N/YqGWIu4uQh16Eh2DsL5U1qwcPT2 Z8cwv2WFQvG88aAgUKR20psZvi0T0IW+J6a8pX/nmVEZs/3HTIJ8RDsZaK2hytiqP0T/ a2g5TDasSCqdUbmnOCdBmTyNIFBf+ap8vmgaTTj90OVqPs0xdL+HuvJfOEUOgP/wHZ9z KsGQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g18-20020a170902d1d200b001bbd586d29esi5494481plb.34.2023.07.30.00.21.44; Sun, 30 Jul 2023 00:21:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229685AbjG3HMf (ORCPT + 99 others); Sun, 30 Jul 2023 03:12:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229463AbjG3HMe (ORCPT ); Sun, 30 Jul 2023 03:12:34 -0400 Received: from 1wt.eu (ded1.1wt.eu [163.172.96.212]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 79B0710FE; Sun, 30 Jul 2023 00:12:33 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 36U7CRen008095; Sun, 30 Jul 2023 09:12:27 +0200 Date: Sun, 30 Jul 2023 09:12:27 +0200 From: Willy Tarreau To: Thomas =?iso-8859-1?Q?Wei=DFschuh?= Cc: Yuan Tan , falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH 2/2] selftests/nolibc: add testcase for pipe. Message-ID: <20230730071227.GB8033@1wt.eu> References: <160ddef0313e11085ee906144d6d9678b8156171.1690307717.git.tanyuan@tinylab.org> <27bd9bc1-e7a5-4a81-91c9-2642feabb7ce@t-8ch.de> <20230730033343.GB7339@1wt.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jul 30, 2023 at 08:55:47AM +0200, Thomas Wei?schuh wrote: > On 2023-07-30 05:33:43+0200, Willy Tarreau wrote: > > 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. > > I meant the newly forked child exiting, not nolibc-test in general. > The exit is just below, so the fds in the child are close here anyways. Ah OK, but still it remains cleaner with it IMHO (i.e. better rely on explicit things in tests, that's less doubts when they fail). > > > > + 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; > > This would still access uninitialized memory and lead to UB in strcmp as > not all bytes in buf were written to by read(). > > If we want to be really sure we should use memcmp() instead of strcmp(). > For memcmp() I would prefer to transfer and check without the '\0', so > my review comments from before need to be adapted a bit. In fact you make a good point regarding the fact that the test doesn't use read()'s return value. This problem totally goes away if the return value is used, e.g.: len = read(pipefd[0], buf, sizeof(buf)); close(pipefd[0]); waitpid(pid, NULL, 0); return len < 0 || len > sizeof(buf) || len > strlen(msg) || memcmp(buf, msg, len) != 0; Willy