1 /* Make sure serializing a dict (possibly repeatedly) does not corrupt either
2    type lookup or the string content of the dict.  */
3 
4 #include <ctf-api.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 
8 int
main(int argc,char * argv[])9 main (int argc, char *argv[])
10 {
11   ctf_dict_t *fp;
12   ctf_id_t zygal, autoschediastic;
13   ctf_snapshot_id_t snap;
14   unsigned char *foo;
15   size_t foo_size;
16   const char *bar;
17   int err;
18   char name[64];
19 
20   /* Adding things after serialization should not corrupt names created before
21      serialization.  */
22 
23   if ((fp = ctf_create (&err)) == NULL)
24     goto create_err;
25 
26   if ((zygal = ctf_add_struct (fp, CTF_ADD_ROOT, "zygal")) == CTF_ERR)
27     goto add_err;
28 
29   if ((foo = ctf_write_mem (fp, &foo_size, 4096)) == NULL)
30     goto write_err;
31   free (foo);
32 
33   if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL)
34     fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp)));
35   else
36     printf ("zygal's name is %s\n", name);
37 
38   if ((autoschediastic = ctf_add_enum (fp, CTF_ADD_ROOT, "autoschediastic")) == CTF_ERR)
39     goto add_err;
40 
41   if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL)
42     fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp)));
43   else
44     printf ("zygal's name is %s\n", name);
45 
46   /* Serializing again should not corrupt names either.  */
47   if ((foo = ctf_write_mem (fp, &foo_size, 4096)) == NULL)
48     goto write_err;
49   free (foo);
50 
51   if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL)
52     fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp)));
53   else
54     printf ("zygal's name is %s\n", name);
55 
56   /* Add another new name, roll back, and make sure the strings are
57      uncorrupted.  */
58 
59   snap = ctf_snapshot (fp);
60   if (ctf_add_enumerator (fp, autoschediastic, "aichmophobia", 0) < 0)
61     goto add_err;
62 
63   if (ctf_rollback (fp, snap) < 0)
64     goto roll_err;
65 
66   if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL)
67     fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp)));
68   else
69     printf ("zygal's name is %s after first rollback\n", name);
70 
71   if (ctf_type_name (fp, autoschediastic, name, sizeof (name)) == NULL)
72     fprintf (stderr, "Can't get name of autoschediastic: %s\n", ctf_errmsg (ctf_errno (fp)));
73   else
74     printf ("autoschediastic's name is %s after first rollback\n", name);
75 
76   ctf_dict_close (fp);
77   return 0;
78 
79  create_err:
80   fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err));
81   return 1;
82  add_err:
83   fprintf (stderr, "Cannot add: %s\n", ctf_errmsg (ctf_errno (fp)));
84   return 1;
85  write_err:
86   fprintf (stderr, "Cannot serialize: %s\n", ctf_errmsg (ctf_errno (fp)));
87   return 1;
88  roll_err:
89   fprintf (stderr, "Cannot roll back: %s\n", ctf_errmsg (ctf_errno (fp)));
90   return 1;
91 }
92