Skip to content

Commit 296ffb5

Browse files
committed
libctf, create: fix ctf_type_add of structs with unnamed members
Our recent commit to support unnamed structure members better ditched the old ctf_member_iter iterator body in favour of ctf_member_next. However, these functions treat unnamed structure members differently: ctf_member_iter just returned whatever the internal representation contained, while ctf_member_next took care to always return "" rather than sometimes returning "" and sometimes NULL depending on whether the dict was dynamic (a product of ctf_create) or not (a product of ctf_open). After this commit, ctf_member_iter did the same. It was always a bug for external callers not to treat a "" return from these functions as if it were NULL, so only buggy callers could be affected -- but one of those buggy callers was ctf_add_type, which assumed that it could just take whatever name was returned from ctf_member_iter and slam it directly into the internal representation of a dynamic dict -- which expects NULL for unnamed members, not "". The net effect of all of this is that taking a struct containing unnamed members and ctf_add_type'ing it into a dynamic dict produced a dict whose unnamed members were inaccessible to ctf_member_info (though if you wrote that dict out and then ctf_open'ed it, they would magically reappear again). Compensate for this by suitably transforming a "" name into NULL in the internal representation, as should have been done all along. libctf/ChangeLog 2021-01-19 Nick Alcock <nick.alcock@oracle.com> * ctf-create.c (membadd): Transform ""-named members into NULL-named ones. * testsuite/libctf-regression/type-add-unnamed-struct*: New test. (cherry picked from commit 26503e2)
1 parent b7540e2 commit 296ffb5

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

libctf/ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2021-01-19 Nick Alcock <nick.alcock@oracle.com>
2+
3+
* ctf-create.c (membadd): Transform ""-named members into
4+
NULL-named ones.
5+
* testsuite/libctf-regression/type-add-unnamed-struct*: New test.
6+
17
2021-01-19 Nick Alcock <nick.alcock@oracle.com>
28

39
* ctf-lookup.c (ctf_lookup_by_name_internal): Do not return the

libctf/ctf-create.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,6 +2403,12 @@ membadd (const char *name, ctf_id_t type, unsigned long offset, void *arg)
24032403
if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
24042404
return (ctf_set_errno (ctb->ctb_dict, EAGAIN));
24052405

2406+
/* Unnamed members in non-dynamic dicts have a name of "", while dynamic dicts
2407+
use NULL. Adapt. */
2408+
2409+
if (name[0] == 0)
2410+
name = NULL;
2411+
24062412
if (name != NULL && (s = strdup (name)) == NULL)
24072413
{
24082414
free (dmd);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct foo
2+
{
3+
union
4+
{
5+
struct
6+
{
7+
int bar;
8+
};
9+
};
10+
union
11+
{
12+
struct
13+
{
14+
int baz;
15+
};
16+
};
17+
};
18+
19+
struct foo *bar;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <ctf-api.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
int
6+
main (int argc, char *argv[])
7+
{
8+
ctf_dict_t *fp;
9+
ctf_archive_t *ctf;
10+
ctf_dict_t *dyn;
11+
ctf_next_t *i = NULL;
12+
ctf_id_t type;
13+
ctf_id_t newtype;
14+
const char *memb;
15+
ctf_membinfo_t mi;
16+
const char *membs[] = { "bar", "baz", NULL };
17+
const char **walk;
18+
int err;
19+
20+
if (argc != 2)
21+
{
22+
fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
23+
exit(1);
24+
}
25+
26+
if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
27+
goto open_err;
28+
29+
if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
30+
goto open_err;
31+
32+
if ((dyn = ctf_create (&err)) == NULL)
33+
goto create_err;
34+
35+
/* Copy 'struct foo' into the dynamic dict, then make sure we can look up a
36+
member situated inside an unnamed struct. */
37+
38+
if ((type = ctf_lookup_by_name (fp, "struct foo")) == CTF_ERR)
39+
{
40+
fprintf (stderr, "Cannot look up struct foo: %s\n", ctf_errmsg (ctf_errno (dyn)));
41+
return 1;
42+
}
43+
44+
if ((newtype = ctf_add_type (dyn, fp, type)) == CTF_ERR)
45+
goto copy_err;
46+
47+
for (walk = membs; *walk != NULL; walk++)
48+
{
49+
if (ctf_member_info (dyn, newtype, *walk, &mi) < 0)
50+
goto lookup_err;
51+
printf ("Looked up %s, type %lx, offset %lx\n", *walk, (long) mi.ctm_type, mi.ctm_offset);
52+
}
53+
54+
ctf_dict_close (dyn);
55+
ctf_dict_close (fp);
56+
ctf_close (ctf);
57+
58+
return 0;
59+
60+
open_err:
61+
fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
62+
return 1;
63+
create_err:
64+
fprintf (stderr, "%s: cannot create: %s\n", argv[0], ctf_errmsg (err));
65+
return 1;
66+
copy_err:
67+
fprintf (stderr, "Type addition failed: %s\n", ctf_errmsg (ctf_errno (dyn)));
68+
return 1;
69+
lookup_err:
70+
fprintf (stderr, "Cannot look up %s: %s\n", *walk, ctf_errmsg (ctf_errno (dyn)));
71+
return 1;
72+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# source: type-add-unnamed-struct-ctf.c
2+
Looked up bar, type [0-9a-f]*, offset [0-9a-f]*
3+
Looked up baz, type [0-9a-f]*, offset [0-9a-f]*

0 commit comments

Comments
 (0)