binutils-gdb/libctf/testsuite/libctf-writable/reserialize-strtab-corruption.c
Nick Alcock e695879142 libctf, testsuite: fix various warnings in tests
These warnings are all off by default, but if they do fire you get
spurious ERRORs when running make check-libctf.

libctf/ChangeLog
2021-09-27  Nick Alcock  <nick.alcock@oracle.com>

	* testsuite/libctf-lookup/enum-symbol.c: Remove unused label.
	* testsuite/libctf-lookup/conflicting-type-syms.c: Remove unused
	variables.
	* testsuite/libctf-regression/pptrtab.c: Likewise.
	* testsuite/libctf-regression/type-add-unnamed-struct.c: Likewise.
	* testsuite/libctf-writable/pptrtab.c: Likewise.
	* testsuite/libctf-writable/reserialize-strtab-corruption.c:
	Likewise.
	* testsuite/libctf-regression/nonstatic-var-section-ld-r.c: Fix
	format string.
	* testsuite/libctf-regression/nonstatic-var-section-ld.c:
	Likewise.
	* testsuite/libctf-regression/nonstatic-var-section-ld.lk: Adjust.
	* testsuite/libctf-writable/symtypetab-nonlinker-writeout.c: Fix
	initializer.
2021-09-27 20:31:26 +01:00

91 lines
2.7 KiB
C

/* 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;
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;
}