Ticket #156: ytest.c

File ytest.c, 1.3 KB (added by ppelletier@…, 4 years ago)

a program which demonstrates the bug

Line 
1// Demonstrates bug: http://pyyaml.org/ticket/156
2
3// Warning: running this program will create a 125 megabyte
4// file "foo.yaml" in the current directory!
5
6#include <yaml.h>
7#include <stdlib.h>
8#include <stdio.h>
9#include <stdbool.h>
10
11int main (int argc, char **argv)
12{ printf ("libYaml version %s on a %zd-bit machine\n",
13          yaml_get_version_string (), 8 * sizeof (void *));
14
15  int i;
16  FILE *f = fopen ("foo.yaml", "w");
17  for (i = 0 ; i < 5000000 ; i++)
18    fprintf (f, "---\nfoo: bar\nbaz: bob\n...\n");
19  fclose (f);
20
21  yaml_parser_t parser;
22  yaml_event_t event;
23  bool done = false;
24
25  yaml_parser_initialize (&parser);
26  f = fopen ("foo.yaml", "r");
27  yaml_parser_set_input_file (&parser, f);
28
29  while (!done)
30    { if (!yaml_parser_parse (&parser, &event))
31        goto error;
32
33      done = (event.type == YAML_STREAM_END_EVENT);
34      yaml_event_delete (&event);
35    }
36
37  yaml_parser_delete (&parser);
38  printf ("Success!\n");
39  return EXIT_SUCCESS;
40
41 error:
42  printf ("parser error %d\n", parser.error);
43  if (parser.context)
44    printf ("context: %s at line %zd\n",
45            parser.context, parser.context_mark.line + 1);
46  if (parser.problem)
47    printf ("problem: %s at line %zd\n",
48            parser.problem, parser.problem_mark.line + 1);
49
50  yaml_parser_delete (&parser);
51  printf ("Failure!\n");
52  return EXIT_FAILURE;
53}