Coverage Report

Created: 2026-11-12 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/gl/fopen.c
Line
Count
Source
1
/* Open a stream to a file.
2
   Copyright (C) 2007-2026 Free Software Foundation, Inc.
3
4
   This file is free software: you can redistribute it and/or modify
5
   it under the terms of the GNU Lesser General Public License as
6
   published by the Free Software Foundation; either version 2.1 of the
7
   License, or (at your option) any later version.
8
9
   This file is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
   GNU Lesser General Public License for more details.
13
14
   You should have received a copy of the GNU Lesser General Public License
15
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19
/* If the user's config.h happens to include <stdio.h>, let it include only
20
   the system's <stdio.h> here, so that orig_fopen doesn't recurse to
21
   rpl_fopen.  */
22
#define _GL_SKIP_GNULIB_STDIO_H
23
#include <config.h>
24
25
/* Get the original definition of fopen.  It might be defined as a macro.  */
26
#include <stdio.h>
27
#undef _GL_SKIP_GNULIB_STDIO_H
28
29
static FILE *
30
orig_fopen (const char *filename, const char *mode)
31
0
{
32
0
  return fopen (filename, mode);
33
0
}
34
35
/* Specification.  */
36
#include <stdio.h>
37
38
#include <errno.h>
39
#include <fcntl.h>
40
#include <string.h>
41
#include <unistd.h>
42
#include <sys/types.h>
43
#include <sys/stat.h>
44
45
FILE *
46
rpl_fopen (const char *filename, const char *mode)
47
2
{
48
#if defined _WIN32 && ! defined __CYGWIN__
49
  if (streq (filename, "/dev/null"))
50
    filename = "NUL";
51
#endif
52
53
  /* Parse the mode.  */
54
2
  int open_direction = 0;
55
2
  int open_flags = 0;
56
2
#if GNULIB_FOPEN_GNU
57
2
  bool open_flags_gnu = false;
58
4
# define BUF_SIZE 80
59
2
  char fdopen_mode_buf[BUF_SIZE + 1];
60
2
#endif
61
2
  {
62
2
    const char *p = mode;
63
2
#if GNULIB_FOPEN_GNU
64
2
    char *q = fdopen_mode_buf;
65
2
#endif
66
67
8
    for (; *p != '\0'; p++)
68
6
      {
69
6
        switch (*p)
70
6
          {
71
2
          case 'r':
72
2
            open_direction = O_RDONLY;
73
2
#if GNULIB_FOPEN_GNU
74
2
            if (q < fdopen_mode_buf + BUF_SIZE)
75
2
              *q++ = *p;
76
2
#endif
77
2
            continue;
78
0
          case 'w':
79
0
            open_direction = O_WRONLY;
80
0
            open_flags |= O_CREAT | O_TRUNC;
81
0
#if GNULIB_FOPEN_GNU
82
0
            if (q < fdopen_mode_buf + BUF_SIZE)
83
0
              *q++ = *p;
84
0
#endif
85
0
            continue;
86
0
          case 'a':
87
0
            open_direction = O_WRONLY;
88
0
            open_flags |= O_CREAT | O_APPEND;
89
0
#if GNULIB_FOPEN_GNU
90
0
            if (q < fdopen_mode_buf + BUF_SIZE)
91
0
              *q++ = *p;
92
0
#endif
93
0
            continue;
94
2
          case 'b':
95
            /* While it is non-standard, O_BINARY is guaranteed by
96
               gnulib <fcntl.h>.  We can also assume that orig_fopen
97
               supports the 'b' flag.  */
98
2
            open_flags |= O_BINARY;
99
2
#if GNULIB_FOPEN_GNU
100
2
            if (q < fdopen_mode_buf + BUF_SIZE)
101
2
              *q++ = *p;
102
2
#endif
103
2
            continue;
104
0
          case '+':
105
0
            open_direction = O_RDWR;
106
0
#if GNULIB_FOPEN_GNU
107
0
            if (q < fdopen_mode_buf + BUF_SIZE)
108
0
              *q++ = *p;
109
0
#endif
110
0
            continue;
111
0
#if GNULIB_FOPEN_GNU
112
0
          case 'x':
113
0
            open_flags |= O_EXCL;
114
0
            open_flags_gnu = true;
115
0
            continue;
116
2
          case 'e':
117
2
            open_flags |= O_CLOEXEC;
118
2
            open_flags_gnu = true;
119
2
            continue;
120
0
#endif
121
0
          default:
122
0
            break;
123
6
          }
124
0
#if GNULIB_FOPEN_GNU
125
        /* The rest of the mode string can be a platform-dependent extension.
126
           Copy it unmodified.  */
127
0
        {
128
0
          size_t len = strlen (p);
129
0
          if (len > fdopen_mode_buf + BUF_SIZE - q)
130
0
            len = fdopen_mode_buf + BUF_SIZE - q;
131
0
          memcpy (q, p, len);
132
0
          q += len;
133
0
        }
134
0
#endif
135
0
        break;
136
6
      }
137
2
#if GNULIB_FOPEN_GNU
138
2
    *q = '\0';
139
2
#endif
140
2
  }
141
142
#if FOPEN_TRAILING_SLASH_BUG
143
  /* Fail if the mode requires write access and the filename ends in a slash,
144
     as POSIX says such a filename must name a directory
145
     <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
146
       "A pathname that contains at least one non-<slash> character and that
147
        ends with one or more trailing <slash> characters shall not be resolved
148
        successfully unless the last pathname component before the trailing
149
        <slash> characters names an existing directory"
150
     If the named file already exists as a directory, then if a mode that
151
     requires write access is specified, fopen() must fail because POSIX
152
     <https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html>
153
     says that it fails with errno = EISDIR in this case.
154
     If the named file does not exist or does not name a directory, then
155
     fopen() must fail since the file does not contain a '.' directory.  */
156
  {
157
    size_t len = strlen (filename);
158
    if (len > 0 && filename[len - 1] == '/')
159
      {
160
        if (open_direction != O_RDONLY)
161
          {
162
            errno = EISDIR;
163
            return NULL;
164
          }
165
166
        int fd = open (filename, open_direction | open_flags,
167
                       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
168
        if (fd < 0)
169
          return NULL;
170
171
        struct stat statbuf;
172
        if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
173
          {
174
            close (fd);
175
            errno = ENOTDIR;
176
            return NULL;
177
          }
178
179
        FILE *fp;
180
# if GNULIB_FOPEN_GNU
181
        fp = fdopen (fd, fdopen_mode_buf);
182
# else
183
        fp = fdopen (fd, mode);
184
# endif
185
        if (fp == NULL)
186
          {
187
            int saved_errno = errno;
188
            close (fd);
189
            errno = saved_errno;
190
          }
191
        return fp;
192
      }
193
  }
194
#endif
195
196
0
#if GNULIB_FOPEN_GNU
197
2
  if (open_flags_gnu)
198
2
    {
199
2
      int fd = open (filename, open_direction | open_flags,
200
2
                     S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
201
2
      if (fd < 0)
202
2
        return NULL;
203
204
0
      FILE *fp = fdopen (fd, fdopen_mode_buf);
205
0
      if (fp == NULL)
206
0
        {
207
0
          int saved_errno = errno;
208
0
          close (fd);
209
0
          errno = saved_errno;
210
0
        }
211
0
      return fp;
212
2
    }
213
0
#endif
214
215
  /* open_direction is sometimes used, sometimes unused.
216
     Silence gcc's warning about this situation.  */
217
0
  (void) open_direction;
218
219
0
  return orig_fopen (filename, mode);
220
2
}