Skip to content

Commit 29736fc

Browse files
committed
libctf: check for problems with error returns
We do this as a writable test because the only known-affected platforms (with ssize_t longer than unsigned long) use PE, and we do not have support for CTF linkage in the PE linker yet. PR libctf/30836 * libctf/testsuite/libctf-writable/libctf-errors.*: New test.
1 parent fded0fb commit 29736fc

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* Make sure that error returns are correct. Usually this is trivially
2+
true, but on platforms with unusual type sizes all the casting might
3+
cause problems with unexpected sign-extension and truncation. */
4+
5+
#include <ctf-api.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
int
10+
main (int argc, char *argv[])
11+
{
12+
ctf_dict_t *fp;
13+
ctf_next_t *i = NULL;
14+
size_t boom = 0;
15+
ctf_id_t itype, stype;
16+
ctf_encoding_t encoding = {0};
17+
ctf_membinfo_t mi;
18+
ssize_t ret;
19+
int err;
20+
21+
if ((fp = ctf_create (&err)) == NULL)
22+
{
23+
fprintf (stderr, "%s: cannot create: %s\n", argv[0], ctf_errmsg (err));
24+
return 1;
25+
}
26+
27+
/* First error class: int return. */
28+
29+
if (ctf_member_count (fp, 1024) >= 0)
30+
fprintf (stderr, "int return: non-error return: %i\n",
31+
ctf_member_count(fp, 1024));
32+
33+
/* Second error class: type ID return. */
34+
35+
if (ctf_type_reference (fp, 1024) != CTF_ERR)
36+
fprintf (stderr, "ctf_id_t return: non-error return: %li\n",
37+
ctf_type_reference (fp, 1024));
38+
39+
/* Third error class: ssize_t return. Create a type to iterate over first. */
40+
41+
if ((itype = ctf_add_integer (fp, CTF_ADD_ROOT, "int", &encoding)) == CTF_ERR)
42+
fprintf (stderr, "cannot add int: %s\n", ctf_errmsg (ctf_errno (fp)));
43+
else if ((stype = ctf_add_struct (fp, CTF_ADD_ROOT, "foo")) == CTF_ERR)
44+
fprintf (stderr, "cannot add struct: %s\n", ctf_errmsg (ctf_errno (fp)));
45+
else if (ctf_add_member (fp, stype, "bar", itype) < 0)
46+
fprintf (stderr, "cannot add member: %s\n", ctf_errmsg (ctf_errno (fp)));
47+
48+
if (ctf_member_info (fp, stype, "bar", &mi) < 0)
49+
fprintf (stderr, "cannot get member info: %s\n", ctf_errmsg (ctf_errno (fp)));
50+
51+
/* Iteration should never produce an offset bigger than the offset just returned,
52+
and should quickly terminate. */
53+
54+
while ((ret = ctf_member_next (fp, stype, &i, NULL, NULL, 0)) >= 0) {
55+
if (ret > mi.ctm_offset)
56+
fprintf (stderr, "ssize_t return: unexpected offset: %zi\n", ret);
57+
if (boom++ > 1000)
58+
{
59+
fprintf (stderr, "member iteration went on way too long\n");
60+
exit (1);
61+
}
62+
}
63+
64+
/* Fourth error class (trivial): pointer return. */
65+
if (ctf_type_aname (fp, 1024) != NULL)
66+
fprintf (stderr, "pointer return: non-error return: %p\n",
67+
ctf_type_aname (fp, 1024));
68+
69+
ctf_file_close (fp);
70+
71+
printf("All done.\n");
72+
73+
return 0;
74+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
All done.

0 commit comments

Comments
 (0)