diff options
Diffstat (limited to 'libctf/testsuite')
-rw-r--r-- | libctf/testsuite/libctf-writable/reserialize-strtab-corruption.c | 91 | ||||
-rw-r--r-- | libctf/testsuite/libctf-writable/reserialize-strtab-corruption.lk | 5 |
2 files changed, 96 insertions, 0 deletions
diff --git a/libctf/testsuite/libctf-writable/reserialize-strtab-corruption.c b/libctf/testsuite/libctf-writable/reserialize-strtab-corruption.c new file mode 100644 index 0000000..1593325 --- /dev/null +++ b/libctf/testsuite/libctf-writable/reserialize-strtab-corruption.c @@ -0,0 +1,91 @@ +/* Make sure serializing a dict (possibly repeatedly) does not corrupt either + type lookup or the string content of the dict. */ + +#include <ctf-api.h> +#include <stdio.h> +#include <stdlib.h> + +int +main (int argc, char *argv[]) +{ + ctf_dict_t *fp; + ctf_id_t zygal, autoschediastic; + ctf_snapshot_id_t snap; + unsigned char *foo; + size_t foo_size; + const char *bar; + int err; + char name[64]; + + /* Adding things after serialization should not corrupt names created before + serialization. */ + + if ((fp = ctf_create (&err)) == NULL) + goto create_err; + + if ((zygal = ctf_add_struct (fp, CTF_ADD_ROOT, "zygal")) == CTF_ERR) + goto add_err; + + if ((foo = ctf_write_mem (fp, &foo_size, 4096)) == NULL) + goto write_err; + free (foo); + + if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL) + fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp))); + else + printf ("zygal's name is %s\n", name); + + if ((autoschediastic = ctf_add_enum (fp, CTF_ADD_ROOT, "autoschediastic")) == CTF_ERR) + goto add_err; + + if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL) + fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp))); + else + printf ("zygal's name is %s\n", name); + + /* Serializing again should not corrupt names either. */ + if ((foo = ctf_write_mem (fp, &foo_size, 4096)) == NULL) + goto write_err; + free (foo); + + if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL) + fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp))); + else + printf ("zygal's name is %s\n", name); + + /* Add another new name, roll back, and make sure the strings are + uncorrupted. */ + + snap = ctf_snapshot (fp); + if (ctf_add_enumerator (fp, autoschediastic, "aichmophobia", 0) < 0) + goto add_err; + + if (ctf_rollback (fp, snap) < 0) + goto roll_err; + + if (ctf_type_name (fp, zygal, name, sizeof (name)) == NULL) + fprintf (stderr, "Can't get name of zygal: %s\n", ctf_errmsg (ctf_errno (fp))); + else + printf ("zygal's name is %s after first rollback\n", name); + + if (ctf_type_name (fp, autoschediastic, name, sizeof (name)) == NULL) + fprintf (stderr, "Can't get name of autoschediastic: %s\n", ctf_errmsg (ctf_errno (fp))); + else + printf ("autoschediastic's name is %s after first rollback\n", name); + + ctf_dict_close (fp); + return 0; + + create_err: + fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err)); + return 1; + add_err: + fprintf (stderr, "Cannot add: %s\n", ctf_errmsg (ctf_errno (fp))); + return 1; + write_err: + fprintf (stderr, "Cannot serialize: %s\n", ctf_errmsg (ctf_errno (fp))); + return 1; + roll_err: + fprintf (stderr, "Cannot roll back: %s\n", ctf_errmsg (ctf_errno (fp))); + return 1; +} diff --git a/libctf/testsuite/libctf-writable/reserialize-strtab-corruption.lk b/libctf/testsuite/libctf-writable/reserialize-strtab-corruption.lk new file mode 100644 index 0000000..58f7a64 --- /dev/null +++ b/libctf/testsuite/libctf-writable/reserialize-strtab-corruption.lk @@ -0,0 +1,5 @@ +zygal's name is struct zygal +zygal's name is struct zygal +zygal's name is struct zygal +zygal's name is struct zygal after first rollback +autoschediastic's name is enum autoschediastic after first rollback |