Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Jun 2016 06:27:44 +0000 (UTC)
From:      Don Lewis <truckman@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r301917 - stable/10/games/random
Message-ID:  <201606150627.u5F6Ri4K021699@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: truckman
Date: Wed Jun 15 06:27:43 2016
New Revision: 301917
URL: https://svnweb.freebsd.org/changeset/base/301917

Log:
  MFC r299484, r301574
  
  r299484 | cem | 2016-05-11 15:04:28 -0700 (Wed, 11 May 2016) | 13 lines
  
  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.
  
  r301574 | truckman | 2016-06-07 19:14:05 -0700 (Tue, 07 Jun 2016) | 15 lines
  
  Fix a (false positive?) Argument cannot be negative coverity defect.
  
  Rather than guarding close(fd) with an fd >= 0 test and setting fd
  to -1 when it is closed to avoid a potential double-close, just
  move the close() call after the conditional "goto make_token".  This
  moves the close() call totally outside the loop to avoid the
  possibility of calling it twice.  This should also prevent a Coverity
  warning about checking fd for validity after it was previously passed
  to read().
  
  Reported by:	Coverity
  CID:		1006123, 1355335

Modified:
  stable/10/games/random/randomize_fd.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/games/random/randomize_fd.c
==============================================================================
--- stable/10/games/random/randomize_fd.c	Wed Jun 15 06:08:05 2016	(r301916)
+++ stable/10/games/random/randomize_fd.c	Wed Jun 15 06:27:43 2016	(r301917)
@@ -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,14 +199,14 @@ randomize_fd(int fd, int type, int uniqu
 		}
 	}
 
-	(void)close(fd);
-
 	/* Necessary evil to compensate for files that don't end with a newline */
 	if (bufc != i) {
 		i--;
 		goto make_token;
 	}
 
+	(void)close(fd);
+
 	free(buf);
 
 	for (i = numnode; i > 0; i--) {



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