Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 370413
Collapse All | Expand All

(-)b/libio/Makefile (-2 / +2 lines)
Lines 1-4 Link Here
1
# Copyright (C) 1995-2004,2006-2009,2011 Free Software Foundation, Inc.
1
# Copyright (C) 1995-2004,2006,2007,2008,2009 Free Software Foundation, Inc.
2
# This file is part of the GNU C Library.
2
# This file is part of the GNU C Library.
3
3
4
# The GNU C Library is free software; you can redistribute it and/or
4
# The GNU C Library is free software; you can redistribute it and/or
Lines 58-64 tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \ Link Here
58
	tst-memstream1 tst-memstream2 \
58
	tst-memstream1 tst-memstream2 \
59
	tst-wmemstream1 tst-wmemstream2 \
59
	tst-wmemstream1 tst-wmemstream2 \
60
	bug-memstream1 bug-wmemstream1 \
60
	bug-memstream1 bug-wmemstream1 \
61
	tst-setvbuf1 tst-popen1 tst-fgetwc bug-wsetpos bug-fclose1
61
	tst-setvbuf1 tst-popen1 tst-fgetwc bug-wsetpos
62
test-srcs = test-freopen
62
test-srcs = test-freopen
63
63
64
all: # Make this the default target; it will be defined in Rules.
64
all: # Make this the default target; it will be defined in Rules.
(-)b/libio/bug-fclose1.c (-132 lines)
Lines 1-132 Link Here
1
// BZ #12724
2
3
static void do_prepare (void);
4
#define PREPARE(argc, argv) do_prepare ()
5
static int do_test (void);
6
#define TEST_FUNCTION do_test()
7
#include "../test-skeleton.c"
8
9
10
static int fd;
11
12
13
static void
14
do_prepare (void)
15
{
16
  fd = create_temp_file ("bug-fclose1.", NULL);
17
  if (fd == -1)
18
    {
19
      printf ("cannot create temporary file: %m\n");
20
      exit (1);
21
    }
22
}
23
24
25
static int
26
do_test (void)
27
{
28
  static const char pattern[] = "hello world";
29
30
  /* Prepare a seekable file.  */
31
  if (write (fd, pattern, sizeof pattern) != sizeof pattern)
32
    {
33
      printf ("cannot write pattern: %m\n");
34
      return 1;
35
    }
36
  if (lseek (fd, 1, SEEK_SET) != 1)
37
    {
38
      printf ("cannot seek after write: %m\n");
39
      return 1;
40
    }
41
42
  /* Create an output stream visiting the file; when it is closed, all
43
     other file descriptors visiting the file must see the new file
44
     position.  */
45
  int fd2 = dup (fd);
46
  if (fd2 < 0)
47
    {
48
      printf ("cannot duplicate descriptor for writing: %m\n");
49
      return 1;
50
    }
51
  FILE *f = fdopen (fd2, "w");
52
  if (f == NULL)
53
    {
54
      printf ("first fdopen failed: %m\n");
55
      return 1;
56
    }
57
  if (fputc (pattern[1], f) != pattern[1])
58
    {
59
      printf ("fputc failed: %m\n");
60
      return 1;
61
    }
62
  if (fclose (f) != 0)
63
    {
64
      printf ("first fclose failed: %m\n");
65
      return 1;
66
    }
67
  errno = 0;
68
  if (lseek (fd2, 0, SEEK_CUR) != -1)
69
    {
70
      printf ("lseek after fclose after write did not fail\n");
71
      return 1;
72
    }
73
  if (errno != EBADF)
74
    {
75
      printf ("lseek after fclose after write did not fail with EBADF: %m\n");
76
      return 1;
77
    }
78
  off_t o = lseek (fd, 0, SEEK_CUR);
79
  if (o != 2)
80
    {
81
      printf ("\
82
lseek on original descriptor after first fclose returned %ld, expected 2\n",
83
	      (long int) o);
84
      return 1;
85
    }
86
87
  /* Likewise for an input stream.  */
88
  fd2 = dup (fd);
89
  if (fd2 < 0)
90
     {
91
      printf ("cannot duplicate descriptor for reading: %m\n");
92
      return 1;
93
    }
94
  f = fdopen (fd2, "r");
95
   if (f == NULL)
96
    {
97
      printf ("second fdopen failed: %m\n");
98
      return 1;
99
    }
100
   char c = fgetc (f);
101
   if (c != pattern[2])
102
     {
103
       printf ("getc returned %c, expected %c\n", c, pattern[2]);
104
       return 1;
105
     }
106
  if (fclose (f) != 0)
107
    {
108
      printf ("second fclose failed: %m\n");
109
      return 1;
110
    }
111
  errno = 0;
112
  if (lseek (fd2, 0, SEEK_CUR) != -1)
113
    {
114
      printf ("lseek after fclose after read did not fail\n");
115
      return 1;
116
    }
117
  if (errno != EBADF)
118
    {
119
      printf ("lseek after fclose after read did not fail with EBADF: %m\n");
120
      return 1;
121
    }
122
  o = lseek (fd, 0, SEEK_CUR);
123
  if (o != 3)
124
    {
125
      printf ("\
126
lseek on original descriptor after second fclose returned %ld, expected 3\n",
127
	      (long int) o);
128
      return 1;
129
    }
130
131
  return 0;
132
}
(-)glibc-2.14/libio/fileops.c (-11 / +3 lines)
Lines 160-187 int Link Here
160
_IO_new_file_close_it (fp)
160
_IO_new_file_close_it (fp)
161
     _IO_FILE *fp;
161
     _IO_FILE *fp;
162
{
162
{
163
  int write_status;
163
  if (!_IO_file_is_open (fp))
164
  if (!_IO_file_is_open (fp))
164
    return EOF;
165
    return EOF;
165
166
166
  int write_status;
167
  if ((fp->_flags & _IO_NO_WRITES) == 0
167
  if (_IO_in_put_mode (fp))
168
      && (fp->_flags & _IO_CURRENTLY_PUTTING) != 0)
168
    write_status = _IO_do_flush (fp);
169
    write_status = _IO_do_flush (fp);
169
  else if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
170
	   && !_IO_in_backup (fp))
171
    {
172
      off64_t o = _IO_SEEKOFF (fp, 0, _IO_seek_cur, 0);
173
      if (o == WEOF)
174
	write_status = EOF;
175
      else
176
	write_status = _IO_SYSSEEK (fp, o, SEEK_SET) < 0 ? EOF : 0;
177
    }
178
  else
170
  else
179
    write_status = 0;
171
    write_status = 0;
180
172
181
  INTUSE(_IO_unsave_markers) (fp);
173
  INTUSE(_IO_unsave_markers) (fp);
182
174
183
  int close_status = ((fp->_flags2 & _IO_FLAGS2_NOCLOSE) == 0
175
  int close_status = ((fp->_flags2 & _IO_FLAGS2_NOCLOSE) == 0
184
		      ? _IO_SYSCLOSE (fp) : 0);
176
		      ? _IO_SYSCLOSE (fp) : 0);
185
177
186
  /* Free buffer. */
178
  /* Free buffer. */
187
#if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
179
#if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T

Return to bug 370413