From nobody Mon Mar 14 15:48:23 2022 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 3FAD71A1749A; Mon, 14 Mar 2022 15:48:24 +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 4KHLY416Bhz3vt1; Mon, 14 Mar 2022 15:48:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647272904; 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=KzyiFrrqhNkz3k/Fk6hutJ71WnpeVr6qWrBe9kdaXC8=; b=pwuBoo/jIw79ep+e7eyt/G8skwk4mqmsYiE7W/NHfdi7PjT99aD3oUGmIGjM0lQD9v8JpQ ic5NgV2PMKTNnRVGRctNOyRod8zckAXeD2GJ9NXy8xTVUlaQWbM5Foid8ZP9rgHjLDefpu QqMFvyXPq/iD03BZ8J/feNgrD0WTnP0R/8bzfjkZ189MSTnmjMdNiXrhrsfE5O0WZ08OHI wsN62cfa497ArjwLpd7WW5T4cqLqwRhDVgp1PIM8t/sLZlF4y3Do6DE5k9xir2z6XD76KP aM3w6jp9n1Q6Mm1UbdU29JcRPtj7xzQ71P3q2kY0tDu+pW9V5Zlwg6//T9oIOA== 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 036FE105B0; Mon, 14 Mar 2022 15:48:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 22EFmNNS033372; Mon, 14 Mar 2022 15:48:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22EFmNgw033371; Mon, 14 Mar 2022 15:48:23 GMT (envelope-from git) Date: Mon, 14 Mar 2022 15:48:23 GMT Message-Id: <202203141548.22EFmNgw033371@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 626c1423ee93 - releng/13.1 - libc __sfvwrite(): roll back FILE buffer pointer on fflush error 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/releng/13.1 X-Git-Reftype: branch X-Git-Commit: 626c1423ee93aa95d1d2dd23c406ba661756dfe1 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647272904; 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=KzyiFrrqhNkz3k/Fk6hutJ71WnpeVr6qWrBe9kdaXC8=; b=ix1ELslxlDq8qdaUaRdMIhAxpbTMxEYLPhNweeZ/NaK6dlStGleUuxf/q3GsY3z5eJ6Okr vj/7eOPHPvaSkR3+aOVsC/OQfNHF98BNlvKjlMcUSzgHSmO7HSwYGK7VKJPmgJJMdeFLjy ONHGAVjX0UKr8quhDzvUgo18ehxQThA6jun2MvdTOzuekosgFQFeaLK5JAb/HpraaxcrQY V74jyw/RxZFCjgYb0DgAXHTWwjdd6bv0fzEH+ao9jp24P6aSN/69u7Gc4RtJOwe6ZnoQKC DWvXNpXBxAi1uHswm+yFOPyuuhOPv7A0jRfqHAbmV0JU1ofHisfQx2IJ45eqGQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1647272904; a=rsa-sha256; cv=none; b=GVljm9qo1cE8z1RtDuwA32rWM6ydwLYyChRdNouy2cuFto+nZGV0Q1kqBBBzmB6EBPs1tl wCsuDCWjp2T8/AoyzcwdJKsO/eebK3uqzQYEiCUT5Za37NRMKA0I+87gdVjXz4D0GoRHkM eMl921klmtoUZOD8TkyFZYOuuZAEw5EIO5pu+de0MQkoOQ+l3042GOT+2TYoay7nxAgKjX Z3y/slXNSaTz3PJb/en3mZs5Y0E2ICUVY8yP4nH5nnTkO0xhMPd7Hm0D2Dvl0db+Pv6z1F 1DohYDg1o5Em1lQetL4uVHBBDKCRqN4puOjjUVJAYm1od+o7oy4SS0bbudsEtg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch releng/13.1 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=626c1423ee93aa95d1d2dd23c406ba661756dfe1 commit 626c1423ee93aa95d1d2dd23c406ba661756dfe1 Author: Konstantin Belousov AuthorDate: 2022-03-06 08:59:39 +0000 Commit: Mark Johnston CommitDate: 2022-03-14 15:47:24 +0000 libc __sfvwrite(): roll back FILE buffer pointer on fflush error __sfvwrite() advances the pointer before calling fflush. If fflush() fails, it is not enough to roll back inside it, because we cannot know how much was advanced by the caller. Reported by: Peter Approved by: re (gjb) Reviewed by: markj Sponsored by: The FreeBSD Foundation Fixes: 86a16ada1ea608408cec370171d9f59353e97c77 (cherry picked from commit bafaa70b6f9098d83d074968c8e6747ecec1e118) (cherry picked from commit 647f02d68a60b66d063a15feed3c817966114d52) --- lib/libc/stdio/fvwrite.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/libc/stdio/fvwrite.c b/lib/libc/stdio/fvwrite.c index 50b32b8eca6e..2a161859afa9 100644 --- a/lib/libc/stdio/fvwrite.c +++ b/lib/libc/stdio/fvwrite.c @@ -54,6 +54,7 @@ int __sfvwrite(FILE *fp, struct __suio *uio) { size_t len; + unsigned char *old_p; char *p; struct __siov *iov; int w, s; @@ -137,8 +138,12 @@ __sfvwrite(FILE *fp, struct __suio *uio) COPY(w); /* fp->_w -= w; */ /* unneeded */ fp->_p += w; - if (__fflush(fp)) + old_p = fp->_p; + if (__fflush(fp) == EOF) { + if (old_p == fp->_p) + fp->_p -= w; goto err; + } } else if (len >= (w = fp->_bf._size)) { /* write directly */ w = _swrite(fp, p, w); @@ -177,8 +182,12 @@ __sfvwrite(FILE *fp, struct __suio *uio) COPY(w); /* fp->_w -= w; */ fp->_p += w; - if (__fflush(fp)) + old_p = fp->_p; + if (__fflush(fp) == EOF) { + if (old_p == fp->_p) + fp->_p -= w; goto err; + } } else if (s >= (w = fp->_bf._size)) { w = _swrite(fp, p, w); if (w <= 0)