libctf: fix slices of slices and of enums

Slices had a bunch of horrible usability problems.  In particular, while
towers of cv-quals are resolved away by functions that need to do it, towers
of cv-quals with slices in the middle are not resolved away by functions
like ctf_enum_value that can see through slices: resolving volatile -> slice
-> const -> enum will leave it with a 'const', which will error pointlessly,
annoying callers, who reasonably expect slices to be more invisible than
this.  (The user-callable ctf_type_resolve still does not resolve away
slices, because this is the only way users can see that the slices are there
at all.)

This is induced by a fix for another wart: ctf_add_enumerator does not
resolve anything away at all, so you can't even add enumerators to const or
volatile enums -- and more problematically, you can't add enumerators to
enums with an explicit encoding without resolving away the types by hand,
since ctf_add_enum_encoded works by returning a slice!  ctf_add_enumerator
now resolves away all of those, so any cvr-or-typedef-or-slice-qual
terminating in an enum can be added to, exactly as callers likely expect.

(New tests added.)

libctf/
	* ctf-create.c (ctf_add_enumerator): Resolve away cvr-qualness.
	* ctf-types.c (ctf_type_resolve_unsliced): Don't terminate at
	the first slice.
	* testsuite/libctf-writable/slice-of-slice.*: New test.
This commit is contained in:
Nick Alcock
2025-02-11 14:32:40 +00:00
parent bf89fce0cf
commit 83e8a5d39b
4 changed files with 98 additions and 6 deletions

View File

@@ -594,21 +594,31 @@ ctf_type_resolve_unsliced (ctf_dict_t *fp, ctf_id_t type)
{
ctf_dict_t *ofp = fp;
const ctf_type_t *tp;
ctf_id_t resolved_type;
if ((type = ctf_type_resolve (fp, type)) == CTF_ERR)
return CTF_ERR;
if ((tp = ctf_lookup_by_id (&fp, type)) == NULL)
return CTF_ERR; /* errno is set for us. */
resolved_type = type;
if ((LCTF_INFO_KIND (fp, tp->ctt_info)) == CTF_K_SLICE)
do
{
ctf_id_t ret;
type = resolved_type;
if ((ret = ctf_type_reference (fp, type)) == CTF_ERR)
return (ctf_set_typed_errno (ofp, ctf_errno (fp)));
return ret;
if ((LCTF_INFO_KIND (fp, tp->ctt_info)) == CTF_K_SLICE)
if ((type = ctf_type_reference (fp, type)) == CTF_ERR)
return (ctf_set_typed_errno (ofp, ctf_errno (fp)));
if ((resolved_type = ctf_type_resolve (fp, type)) == CTF_ERR)
return CTF_ERR;
if ((tp = ctf_lookup_by_id (&fp, resolved_type)) == NULL)
return CTF_ERR; /* errno is set for us. */
}
while (LCTF_INFO_KIND (fp, tp->ctt_info) == CTF_K_SLICE);
return type;
}