From owner-svn-src-stable-10@freebsd.org Wed Jun 1 17:47:35 2016 Return-Path: Delivered-To: svn-src-stable-10@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8F77FB613D4; Wed, 1 Jun 2016 17:47:35 +0000 (UTC) (envelope-from truckman@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 44A9D13B3; Wed, 1 Jun 2016 17:47:35 +0000 (UTC) (envelope-from truckman@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u51HlY5h060509; Wed, 1 Jun 2016 17:47:34 GMT (envelope-from truckman@FreeBSD.org) Received: (from truckman@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u51HlYGT060508; Wed, 1 Jun 2016 17:47:34 GMT (envelope-from truckman@FreeBSD.org) Message-Id: <201606011747.u51HlYGT060508@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: truckman set sender to truckman@FreeBSD.org using -f From: Don Lewis Date: Wed, 1 Jun 2016 17:47:34 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r301155 - stable/10/bin/ed X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-10@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for only the 10-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Jun 2016 17:47:35 -0000 Author: truckman Date: Wed Jun 1 17:47:34 2016 New Revision: 301155 URL: https://svnweb.freebsd.org/changeset/base/301155 Log: MFC r300692 Close the input FILE * in read_file() and the output FILE * in write_file() if read_stream() or write_stream() fails to avoid leaking the FILE. Reported by: Coverity CID: 977702 Reviewed by: pfg Differential Revision: https://reviews.freebsd.org/D6554 Modified: stable/10/bin/ed/io.c Directory Properties: stable/10/ (props changed) Modified: stable/10/bin/ed/io.c ============================================================================== --- stable/10/bin/ed/io.c Wed Jun 1 17:45:00 2016 (r301154) +++ stable/10/bin/ed/io.c Wed Jun 1 17:47:34 2016 (r301155) @@ -36,20 +36,24 @@ read_file(char *fn, long n) { FILE *fp; long size; - + int cs; fp = (*fn == '!') ? popen(fn + 1, "r") : fopen(strip_escapes(fn), "r"); if (fp == NULL) { fprintf(stderr, "%s: %s\n", fn, strerror(errno)); errmsg = "cannot open input file"; return ERR; - } else if ((size = read_stream(fp, n)) < 0) - return ERR; - else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) { + } + if ((size = read_stream(fp, n)) < 0) { + fprintf(stderr, "%s: %s\n", fn, strerror(errno)); + errmsg = "error reading input file"; + } + if ((cs = (*fn == '!') ? pclose(fp) : fclose(fp)) < 0) { fprintf(stderr, "%s: %s\n", fn, strerror(errno)); errmsg = "cannot close input file"; - return ERR; } + if (size < 0 || cs < 0) + return ERR; if (!scripted) fprintf(stdout, "%lu\n", size); return current_addr - n; @@ -143,19 +147,24 @@ write_file(char *fn, const char *mode, l { FILE *fp; long size; + int cs; fp = (*fn == '!') ? popen(fn+1, "w") : fopen(strip_escapes(fn), mode); if (fp == NULL) { fprintf(stderr, "%s: %s\n", fn, strerror(errno)); errmsg = "cannot open output file"; return ERR; - } else if ((size = write_stream(fp, n, m)) < 0) - return ERR; - else if (((*fn == '!') ? pclose(fp) : fclose(fp)) < 0) { + } + if ((size = write_stream(fp, n, m)) < 0) { + fprintf(stderr, "%s: %s\n", fn, strerror(errno)); + errmsg = "error writing output file"; + } + if ((cs = (*fn == '!') ? pclose(fp) : fclose(fp)) < 0) { fprintf(stderr, "%s: %s\n", fn, strerror(errno)); errmsg = "cannot close output file"; - return ERR; } + if (size < 0 || cs < 0) + return ERR; if (!scripted) fprintf(stdout, "%lu\n", size); return n ? m - n + 1 : 0;