aboutsummaryrefslogtreecommitdiff
path: root/stdio-common/tst-ungetc-nomem.c
blob: 0872de60506e970e91db125afdd21130c5501af3 (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
/* Test ungetc behavior with malloc failures.
   Copyright The GNU Toolchain Authors.
   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 <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/xstdio.h>

static volatile bool fail = false;

/* Induce a malloc failure whenever FAIL is set; we use the __LIBC_MALLOC entry
   point to avoid the other alternative, which is RTLD_NEXT.  */
void *
malloc (size_t sz)
{
  if (fail)
    return NULL;

  static void *(*real_malloc) (size_t);

  if (real_malloc == NULL)
    real_malloc = dlsym (RTLD_NEXT, "malloc");

  return real_malloc (sz);
}

static int
do_test (void)
{
  char *filename = NULL;
  struct stat props = {};
  size_t bufsz = 0;

  create_temp_file ("tst-ungetc-nomem.", &filename);
  if (stat (filename, &props) != 0)
    FAIL_EXIT1 ("Could not get file status: %m\n");

  FILE *fp = fopen (filename, "w");

  /* The libio buffer sizes are the same as block size.  This is to ensure that
     the test runs at the read underflow boundary as well.  */
  bufsz = props.st_blksize + 2;

  char *buf = xmalloc (bufsz);
  memset (buf, 'a', bufsz);

  if (fwrite (buf, sizeof (char), bufsz, fp) != bufsz)
    FAIL_EXIT1 ("fwrite failed: %m\n");
  xfclose (fp);

  /* Begin test.  */
  fp = xfopen (filename, "r");

  while (!feof (fp))
    {
      /* Reset the pushback buffer state.  */
      fseek (fp, 0, SEEK_CUR);

      fail = true;
      /* 1: First ungetc should always succeed, as the standard requires.  */
      TEST_COMPARE (ungetc ('b', fp), 'b');

      /* 2: This will result in resizing, which should fail.  */
      TEST_COMPARE (ungetc ('c', fp), EOF);

      /* 3: Now allow the resizing, which should immediately fill up the buffer
         too, since this allocates only double the current buffer, i.e.
         2-bytes.  */
      fail = false;
      TEST_COMPARE (ungetc ('d', fp), 'd');

      /* 4: And fail again because this again forces an alloc, which fails.  */
      fail = true;
      TEST_COMPARE (ungetc ('e', fp), EOF);

      /* 5: Enable allocations again so that we now get a 4-byte buffer.  Now
         both calls should work.  */
      fail = false;
      TEST_COMPARE (ungetc ('f', fp), 'f');
      fail = true;
      TEST_COMPARE (ungetc ('g', fp), 'g');

      /* Drain out the x's.  */
      TEST_COMPARE (fgetc (fp), 'g');
      TEST_COMPARE (fgetc (fp), 'f');
      TEST_COMPARE (fgetc (fp), 'd');

      /* Finally, drain out the first char we had pushed back, followed by one
	 more char from the stream, if present.  */
      TEST_COMPARE (fgetc (fp), 'b');
      char c = fgetc (fp);
      if (!feof (fp))
	TEST_COMPARE (c, 'a');
    }

  /* Final sanity check before we're done.  */
  TEST_COMPARE (ferror (fp), 0);
  xfclose (fp);

  return 0;
}

#include <support/test-driver.c>