Fix: set proper size when creating new strings
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 19 Sep 2023 19:41:11 +0000 (15:41 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 20 Oct 2023 23:29:14 +0000 (19:29 -0400)
After adding a wrapper to g_array_index that validates that accessed
indices don't go past the end of the arrays, I got this:

    (╯°□°)╯︵ ┻━┻  /home/smarchi/src/babeltrace/src/lib/trace-ir/field.c:392: create_string_field(): Assertion `0 < string_field->buf->len` failed.

The problem is that arrays backing string fields are created with:

    string->buf = g_array_sized_new(FALSE, FALSE, sizeof(char), 1);

This g_array_sized_new call reserves space for one element, but doesn't
actually make the length of the array one.  Add a call to
g_array_set_size to fix that.  Fix another occurence of the same bug in
ctf-writer.

Change-Id: I2b90b26c37d70d64bcd825b5b07b204bcbb5baf5
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10904
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/ctf-writer/fields.c
src/lib/trace-ir/field.c

index 5334dddd54b5e9847092877a9497337991ac4f11..3d6e35236ac61cde337b145524058e4690c4f9f8 100644 (file)
@@ -162,6 +162,7 @@ int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common *field,
                goto end;
        }
 
+       g_array_set_size(string->buf, 1);
        g_array_index(string->buf, char, 0) = '\0';
        BT_LOGD("Initialized common string field object: addr=%p, ft-addr=%p",
                field, type);
index c07ab363cad98b0da8545779b14efe55b9a9e35d..41ea5f642cb9ef11ebca33fd7acd707b62357ac1 100644 (file)
@@ -388,6 +388,7 @@ struct bt_field *create_string_field(struct bt_field_class *fc)
                goto end;
        }
 
+       g_array_set_size(string_field->buf, 1);
        g_array_index(string_field->buf, char, 0) = '\0';
        BT_LIB_LOGD("Created string field object: %!+f", string_field);
 
This page took 0.025851 seconds and 4 git commands to generate.