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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/* Test skeleton for formatted scanf 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/>. */
/* The following definitions have to be supplied by the source including
this skeleton:
Macros:
TYPE_T_UNSIGNED_P [optional] Set to 1 if handling an unsigned
integer conversion.
Typedefs:
type_t Type to hold data produced by the conversion
handled.
Callable objects:
scanf_under_test Wrapper for the 'scanf' family feature to be
tested.
verify_input Verifier called to determine whether there is a
match between the data retrieved by the feature
tested and MATCH reference data supplied by input.
pointer_to_value Converter making a pointer suitable for the
feature tested from the data holding type.
initialize_value Initializer for the data holder to use ahead of
each call to the feature tested.
It is up to the source including this skeleton whether the individual
callable objects are going to be macros or actual functions.
See tst-*scanf-format-*.c for usage examples. */
#include <ctype.h>
#include <dlfcn.h>
#include <mcheck.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
/* Tweak our environment according to any TYPE_T_UNSIGNED_P setting
supplied by the individual test case. */
#ifndef TYPE_T_UNSIGNED_P
# define TYPE_T_UNSIGNED_P 0
#endif
#if TYPE_T_UNSIGNED_P
# define UNSIGNED unsigned
#else
# define UNSIGNED
#endif
/* Read and return a single character from standard input, returning
end-of-file or error status indication where applicable. */
static int
read_input (void)
{
int c = getchar ();
if (ferror (stdin))
c = INPUT_ERROR;
else if (feof (stdin))
c = INPUT_EOF;
return c;
}
/* Consume a signed decimal integer supplied by READ_INPUT above, up to
the following ':' field separator which is removed from input, making
sure the value requested does not overflow the range of the data type
according to TYPE_T_UNSIGNED_P.
Return the value retrieved and set ERRP to zero on success, otherwise
set ERRP to the error code. */
static long long
read_integer (int *errp)
{
bool m = false;
int ch;
ch = read_input ();
if (ch == '-' || ch == '+')
{
m = ch == '-';
ch = read_input ();
}
if (ch == ':')
{
*errp = INPUT_FORMAT;
return 0;
}
unsigned long long v = 0;
while (1)
{
unsigned long long v0 = v;
if (isdigit (ch))
{
v = 10 * v + (ch - '0');
if (!(TYPE_T_UNSIGNED_P
|| (v & ~((~0ULL) >> 1)) == 0
|| (m && v == ~((~0ULL) >> 1)))
|| v < v0)
{
*errp = INPUT_OVERFLOW;
return 0;
}
}
else if (ch < 0)
{
*errp = ch;
return 0;
}
else if (ch != ':')
{
*errp = INPUT_FORMAT;
return 0;
}
else
break;
ch = read_input ();
}
*errp = 0;
return m ? -v : v;
}
/* Return an error message corresponding to ERR. */
static const char *
get_error_message (int err)
{
switch (err)
{
case INPUT_EOF:
return "input line %zi: premature end of input";
case INPUT_ERROR:
return "input line %zi: error reading input data: %m";
case INPUT_FORMAT:
return "input line %zi: input data format error";
case INPUT_OVERFLOW:
return "input line %zi: input data arithmetic overflow";
case OUTPUT_TERM:
return "input line %zi: string termination missing from output";
case OUTPUT_OVERRUN:
return "input line %zi: output data overrun";
default:
return "input line %zi: internal test error";
}
}
/* Consume a record supplied by READ_INPUT above, according to '%' and
any assignment-suppressing character '*', followed by any width W,
any length modifier L, and conversion C, all already provided in FMT
(along with trailing "%lln" implicitly appended by the caller) and
removed from input along with the following ':' field separator.
For convenience the last character of conversion C is supplied as
the F parameter.
Record formats consumed:
%*<L><C>:<INPUT>:<RESULT==0>:<COUNT==-1>:
%*<W><L><C>:<INPUT>:<RESULT==0>:<COUNT==-1>:
%<L><C>:<INPUT>:<RESULT==0>:<COUNT==-1>:
%<W><L><C>:<INPUT>:<RESULT==0>:<COUNT==-1>:
%*<L><C>:<INPUT>:<RESULT>:<COUNT>:
%*<W><L><C>:<INPUT>:<RESULT>:<COUNT>:
%<L><C>:<INPUT>:<RESULT!=0>:<COUNT>:<MATCH>:
%<W><L><C>:<INPUT>:<RESULT!=0>:<COUNT>:<MATCH>:
Verify that the 'scanf' family function under test returned RESULT,
that the "%lln" conversion recorded COUNT characters or has not been
executed leaving the value at -1 as applicable, and where executed
that the conversion requested produced output matching MATCH.
Return 0 on success, -1 on failure. */
static int
do_scanf (char f, char *fmt)
{
bool value_match = true;
bool count_match = true;
long long count = -1;
bool match = true;
long long result;
long long r;
long long c;
type_t val;
int err;
int ch;
initialize_value (val);
/* Make sure it's been committed. */
__asm__ ("" : : : "memory");
if (fmt[1] == '*')
result = scanf_under_test (fmt, &count);
else
result = scanf_under_test (fmt, pointer_to_value (val), &count);
if (result < 0)
FAIL_RET (get_error_message (result), line);
do
ch = read_input ();
while (ch != ':' && ch != INPUT_ERROR && ch != INPUT_EOF);
if (ch != ':')
FAIL_RET (get_error_message (ch), line);
r = read_integer (&err);
if (err < 0)
FAIL_RET (get_error_message (err), line);
match &= r == result;
c = read_integer (&err);
if (err < 0)
FAIL_RET (get_error_message (err), line);
match &= (count_match = c == count);
if (r > 0)
{
match &= (value_match = verify_input (f, val, count, &err));
if (err < 0)
FAIL_RET (get_error_message (err), line);
}
ch = read_input ();
if (ch != '\n')
FAIL_RET (get_error_message (ch == INPUT_ERROR || ch == INPUT_EOF
? ch : INPUT_FORMAT), line);
if (!match)
{
if (r != result)
FAIL ("input line %zi: input assignment count mismatch: %lli",
line, result);
if (!count_match)
FAIL ("input line %zi: input character count mismatch: %lli",
line, count);
if (!value_match)
FAIL ("input line %zi: input value mismatch", line);
return -1;
}
return 0;
}
/* Consume a list of input records line by line supplied by READ_INPUT
above, discarding any that begin with the '#' line comment designator
and interpreting the initial part of the remaining ones from leading
'%' up to the first ':' field separator, which is removed from input,
by appending "%lln" to the part retrieved and handing over along with
the rest of input line to read to DO_SCANF above. Terminate upon the
end of input or the first processing error encountered.
See the top of this file for the definitions that have to be
provided by the source including this skeleton. */
int
do_test (void)
{
size_t fmt_size = 0;
char *fmt = NULL;
mtrace ();
int result = 0;
do
{
size_t i = 0;
int ch = 0;
char f;
line++;
do
{
f = ch;
ch = read_input ();
if ((i == 0 && ch == '#') || ch == INPUT_EOF || ch == INPUT_ERROR)
break;
if (i == fmt_size)
{
fmt_size += SIZE_CHUNK;
fmt = xrealloc (fmt, fmt_size);
}
fmt[i++] = ch;
}
while (ch != ':');
if (ch == INPUT_EOF && i == 0)
{
if (line == 1)
{
FAIL ("input line %zi: empty input", line);
result = -1;
}
break;
}
if (ch == INPUT_ERROR)
{
FAIL ("input line %zi: error reading format string: %m", line);
result = -1;
break;
}
if (ch == '#')
{
do
ch = read_input ();
while (ch != '\n' && ch != INPUT_EOF && ch != INPUT_ERROR);
if (ch == '\n')
continue;
if (ch == INPUT_EOF)
FAIL ("input line %zi: premature end of input reading comment",
line);
else
FAIL ("input line %zi: error reading comment: %m", line);
result = -1;
break;
}
if (ch != ':' || i < 3 || fmt[0] != '%')
{
FAIL ("input line %zi: format string format error: \"%.*s\"", line,
(int) (i - 1), fmt);
result = -1;
break;
}
if (i + 4 > fmt_size)
{
fmt_size += SIZE_CHUNK;
fmt = xrealloc (fmt, fmt_size);
}
fmt[i - 1] = '%';
fmt[i++] = 'l';
fmt[i++] = 'l';
fmt[i++] = 'n';
fmt[i++] = '\0';
result = do_scanf (f, fmt);
}
while (result == 0);
free (fmt);
return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
/* Interpose 'dladdr' with a stub to speed up malloc tracing. */
int
dladdr (const void *addr, Dl_info *info)
{
return 0;
}
#include <support/test-driver.c>
|