libctf: always name nameless types "", never NULL
authorNick Alcock <nick.alcock@oracle.com>
Fri, 29 Jan 2021 13:33:11 +0000 (13:33 +0000)
committerNick Alcock <nick.alcock@oracle.com>
Thu, 4 Feb 2021 16:01:53 +0000 (16:01 +0000)
The ctf_type_name_raw and ctf_type_aname_raw functions, which return the
raw, unadorned name of CTF types, have one unfortunate wrinkle: they
return NULL not only on error but when returning the name of types
without a name in writable dicts.  This was unintended: it not only
makes it impossible to reliably tell if a given call to
ctf_type_name_raw failed (due to a bad string offset say), but also
complicates all its callers, who now have to check for both NULL and "".

The written-out form of CTF has no concept of a NULL pointer instead of
a string: all null strings are strtab offset 0, "".  So the more we can
do to remove this distinction from the writable form, the less complex
the rest of our code needs to be.

Armour against NULL in multiple places, arranging to return "" from
ctf_type_name_raw if offset 0 is passed in, and removing a risky
optimization from ctf_str_add* that avoided doing anything if a NULL was
passed in: this added needless irregularity to the functions' API
surface, since "" and NULL should be treated identically, and in the
case of ctf_str_add_ref, we shouldn't skip adding the passed-in REF to
the list of references to be updated no matter what the content of the
string happens to be.

This means we can simplify the deduplicator a tiny bit, also fixing a
bug (latent when used by ld) where if the input dict was writable,
we failed to realise when types were nameless and could end up creating
deeply unhelpful synthetic forwards with no name, which we just banned
a few commits ago, so the link failed.

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

* ctf-string.c (ctf_str_add): Treat adding a NULL as adding "".
(ctf_str_add_ref): Likewise.
(ctf_str_add_external): Likewise.
* ctf-types.c (ctf_type_name_raw): Always return "" for offset 0.
* ctf-dedup.c (ctf_dedup_multiple_input_dicts): Don't armour
against NULL name.
(ctf_dedup_maybe_synthesize_forward): Likewise.

libctf/ChangeLog
libctf/ctf-dedup.c
libctf/ctf-string.c
libctf/ctf-types.c

index c700297abc6813051f78fc1b1ee4676cc599f35c..35c22d9fa48d214f1c67b4f14fc80bdc512c4b55 100644 (file)
@@ -1,3 +1,13 @@
+2021-01-27  Nick Alcock  <nick.alcock@oracle.com>
+
+       * ctf-string.c (ctf_str_add): Treat adding a NULL as adding "".
+       (ctf_str_add_ref): Likewise.
+       (ctf_str_add_external): Likewise.
+       * ctf-types.c (ctf_type_name_raw): Always return "" for offset 0.
+       * ctf-dedup.c (ctf_dedup_multiple_input_dicts): Don't armour
+       against NULL name.
+       (ctf_dedup_maybe_synthesize_forward): Likewise.
+
 2021-01-27  Nick Alcock  <nick.alcock@oracle.com>
 
        * ctf-create.c (ctf_serialize): Fix shadowing.
index da88ae37147bed1866131241d6f25e28a8f65858..001c2483a205e964f26bcbe85b4f15436a43676f 100644 (file)
@@ -1776,7 +1776,7 @@ ctf_dedup_multiple_input_dicts (ctf_dict_t *output, ctf_dict_t **inputs,
   name = ctf_type_name_raw (input_fp, input_id);
 
   if ((fwdkind == CTF_K_STRUCT || fwdkind == CTF_K_UNION)
-      && name && name[0] != '\0')
+      && name[0] != '\0')
     {
       const void *origin;
 
@@ -2375,20 +2375,19 @@ ctf_dedup_maybe_synthesize_forward (ctf_dict_t *output, ctf_dict_t *target,
   ctf_dedup_t *td = &target->ctf_dedup;
   int kind;
   int fwdkind;
-  const char *name;
+  const char *name = ctf_type_name_raw (input, id);
   const char *decorated;
   void *v;
   ctf_id_t emitted_forward;
 
   if (!ctf_dynset_exists (od->cd_conflicting_types, hval, NULL)
       || target->ctf_flags & LCTF_CHILD
-      || !ctf_type_name_raw (input, id)
+      || name[0] == '\0'
       || (((kind = ctf_type_kind_unsliced (input, id)) != CTF_K_STRUCT
           && kind != CTF_K_UNION && kind != CTF_K_FORWARD)))
     return 0;
 
   fwdkind = ctf_type_kind_forwarded (input, id);
-  name = ctf_type_name_raw (input, id);
 
   ctf_dprintf ("Using synthetic forward for conflicted struct/union with "
               "hval %s\n", hval);
index cb36f9166bc3cb99c1db00b16388e480329e4b7f..91ad2e36db7f76e8c1289c24ea077108c92fccca 100644 (file)
@@ -218,8 +218,9 @@ uint32_t
 ctf_str_add (ctf_dict_t *fp, const char *str)
 {
   ctf_str_atom_t *atom;
+
   if (!str)
-    return 0;
+    str = "";
 
   atom = ctf_str_add_ref_internal (fp, str, FALSE, TRUE, 0);
   if (!atom)
@@ -235,8 +236,9 @@ uint32_t
 ctf_str_add_ref (ctf_dict_t *fp, const char *str, uint32_t *ref)
 {
   ctf_str_atom_t *atom;
+
   if (!str)
-    return 0;
+    str = "";
 
   atom = ctf_str_add_ref_internal (fp, str, TRUE, TRUE, ref);
   if (!atom)
@@ -251,8 +253,9 @@ int
 ctf_str_add_external (ctf_dict_t *fp, const char *str, uint32_t offset)
 {
   ctf_str_atom_t *atom;
+
   if (!str)
-    return 0;
+    str = "";
 
   atom = ctf_str_add_ref_internal (fp, str, FALSE, FALSE, 0);
   if (!atom)
index 4129fbc7b835507dac3a4a812c69857f0d758316..57a284d82e75fe8a865e274c761e518312b0259a 100644 (file)
@@ -922,7 +922,10 @@ ctf_type_name (ctf_dict_t *fp, ctf_id_t type, char *buf, size_t len)
 }
 
 /* Lookup the given type ID and return its raw, unadorned, undecorated name.
-   The name will live as long as its ctf_dict_t does.  */
+   The name will live as long as its ctf_dict_t does.
+
+   The only decoration is that a NULL return always means an error: nameless
+   types return a null string.  */
 
 const char *
 ctf_type_name_raw (ctf_dict_t *fp, ctf_id_t type)
@@ -932,6 +935,9 @@ ctf_type_name_raw (ctf_dict_t *fp, ctf_id_t type)
   if ((tp = ctf_lookup_by_id (&fp, type)) == NULL)
     return NULL;               /* errno is set for us.  */
 
+  if (tp->ctt_name == 0)
+    return "";
+
   return ctf_strraw (fp, tp->ctt_name);
 }
 
This page took 0.028545 seconds and 4 git commands to generate.