From nobody Fri Jul 07 21:56:56 2023 X-Original-To: dev-commits-src-main@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 4QyS0n2Dzqz4mBgh; Fri, 7 Jul 2023 21:56:57 +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 4QyS0n1n4Gz3H4F; Fri, 7 Jul 2023 21:56:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688767017; 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=RWrlD4d1zeenR/3HEplyH4dJnZR+FHD3IzpL2pfFaJU=; b=CaNQhjGjo5Wl24Uiv+suWpV0UaKSEwakcYSybTqykYHT4VbFerWT+hEXSJYS54p55Rci5l ehCMxlerdo+EgP444k1K11+oGl5T62o4Td7YETFd7nmiDs54zJ2Sc+RvXSjEeFwWb+UXBv 076mupciBJ+oUNOV2HJIveZUcMMXC1a1TFM4Q5EHOPannbHxo0TFm9fLbCILKYUtRoMb5q J44N2LmC0YqBHz95rns4EMUsYx4BXoJ7x+q5P3829eywb3C7UIBYrUxeFYl5pvqQDzQZyo ueifwfw5p3MpNp9xTQ2uahJAuAyw2jsBa9A2BvuIXJPFG796jGcixL3GH2lWbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688767017; 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=RWrlD4d1zeenR/3HEplyH4dJnZR+FHD3IzpL2pfFaJU=; b=VED8DUWW2bR/KqQRPZgUQoKG2dxtGonSdx++VeV8/zsYdYWcxN8rvHTgdUDURhvKplgpKq 44B/8A3jmI3yixSos+zQbL3tb4W0CAlxUgAibKy5jWE5dBuBhU/5l/i5+9Ejo2et6KP9Ds IZl1ZmM2oVWJEeaGNGWaKAaQ0Qcner/t5kcFcNrpysfdtyeBuM5uy3xGqEppQklOcVOvB4 Z15OCbL22+oo7MsnqJPebtSpAukiWHIWVubsoPHYeVg3inn0gxW8qfR8onXPnMBif23iao MMrwkA24lxV+GMsFQBU9ahzlPXDXR63T/KAWA5mK4vp3VI5KlGzBoWl/3U4GRQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1688767017; a=rsa-sha256; cv=none; b=LU3pZI9VEp/L3Zpms1FmDFMERsvumDDeGZlERfvMBFg52oH44ts2JQEdlMUX7OWVLhWmp+ g8b4lljFdZ2vvkW8CGifOrT6c1WNFUH8g4ldLBri2uD3oOXzH5cyPsuIa2aHsnqtYz9GGA SVHn0HFThAAMZ52wuLFQthdUQterN/0glTguLwbhuyfBoE040EBg7ghwvQcZUxL01ttBlr gNdwSiogK/IUjN9UrEpev3ai/xSLyL9560nxAKMqM5mnyB/vFHqckdLcpCJv8wDPspYilb tQ4E6YF25QGlTrTAknA5pmMUo9XFx6knlvw2mPv9t4wchq0ptTO/r6wpyxaegw== 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 4QyS0n0WmqzvW7; Fri, 7 Jul 2023 21:56:57 +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 367LuuZW010956; Fri, 7 Jul 2023 21:56:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 367Luuh0010955; Fri, 7 Jul 2023 21:56:56 GMT (envelope-from git) Date: Fri, 7 Jul 2023 21:56:56 GMT Message-Id: <202307072156.367Luuh0010955@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 38f57faa06cf - main - cat: ensure serrno is always set List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 38f57faa06cf1b96568eac6cb270c1748f3fde43 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=38f57faa06cf1b96568eac6cb270c1748f3fde43 commit 38f57faa06cf1b96568eac6cb270c1748f3fde43 Author: Alfonso Gregory AuthorDate: 2023-07-07 21:54:26 +0000 Commit: Warner Losh CommitDate: 2023-07-07 21:56:35 +0000 cat: ensure serrno is always set Set serrno to errno before the loop begins for the edge case that res0 is immediately NULL. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/724 --- bin/cat/cat.c | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/bin/cat/cat.c b/bin/cat/cat.c index 5a1fab0c26a0..a58bbe93835e 100644 --- a/bin/cat/cat.c +++ b/bin/cat/cat.c @@ -436,7 +436,6 @@ udom_open(const char *path, int flags) */ bzero(&hints, sizeof(hints)); hints.ai_family = AF_LOCAL; - fd = -1; if (fileargs_realpath(fa, path, rpath) == NULL) return (-1); @@ -449,6 +448,10 @@ udom_open(const char *path, int flags) } cap_rights_init(&rights, CAP_CONNECT, CAP_READ, CAP_WRITE, CAP_SHUTDOWN, CAP_FSTAT, CAP_FCNTL); + + /* Default error if something goes wrong. */ + serrno = EINVAL; + for (res = res0; res != NULL; res = res->ai_next) { fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); @@ -471,39 +474,40 @@ udom_open(const char *path, int flags) else { serrno = errno; close(fd); - fd = -1; } } freeaddrinfo(res0); + if (res == NULL) { + errno = serrno; + return (-1); + } + /* * handle the open flags by shutting down appropriate directions */ - if (fd >= 0) { - switch(flags & O_ACCMODE) { - case O_RDONLY: - cap_rights_clear(&rights, CAP_WRITE); - if (shutdown(fd, SHUT_WR) == -1) - warn(NULL); - break; - case O_WRONLY: - cap_rights_clear(&rights, CAP_READ); - if (shutdown(fd, SHUT_RD) == -1) - warn(NULL); - break; - default: - break; - } - cap_rights_clear(&rights, CAP_CONNECT, CAP_SHUTDOWN); - if (caph_rights_limit(fd, &rights) < 0) { - serrno = errno; - close(fd); - errno = serrno; - return (-1); - } - } else { + switch (flags & O_ACCMODE) { + case O_RDONLY: + cap_rights_clear(&rights, CAP_WRITE); + if (shutdown(fd, SHUT_WR) == -1) + warn(NULL); + break; + case O_WRONLY: + cap_rights_clear(&rights, CAP_READ); + if (shutdown(fd, SHUT_RD) == -1) + warn(NULL); + break; + default: + break; + } + + cap_rights_clear(&rights, CAP_CONNECT, CAP_SHUTDOWN); + if (caph_rights_limit(fd, &rights) < 0) { + serrno = errno; + close(fd); errno = serrno; + return (-1); } return (fd); }