aboutsummaryrefslogtreecommitdiff
path: root/stdio-common/tst-scanf-format-character.h
blob: b68a5e1f396872942557e517e2388ef4a76c9511 (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
/* Test feature wrapper for formatted character input.
   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 <string.h>

#include <support/next_to_fault.h>

/* Reference data is a sequence of characters to match against
   byte-wise.

   For the 's' conversion specifier the number of characters read
   from input includes any leading white-space characters consumed
   as well, so we also consider it a successful match when the ':'
   character following the reference data matches a terminating null
   character in the output produced by the 'scanf' family function
   under test while the character count hasn't been exhausted yet.

   The buffer is preinitialized to contain repeating '\xa5' character
   so as to catch missing data output.  Also no data is expected to be
   written beyond the character sequence received, which is verified
   by checking the following character in the buffer to remain '\xa5'.  */

#define SCANF_BUFFER_SIZE 65536

static struct support_next_to_fault ntf;

#define PREPARE initialize_value_init
static void
initialize_value_init (int argc, char **argv)
{
  ntf = support_next_to_fault_allocate (SCANF_BUFFER_SIZE);
}

static void __attribute__ ((destructor))
initialize_value_fini (void)
{
  support_next_to_fault_free (&ntf);
}

#define pointer_to_value(val) (val)

#define initialize_value(val)						\
do									\
  {									\
    val = ntf.buffer;							\
    memset (val, 0xa5, SCANF_BUFFER_SIZE);				\
  }									\
while (0)

#define verify_input(f, val, count, errp)				\
({									\
  __label__ out, skip;							\
  bool match = true;							\
  int err = 0;								\
  size_t i;								\
  int ch;								\
									\
  for (i = 0; i < count; i++)						\
    {									\
      ch = read_input ();						\
      if (ch < 0)							\
	{								\
	  err = ch;							\
	  goto out;							\
	}								\
      if (ch == ':' && val[i] == '\0' && f == 's')			\
	goto skip;							\
      if (ch != val[i])							\
	{								\
	  match = false;						\
	  goto out;							\
	}								\
    }									\
  ch = read_input ();							\
  if (ch < 0)								\
    {									\
      err = ch;								\
      goto out;								\
    }									\
									\
skip:									\
  if (f != 'c' && val[i++] != '\0')					\
    {									\
      err = OUTPUT_TERM;						\
      goto out;								\
    }									\
  if (val[i] != '\xa5')							\
    {									\
      err = OUTPUT_OVERRUN;						\
      goto out;								\
    }									\
									\
  while (ch != ':')							\
    {									\
      ch = read_input ();						\
      if (ch < 0)							\
	{								\
	  err = ch;							\
	  goto out;							\
	}								\
      match = false;							\
    }									\
									\
out:									\
  if (err || !match)							\
    {									\
      printf ("error: %s:%d: input buffer: `", __FILE__, __LINE__);	\
      for (size_t j = 0; j <= i; j++)					\
	printf ("%c", val[j]);						\
      printf ("'\n");							\
    }									\
									\
  *errp = err;								\
  match;								\
})