From nobody Thu Jun 06 17:06:43 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Vw9jH4StXz5NH7R; Thu, 06 Jun 2024 17:06:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Vw9jH2RZmz4JrW; Thu, 6 Jun 2024 17:06:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1717693603; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yyx1Xr4E/Nb52sjPs2TF3tYGNJebheY+QfHu79IrJxI=; b=h7qnxDYYoSOBWXOBNa1TmeNBcqKIsE3mXEMnW046T6nWvWoTU/rxKVI3DpfxrGMxcSvQwn gD/2H/pMXFgrjIsh80TLo2uWG6unnYPakXM+Ibfqrs7dfKJFADMPdwjhxTmnsZyPEhF60v 5vfdnEGWq24qKNduZ2NH/QsHtDZvP248wf+jX6BupHfeXyxwzwuIL6uJOoVN4y1jqi9UXW mowim+SMHrew9FKVTIvvzpHheOylAgxuJ0jhfz/WIBjbtiFS2xR9E6uu5ym/IG2riRBg24 EfdSo0GOHwmoYV8aXk7BOIfTZc3mPZ2jEVkkDAsgzTYxYd8iocz8r5/5NPIaQg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1717693603; a=rsa-sha256; cv=none; b=Qi6U2UXzQBziC/br63JvWDjKE/wWgYw47DnszeoBKN7z/VsrQSPwlokjOKSS99DhXOsaBM 5jb4W/v7RBJlFrnUQ2B9ocCQ5qZllfvSVStY6mf79YcxEexwWpTQcNp1TGR3KuEmzIkfF7 P3lwp0aSOJ7iA9BxstQm9BHT+LcJ14nOncEcJuWR8BXpKvICpND/KujWFhUqEbhqfC7SyZ NsNzPxHZzAfE+MBzur1Yusbh3seLTotKMWfh7Fkfe92BMPPcdRJXchUxv2FcNfJL/OlR8q TrrG1/QGeLuVNMfFjSlHAfy4tAyj8FkjM0i97RHB/w36gJXJvWSd+du/KEtJpQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1717693603; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yyx1Xr4E/Nb52sjPs2TF3tYGNJebheY+QfHu79IrJxI=; b=UfnyBAAhHJfnXKQfkOjIBqGMsOnvzdZGxnM6ePkspwlnZeR38EUDhxY0bnIAfcR6zS0FXd 9RlWQD6Mrkj/6IzCFu4KYVHUDJiTwqI9bPj679LQHvcN9IPclPKuzTwANzjSQnVNTz9QOx jv38CAmG4iclGlw+evLjpkNIeGiiowExIfT8Vr8Lu/MuvBWfQ41yoKwkLlBAWjxFU8AAAU d3OGTSd7cvtLX7ga99umKpsdOwrTiw+BvbGzb20hPofKJUvfM7ju2BrPrxkudbBHzyEQVu 2LP3JQs1uiL06ruXBZYwJOB+r+2cd4wtmD6YXzrMjHotMBUV6waclrTOYnNaRw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Vw9jH1qYtzvWx; Thu, 6 Jun 2024 17:06:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 456H6hdb073673; Thu, 6 Jun 2024 17:06:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 456H6ht9073670; Thu, 6 Jun 2024 17:06:43 GMT (envelope-from git) Date: Thu, 6 Jun 2024 17:06:43 GMT Message-Id: <202406061706.456H6ht9073670@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Allan Jude Subject: git: 7dd39ef4e0d5 - main - comm: close a race condition when comm is fed from stdin List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: allanjude X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7dd39ef4e0d56b213445754a189d204b70a77a00 Auto-Submitted: auto-generated The branch main has been updated by allanjude: URL: https://cgit.FreeBSD.org/src/commit/?id=7dd39ef4e0d56b213445754a189d204b70a77a00 commit 7dd39ef4e0d56b213445754a189d204b70a77a00 Author: Cosimo Cecchi AuthorDate: 2024-06-06 16:51:43 +0000 Commit: Allan Jude CommitDate: 2024-06-06 17:04:38 +0000 comm: close a race condition when comm is fed from stdin If one of the files has ended, we won't show the column, but we still need to drain the file pointer to avoid potentially hitting a pipe failure. This commit moves the NULL offset checks inside show() so that getline() and ferror() are still called on fp. Reviewed by: allanjude Sponsored by: Apple Inc. Differential Revision: https://reviews.freebsd.org/D45440 --- usr.bin/comm/comm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/usr.bin/comm/comm.c b/usr.bin/comm/comm.c index 2f305e23ce8f..4dcd7cc9a2dd 100644 --- a/usr.bin/comm/comm.c +++ b/usr.bin/comm/comm.c @@ -129,12 +129,12 @@ main(int argc, char *argv[]) /* if one file done, display the rest of the other file */ if (n1 < 0) { - if (n2 >= 0 && col2 != NULL) + if (n2 >= 0) show(fp2, argv[1], col2, &line2, &line2len); break; } if (n2 < 0) { - if (n1 >= 0 && col1 != NULL) + if (n1 >= 0) show(fp1, argv[0], col1, &line1, &line1len); break; } @@ -206,10 +206,12 @@ show(FILE *fp, const char *fn, const char *offset, char **bufp, size_t *buflenp) ssize_t n; do { - (void)printf("%s%s\n", offset, *bufp); + /* offset is NULL when draining fp, not printing */ + if (offset != NULL) + (void)printf("%s%s\n", offset, *bufp); if ((n = getline(bufp, buflenp, fp)) < 0) break; - if (n > 0 && (*bufp)[n - 1] == '\n') + if (n > 0 && offset != NULL && (*bufp)[n - 1] == '\n') (*bufp)[n - 1] = '\0'; } while (1); if (ferror(fp))