aboutsummaryrefslogtreecommitdiff
path: root/stdio-common/tst-fclose-offset.c
blob: a31de1117c7dfeec97b1e5e7099c0735e7d98545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* Test offset of input file descriptor after close (bug 12724).
   Copyright (C) 2025 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <stdio.h>
#include <wchar.h>

#include <support/check.h>
#include <support/temp_file.h>
#include <support/xstdio.h>
#include <support/xunistd.h>

int
do_test (void)
{
  char *filename = NULL;
  int fd = create_temp_file ("tst-fclose-offset", &filename);
  TEST_VERIFY_EXIT (fd != -1);

  /* Test offset of open file description for output and input streams
     after fclose, case from bug 12724.  */

  const char buf[] = "hello world";
  xwrite (fd, buf, sizeof buf);
  TEST_COMPARE (lseek (fd, 1, SEEK_SET), 1);
  int fd2 = xdup (fd);
  FILE *f = fdopen (fd2, "w");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (fputc (buf[1], f), buf[1]);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 2);

  /* Likewise for an input stream.  */
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (fgetc (f), buf[2]);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 3);

  /* Test offset of open file description for output and input streams
     after fclose, case from comment on bug 12724 (failed after first
     attempt at fixing that bug).  This verifies that the offset is
     not reset when there has been no input or output on the FILE* (in
     that case, the FILE* might not be the active handle).  */

  TEST_COMPARE (lseek (fd, 0, SEEK_SET), 0);
  xwrite (fd, buf, sizeof buf);
  TEST_COMPARE (lseek (fd, 1, SEEK_SET), 1);
  fd2 = xdup (fd);
  f = fdopen (fd2, "w");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (lseek (fd, 4, SEEK_SET), 4);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 4);

  /* Likewise for an input stream.  */
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (lseek (fd, 4, SEEK_SET), 4);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 4);

  /* Further cases without specific tests in bug 12724, to verify
     proper operation of the rules about the offset only being set
     when the stream is the active handle.  */

  /* Test offset set by fclose after fseek and fgetc.  */
  TEST_COMPARE (lseek (fd, 0, SEEK_SET), 0);
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (fseek (f, 1, SEEK_SET), 0);
  TEST_COMPARE (fgetc (f), buf[1]);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 2);

  /* Test offset not set by fclose after fseek and fgetc, if that
     fgetc is at EOF (in which case the active handle might have
     changed).  */
  TEST_COMPARE (lseek (fd, 0, SEEK_SET), 0);
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (fseek (f, sizeof buf, SEEK_SET), 0);
  TEST_COMPARE (fgetc (f), EOF);
  TEST_COMPARE (lseek (fd, 4, SEEK_SET), 4);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 4);

  /* Test offset not set by fclose after fseek and fgetc and fflush
     (active handle might have changed after fflush).  */
  TEST_COMPARE (lseek (fd, 0, SEEK_SET), 0);
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (fseek (f, 1, SEEK_SET), 0);
  TEST_COMPARE (fgetc (f), buf[1]);
  TEST_COMPARE (fflush (f), 0);
  TEST_COMPARE (lseek (fd, 4, SEEK_SET), 4);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 4);

  /* Test offset not set by fclose after fseek and fgetc, if the
     stream is unbuffered (active handle might change at any
     time).  */
  TEST_COMPARE (lseek (fd, 0, SEEK_SET), 0);
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  setbuf (f, NULL);
  TEST_COMPARE (fseek (f, 1, SEEK_SET), 0);
  TEST_COMPARE (fgetc (f), buf[1]);
  TEST_COMPARE (lseek (fd, 4, SEEK_SET), 4);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 4);

  /* Also test such cases with the stream in wide mode.  */

  /* Test offset set by fclose after fseek and fgetwc.  */
  TEST_COMPARE (lseek (fd, 0, SEEK_SET), 0);
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (fseek (f, 1, SEEK_SET), 0);
  TEST_COMPARE (fgetwc (f), (wint_t) buf[1]);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 2);

  /* Test offset not set by fclose after fseek and fgetwc, if that
     fgetwc is at EOF (in which case the active handle might have
     changed).  */
  TEST_COMPARE (lseek (fd, 0, SEEK_SET), 0);
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (fseek (f, sizeof buf, SEEK_SET), 0);
  TEST_COMPARE (fgetwc (f), WEOF);
  TEST_COMPARE (lseek (fd, 4, SEEK_SET), 4);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 4);

  /* Test offset not set by fclose after fseek and fgetwc and fflush
     (active handle might have changed after fflush).  */
  TEST_COMPARE (lseek (fd, 0, SEEK_SET), 0);
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  TEST_COMPARE (fseek (f, 1, SEEK_SET), 0);
  TEST_COMPARE (fgetwc (f), (wint_t) buf[1]);
  TEST_COMPARE (fflush (f), 0);
  TEST_COMPARE (lseek (fd, 4, SEEK_SET), 4);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 4);

  /* Test offset not set by fclose after fseek and fgetwc, if the
     stream is unbuffered (active handle might change at any
     time).  */
  TEST_COMPARE (lseek (fd, 0, SEEK_SET), 0);
  fd2 = xdup (fd);
  f = fdopen (fd2, "r");
  TEST_VERIFY_EXIT (f != NULL);
  setbuf (f, NULL);
  TEST_COMPARE (fseek (f, 1, SEEK_SET), 0);
  TEST_COMPARE (fgetwc (f), (wint_t) buf[1]);
  TEST_COMPARE (lseek (fd, 4, SEEK_SET), 4);
  xfclose (f);
  errno = 0;
  TEST_COMPARE (lseek (fd2, 0, SEEK_CUR), -1);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (lseek (fd, 0, SEEK_CUR), 4);

  return 0;
}

#include <support/test-driver.c>