From 3f629d3134494288edf4965616408f90fae6f6d7 Mon Sep 17 00:00:00 2001 From: truckman Date: Wed, 15 Jun 2016 06:27:43 +0000 Subject: [PATCH] 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 git-svn-id: svn://svn.freebsd.org/base/stable/10@301917 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- games/random/randomize_fd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/games/random/randomize_fd.c b/games/random/randomize_fd.c index f66b9654d..684c84ec5 100644 --- a/games/random/randomize_fd.c +++ b/games/random/randomize_fd.c @@ -174,7 +174,7 @@ randomize_fd(int fd, int type, int unique, double denom) 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 unique, double denom) } } - (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--) { -- 2.42.0