Files
binutils-gdb/libctf/testsuite/libctf-writable/ctf-compressed.c
Nick Alcock 14303d6295 libctf: archive, open: when opening, always set errp to something
ctf_arc_import_parent, called by the cached-opening machinery used by
ctf_archive_next and archive-wide lookup functions like
ctf_arc_lookup_symbol, has an err-pointer parameter like all other opening
functions.  Unfortunately it unconditionally initializes it whenever
provided, even if there was no error, which can lead to its being
initialized to an uninitialized value.  This is not technically an
API-contract violation, since we don't define what happens to the error
value except when an error happens, but it is still unpleasant.

Initialize it only when there is an actual error, so we never initialize it
to an uninitialized value.

While we're at it, improve all the opening pathways: on success, set errp to
0, rather than leaving it what it was, reducing the likelihood of
uninitialized error param returns in callers too.  (This is inconsistent
with the treatment of ctf_errno(), but the err value being a parameter
passed in from outside makes the divergence acceptable: in open functions,
you're never going to be overwriting some old error value someone might want
to keep around across multiple calls, some of which are successful and some
of which are not.)

Soup up existing tests to verify all this.

Thanks to Bruce McCulloch for the original patch, and Stephen Brennan for
the report.

libctf/
	PR libctf/32903
	* ctf-archive.c (ctf_arc_open_internal): Zero errp on success.
	(ctf_dict_open_sections): Zero errp at the start.
	(ctf_arc_import_parent): Intialize err.
	* ctf-open.c (ctf_bufopen): Zero errp at the start.
	* testsuite/libctf-lookup/add-to-opened.c: Make sure one-element
	archive opens update errp.
	* testsuite/libctf-writable/ctf-compressed.c: Make sure real archive
	opens update errp.
2025-05-23 12:34:35 +01:00

163 lines
4.3 KiB
C

/* Make sure linking and writing an archive with a low threshold correctly
compresses it. (This tests for two bugs, one where archives weren't
serialized regardless of their threshold, and another where nothing was.) */
#include <ctf-api.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main (int argc, char *argv[])
{
ctf_dict_t *in1;
ctf_dict_t *in2;
ctf_dict_t *fp;
ctf_dict_t *dump_fp;
ctf_archive_t *arc1, *arc2;
ctf_archive_t *final_arc;
ctf_sect_t s1, s2;
ctf_encoding_t encoding = { CTF_INT_SIGNED, 0, sizeof (char) };
ctf_encoding_t encoding2 = { CTF_INT_SIGNED, 0, sizeof (long long) };
unsigned char *buf1, *buf2, *buf3;
size_t buf1_sz, buf2_sz, buf3_sz;
ctf_dump_state_t *dump_state = NULL;
ctf_next_t *i = NULL;
int err;
/* Linking does not currently work on mingw because of an unreliable tmpfile
implementation on that platform (see
https://github.com/msys2/MINGW-packages/issues/18878). Simply skip for
now. */
#ifdef __MINGW32__
printf ("UNSUPPORTED: platform bug breaks ctf_link\n");
return 0;
#else
if ((fp = ctf_create (&err)) == NULL)
goto create_err;
if ((in1 = ctf_create (&err)) == NULL)
goto create_err;
if ((in2 = ctf_create (&err)) == NULL)
goto create_err;
/* Force a conflict to get an archive created. */
if ((ctf_add_integer (in1, CTF_ADD_ROOT, "foo", &encoding)) == CTF_ERR)
{
fprintf (stderr, "Cannot add: %s\n", ctf_errmsg (ctf_errno (in1)));
return 1;
}
if ((ctf_add_integer (in2, CTF_ADD_ROOT, "foo", &encoding2)) == CTF_ERR)
{
fprintf (stderr, "Cannot add: %s\n", ctf_errmsg (ctf_errno (in2)));
return 1;
}
/* Write them out and read them back in, to turn them into archives.
This would be unnecessary if ctf_link_add() were public :( */
if ((buf1 = ctf_write_mem (in1, &buf1_sz, -1)) == NULL)
{
fprintf (stderr, "Cannot serialize: %s\n", ctf_errmsg (ctf_errno (in1)));
return 1;
}
if ((buf2 = ctf_write_mem (in2, &buf2_sz, -1)) == NULL)
{
fprintf (stderr, "Cannot serialize: %s\n", ctf_errmsg (ctf_errno (in2)));
return 1;
}
s1.cts_name = "foo";
s2.cts_name = "bar";
s1.cts_data = (void *) buf1;
s2.cts_data = (void *) buf2;
s1.cts_size = buf1_sz;
s2.cts_size = buf2_sz;
s1.cts_entsize = 64; /* Unimportant. */
s2.cts_entsize = 64; /* Unimportant. */
if ((arc1 = ctf_arc_bufopen (&s1, NULL, NULL, &err)) == NULL ||
(arc2 = ctf_arc_bufopen (&s2, NULL, NULL, &err)) == NULL)
goto open_err;
ctf_dict_close (in1);
ctf_dict_close (in2);
/* Link them together. */
if (ctf_link_add_ctf (fp, arc1, "a") < 0 ||
ctf_link_add_ctf (fp, arc2, "b") < 0)
goto link_err;
if (ctf_link (fp, 0) < 0)
goto link_err;
/* Write them out. We need a new buf here, because the archives are still
using the other two bufs. */
if ((buf3 = ctf_link_write (fp, &buf3_sz, 1)) == NULL)
goto link_err;
/* Read them back in. */
s1.cts_data = (void *) buf3;
s1.cts_size = buf3_sz;
if ((final_arc = ctf_arc_bufopen (&s1, NULL, NULL, &err)) == NULL)
goto open_err;
if (ctf_archive_count (final_arc) != 2)
{
fprintf (stderr, "Archive is the wrong length: %zi.\n", ctf_archive_count (final_arc));
return -1;
}
/* Dump the header of each archive member, and search for CTF_F_COMPRESS in
the resulting dump. */
err = 666;
while ((dump_fp = ctf_archive_next (final_arc, &i, NULL, 0, &err)) != NULL)
{
char *dumpstr;
if (err != 0)
fprintf (stderr, "err not set to success on success\n");
while ((dumpstr = ctf_dump (dump_fp, &dump_state, CTF_SECT_HEADER,
NULL, NULL)) != NULL)
{
if (strstr (dumpstr, "CTF_F_COMPRESS") != NULL)
printf ("Output is compressed.\n");
free (dumpstr);
}
ctf_dict_close (dump_fp);
}
if (err != ECTF_NEXT_END)
{
fprintf (stderr, "Archive iteation error: %s\n", ctf_errmsg (err));
return 1;
}
ctf_arc_close (final_arc);
free (buf1);
free (buf2);
free (buf3);
ctf_dict_close (fp);
return 0;
create_err:
fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err));
return 1;
open_err:
fprintf (stderr, "Cannot open: %s\n", ctf_errmsg (err));
return 1;
link_err:
fprintf (stderr, "Cannot link: %s\n", ctf_errmsg (ctf_errno (fp)));
return 1;
#endif
}