Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 May 2016 22:04:28 +0000 (UTC)
From:      "Conrad E. Meyer" <cem@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r299484 - head/usr.bin/random
Message-ID:  <201605112204.u4BM4Sgc034616@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: cem
Date: Wed May 11 22:04:28 2016
New Revision: 299484
URL: https://svnweb.freebsd.org/changeset/base/299484

Log:
  random(6): Fix double-close
  
  In the case where a file lacks a trailing newline, there is some "evil" code to
  reverse goto the tokenizing code ("make_token") for the final token in the
  file.  In this case, 'fd' is closed more than once.  Use a negative sentinel
  value to guard close(2), preventing the double close.
  
  Ideally, this code would be restructured to avoid this ugly construction.
  
  Reported by:	Coverity
  CID:		1006123
  Sponsored by:	EMC / Isilon Storage Division

Modified:
  head/usr.bin/random/randomize_fd.c

Modified: head/usr.bin/random/randomize_fd.c
==============================================================================
--- head/usr.bin/random/randomize_fd.c	Wed May 11 21:35:58 2016	(r299483)
+++ head/usr.bin/random/randomize_fd.c	Wed May 11 22:04:28 2016	(r299484)
@@ -174,7 +174,7 @@ randomize_fd(int fd, int type, int uniqu
 			if ((type == RANDOM_TYPE_LINES && buf[i] == '\n') ||
 			    (type == RANDOM_TYPE_WORDS && isspace(buf[i])) ||
 			    (eof && i == buflen - 1)) {
-			make_token:
+make_token:
 				if (numnode == RANDOM_MAX_PLUS1) {
 					errno = EFBIG;
 					err(1, "too many delimiters");
@@ -199,7 +199,10 @@ randomize_fd(int fd, int type, int uniqu
 		}
 	}
 
-	(void)close(fd);
+	if (fd >= 0) {
+		(void)close(fd);
+		fd = -1;
+	}
 
 	/* Necessary evil to compensate for files that don't end with a newline */
 	if (bufc != i) {



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