| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/gnulib/lib/freopen-safer.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* Invoke freopen, but avoid some glitches. | - | ||||||||||||
| 2 | - | |||||||||||||
| 3 | Copyright (C) 2009-2018 Free Software Foundation, Inc. | - | ||||||||||||
| 4 | - | |||||||||||||
| 5 | This program is free software: you can redistribute it and/or modify | - | ||||||||||||
| 6 | it under the terms of the GNU General Public License as published by | - | ||||||||||||
| 7 | the Free Software Foundation; either version 3 of the License, or | - | ||||||||||||
| 8 | (at your option) any later version. | - | ||||||||||||
| 9 | - | |||||||||||||
| 10 | This program is distributed in the hope that it will be useful, | - | ||||||||||||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||||||||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||||||||
| 13 | GNU General Public License for more details. | - | ||||||||||||
| 14 | - | |||||||||||||
| 15 | You should have received a copy of the GNU General Public License | - | ||||||||||||
| 16 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ | - | ||||||||||||
| 17 | - | |||||||||||||
| 18 | /* Written by Eric Blake. */ | - | ||||||||||||
| 19 | - | |||||||||||||
| 20 | #include <config.h> | - | ||||||||||||
| 21 | - | |||||||||||||
| 22 | #include "stdio-safer.h" | - | ||||||||||||
| 23 | - | |||||||||||||
| 24 | #include <errno.h> | - | ||||||||||||
| 25 | #include <fcntl.h> | - | ||||||||||||
| 26 | #include <stdbool.h> | - | ||||||||||||
| 27 | #include <unistd.h> | - | ||||||||||||
| 28 | - | |||||||||||||
| 29 | #ifndef FALLTHROUGH | - | ||||||||||||
| 30 | # if __GNUC__ < 7 | - | ||||||||||||
| 31 | # define FALLTHROUGH ((void) 0) | - | ||||||||||||
| 32 | # else | - | ||||||||||||
| 33 | # define FALLTHROUGH __attribute__ ((__fallthrough__)) | - | ||||||||||||
| 34 | # endif | - | ||||||||||||
| 35 | #endif | - | ||||||||||||
| 36 | - | |||||||||||||
| 37 | /* Guarantee that FD is open; all smaller FDs must already be open. | - | ||||||||||||
| 38 | Return true if successful. */ | - | ||||||||||||
| 39 | static bool | - | ||||||||||||
| 40 | protect_fd (int fd) | - | ||||||||||||
| 41 | { | - | ||||||||||||
| 42 | int value = open ("/dev/null", O_RDONLY); | - | ||||||||||||
| 43 | if (value != fd)
| 0 | ||||||||||||
| 44 | { | - | ||||||||||||
| 45 | if (0 <= value)
| 0 | ||||||||||||
| 46 | { | - | ||||||||||||
| 47 | close (value); | - | ||||||||||||
| 48 | errno = EBADF; /* Unexpected; this is as good as anything else. */ | - | ||||||||||||
| 49 | } never executed: end of block | 0 | ||||||||||||
| 50 | return false; never executed: return 0 ; | 0 | ||||||||||||
| 51 | } | - | ||||||||||||
| 52 | return true; never executed: return 1 ; | 0 | ||||||||||||
| 53 | } | - | ||||||||||||
| 54 | - | |||||||||||||
| 55 | /* Like freopen, but guarantee that reopening stdin, stdout, or stderr | - | ||||||||||||
| 56 | preserves the invariant that STDxxx_FILENO==fileno(stdxxx), and | - | ||||||||||||
| 57 | that no other stream will interfere with the standard streams. | - | ||||||||||||
| 58 | This is necessary because most freopen implementations will change | - | ||||||||||||
| 59 | the associated fd of a stream to the lowest available slot. */ | - | ||||||||||||
| 60 | - | |||||||||||||
| 61 | FILE * | - | ||||||||||||
| 62 | freopen_safer (char const *name, char const *mode, FILE *f) | - | ||||||||||||
| 63 | { | - | ||||||||||||
| 64 | /* Unfortunately, we cannot use the fopen_safer approach of using | - | ||||||||||||
| 65 | fdopen (dup_safer (fileno (freopen (cmd, mode, f)))), because we | - | ||||||||||||
| 66 | need to return f itself. The implementation of freopen(NULL,m,f) | - | ||||||||||||
| 67 | is system-dependent, so the best we can do is guarantee that all | - | ||||||||||||
| 68 | lower-valued standard fds are open prior to the freopen call, | - | ||||||||||||
| 69 | even though this puts more pressure on open fds. */ | - | ||||||||||||
| 70 | bool protect_in = false; | - | ||||||||||||
| 71 | bool protect_out = false; | - | ||||||||||||
| 72 | bool protect_err = false; | - | ||||||||||||
| 73 | int saved_errno; | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | switch (fileno (f)) | - | ||||||||||||
| 76 | { | - | ||||||||||||
| 77 | default: /* -1 or not a standard stream. */ never executed: default: | 0 | ||||||||||||
| 78 | if (dup2 (STDERR_FILENO, STDERR_FILENO) != STDERR_FILENO)
| 0 | ||||||||||||
| 79 | protect_err = true; never executed: protect_err = 1 ; | 0 | ||||||||||||
| 80 | FALLTHROUGH; | - | ||||||||||||
| 81 | case STDERR_FILENO: code before this statement never executed: case 2 :never executed: case 2 : | 0 | ||||||||||||
| 82 | if (dup2 (STDOUT_FILENO, STDOUT_FILENO) != STDOUT_FILENO)
| 0 | ||||||||||||
| 83 | protect_out = true; never executed: protect_out = 1 ; | 0 | ||||||||||||
| 84 | FALLTHROUGH; | - | ||||||||||||
| 85 | case STDOUT_FILENO: code before this statement never executed: case 1 :never executed: case 1 : | 0 | ||||||||||||
| 86 | if (dup2 (STDIN_FILENO, STDIN_FILENO) != STDIN_FILENO)
| 0 | ||||||||||||
| 87 | protect_in = true; never executed: protect_in = 1 ; | 0 | ||||||||||||
| 88 | FALLTHROUGH; | - | ||||||||||||
| 89 | case STDIN_FILENO: code before this statement never executed: case 0 :executed 181 times by 5 tests: case 0 :Executed by:
| 0-181 | ||||||||||||
| 90 | /* Nothing left to protect. */ | - | ||||||||||||
| 91 | break; executed 181 times by 5 tests: break;Executed by:
| 181 | ||||||||||||
| 92 | } | - | ||||||||||||
| 93 | if (protect_in && !protect_fd (STDIN_FILENO))
| 0-181 | ||||||||||||
| 94 | f = NULL; never executed: f = ((void *)0) ; | 0 | ||||||||||||
| 95 | else if (protect_out && !protect_fd (STDOUT_FILENO))
| 0-181 | ||||||||||||
| 96 | f = NULL; never executed: f = ((void *)0) ; | 0 | ||||||||||||
| 97 | else if (protect_err && !protect_fd (STDERR_FILENO))
| 0-181 | ||||||||||||
| 98 | f = NULL; never executed: f = ((void *)0) ; | 0 | ||||||||||||
| 99 | else | - | ||||||||||||
| 100 | f = freopen (name, mode, f); executed 181 times by 5 tests: f = rpl_freopen (name, mode, f);Executed by:
| 181 | ||||||||||||
| 101 | saved_errno = errno; | - | ||||||||||||
| 102 | if (protect_err)
| 0-181 | ||||||||||||
| 103 | close (STDERR_FILENO); never executed: close ( 2 ); | 0 | ||||||||||||
| 104 | if (protect_out)
| 0-181 | ||||||||||||
| 105 | close (STDOUT_FILENO); never executed: close ( 1 ); | 0 | ||||||||||||
| 106 | if (protect_in)
| 0-181 | ||||||||||||
| 107 | close (STDIN_FILENO); never executed: close ( 0 ); | 0 | ||||||||||||
| 108 | if (!f)
| 2-179 | ||||||||||||
| 109 | errno = saved_errno; executed 2 times by 2 tests: (*__errno_location ()) = saved_errno;Executed by:
| 2 | ||||||||||||
| 110 | return f; executed 181 times by 5 tests: return f;Executed by:
| 181 | ||||||||||||
| 111 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |