Fix: ctf: verify that field class is int before calling ctf_field_class_as_int
[babeltrace.git] / src / plugins / ctf / common / metadata / visitor-generate-ir.cpp
index 50d8d441fe7ed312a16b0ab99762950bc8a332b6..df7574060c5c023818cfff5bef607db94a35e2f3 100644 (file)
@@ -23,6 +23,7 @@
 #include <glib.h>
 #include <inttypes.h>
 #include <errno.h>
+#include <string>
 #include "common/common.h"
 #include "common/uuid.h"
 #include "compat/endian.h"
@@ -265,23 +266,9 @@ end:
 static GQuark get_prefixed_named_quark(struct ctf_visitor_generate_ir *ctx, char prefix,
                                        const char *name)
 {
-    GQuark qname = 0;
-
     BT_ASSERT(name);
-
-    /* Prefix character + original string + '\0' */
-    char *prname = g_new(char, strlen(name) + 2);
-    if (!prname) {
-        _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to allocate a string.");
-        goto end;
-    }
-
-    sprintf(prname, "%c%s", prefix, name);
-    qname = g_quark_from_string(prname);
-    g_free(prname);
-
-end:
-    return qname;
+    std::string prname = std::string {prefix} + name;
+    return g_quark_from_string(prname.c_str());
 }
 
 /**
@@ -1140,10 +1127,8 @@ static int visit_field_class_declarator(struct ctf_visitor_generate_ir *ctx,
 
     /* Find the right nested declaration if not provided */
     if (!nested_decl) {
-        struct bt_list_head *pointers =
-            &node_field_class_declarator->u.field_class_declarator.pointers;
-
-        if (node_field_class_declarator && !bt_list_empty(pointers)) {
+        if (node_field_class_declarator &&
+            !bt_list_empty(&node_field_class_declarator->u.field_class_declarator.pointers)) {
             GQuark qalias;
 
             /*
@@ -3313,21 +3298,21 @@ static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir *c
                                                struct ctf_field_class *fc)
 {
     struct ctf_clock_class *clock_class_to_map_to = NULL;
-    struct ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
-    int ret = 0;
     uint64_t clock_class_count;
 
     if (!fc) {
-        goto end;
+        return 0;
     }
 
     if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) {
-        goto end;
+        return 0;
     }
 
+    ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
+
     if (int_fc->mapped_clock_class) {
         /* Already mapped */
-        goto end;
+        return 0;
     }
 
     clock_class_count = ctx->ctf_tc->clock_classes->len;
@@ -3343,7 +3328,6 @@ static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir *c
         BT_ASSERT(clock_class_to_map_to);
         clock_class_to_map_to->frequency = UINT64_C(1000000000);
         g_string_assign(clock_class_to_map_to->name, "default");
-        BT_ASSERT(ret == 0);
         g_ptr_array_add(ctx->ctf_tc->clock_classes, clock_class_to_map_to);
         break;
     case 1:
@@ -3361,15 +3345,13 @@ static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir *c
         _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
             "Timestamp field found with no mapped clock class, "
             "but there's more than one clock class in the trace at this point.");
-        ret = -1;
-        goto end;
+        return -1;
     }
 
     BT_ASSERT(clock_class_to_map_to);
     int_fc->mapped_clock_class = clock_class_to_map_to;
 
-end:
-    return ret;
+    return 0;
 }
 
 static int auto_map_fields_to_trace_clock_class(struct ctf_visitor_generate_ir *ctx,
This page took 0.027151 seconds and 4 git commands to generate.