Coverage Report

Created: 2026-01-31 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wget2/lib/fclose.c
Line
Count
Source
1
/* fclose replacement.
2
   Copyright (C) 2008-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
#include <config.h>
18
19
/* Specification.  */
20
#include <stdio.h>
21
22
#include <errno.h>
23
#include <unistd.h>
24
25
#include "freading.h"
26
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
27
# include "msvc-inval.h"
28
#endif
29
30
#undef fclose
31
32
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
33
static int
34
fclose_nothrow (FILE *fp)
35
{
36
  int result;
37
38
  TRY_MSVC_INVAL
39
    {
40
      result = fclose (fp);
41
    }
42
  CATCH_MSVC_INVAL
43
    {
44
      result = EOF;
45
      errno = EBADF;
46
    }
47
  DONE_MSVC_INVAL;
48
49
  return result;
50
}
51
#else
52
36.9k
# define fclose_nothrow fclose
53
#endif
54
55
/* Override fclose() to call the overridden fflush() or close().  */
56
57
int
58
rpl_fclose (FILE *fp)
59
36.9k
{
60
36.9k
  int saved_errno = 0;
61
62
  /* Don't change behavior on memstreams.  */
63
36.9k
  int fd = fileno (fp);
64
36.9k
  if (fd < 0)
65
30.9k
    return fclose_nothrow (fp);
66
67
  /* We only need to flush the file if it is not reading or if it is
68
     seekable.  This only guarantees the file position of input files
69
     if the fflush module is also in use.  */
70
5.97k
  if ((!freading (fp) || lseek (fileno (fp), 0, SEEK_CUR) != -1)
71
5.97k
      && fflush (fp))
72
0
    saved_errno = errno;
73
74
5.97k
  int result = 0;
75
76
  /* fclose() calls close(), but we need to also invoke all hooks that our
77
     overridden close() function invokes.  See lib/close.c.  */
78
#if WINDOWS_SOCKETS
79
  /* Call the overridden close(), then the original fclose().
80
     Note about multithread-safety: There is a race condition where some
81
     other thread could open fd between our close and fclose.  */
82
  if (close (fd) < 0 && saved_errno == 0)
83
    saved_errno = errno;
84
85
  fclose_nothrow (fp); /* will fail with errno = EBADF,
86
                          if we did not lose a race */
87
88
#else /* !WINDOWS_SOCKETS */
89
  /* Call fclose() and invoke all hooks of the overridden close().  */
90
91
# if REPLACE_FCHDIR
92
  /* Note about multithread-safety: There is a race condition here as well.
93
     Some other thread could open fd between our calls to fclose and
94
     _gl_unregister_fd.  */
95
  result = fclose_nothrow (fp);
96
  if (result == 0)
97
    _gl_unregister_fd (fd);
98
# else
99
  /* No race condition here.  */
100
5.97k
  result = fclose_nothrow (fp);
101
5.97k
# endif
102
103
5.97k
#endif /* !WINDOWS_SOCKETS */
104
105
5.97k
  if (saved_errno != 0)
106
0
    {
107
0
      errno = saved_errno;
108
0
      result = EOF;
109
0
    }
110
111
5.97k
  return result;
112
36.9k
}