Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 25 May 2016 18:38:30 +0000 (UTC)
From:      Don Lewis <truckman@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r300692 - head/bin/ed
Message-ID:  <201605251838.u4PIcUhc032521@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: truckman
Date: Wed May 25 18:38:30 2016
New Revision: 300692
URL: https://svnweb.freebsd.org/changeset/base/300692

Log:
  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
  MFC after:	1 week
  Differential Revision:	https://reviews.freebsd.org/D6554

Modified:
  head/bin/ed/io.c

Modified: head/bin/ed/io.c
==============================================================================
--- head/bin/ed/io.c	Wed May 25 18:10:44 2016	(r300691)
+++ head/bin/ed/io.c	Wed May 25 18:38:30 2016	(r300692)
@@ -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;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201605251838.u4PIcUhc032521>