]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
libctf: don't leak enums if ctf_add_type fails
authorNick Alcock <nick.alcock@oracle.com>
Tue, 11 Jun 2024 19:11:29 +0000 (20:11 +0100)
committerNick Alcock <nick.alcock@oracle.com>
Tue, 18 Jun 2024 12:20:31 +0000 (13:20 +0100)
If ctf_add_type failed in the middle of enumerator addition, the
destination would end up containing the source enum type and some
but not all of its enumerator constants.

Use snapshots to roll back the enum addition as a whole if this happens.
Before now, it's been pretty unlikely, but in an upcoming commit we will ban
addition of enumerators that already exist in a given dict, making failure
of ctf_add_enumerator and thus of this part of ctf_add_type much more
likely.

libctf/
* ctf-create.c (ctf_add_type_internal): Roll back if enum or
          enumerator addition fails.

libctf/ctf-create.c

index ee79e49794db5df2eb5453e166dda0c501f2a234..073006b24ea9c8e28f2e31827fd504dd25dbb8e8 100644 (file)
@@ -1944,10 +1944,15 @@ ctf_add_type_internal (ctf_dict_t *dst_fp, ctf_dict_t *src_fp, ctf_id_t src_type
        }
       else
        {
+         ctf_snapshot_id_t snap = ctf_snapshot (dst_fp);
+
          dst_type = ctf_add_enum (dst_fp, flag, name);
          if ((dst.ctb_type = dst_type) == CTF_ERR
              || ctf_enum_iter (src_fp, src_type, enumadd, &dst))
-           return CTF_ERR;                     /* errno is set for us */
+           {
+             ctf_rollback (dst_fp, snap);
+             return CTF_ERR;                   /* errno is set for us */
+           }
        }
       break;