Skip to content

Commit 03b23b9

Browse files
simarkeepp
authored andcommitted
Fix: set proper size when creating new strings
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. Signed-off-by: Simon Marchi <[email protected]> Signed-off-by: Philippe Proulx <[email protected]> Change-Id: I147cdaaa7cff00ee06ec4c98bd9423a975ddd770
1 parent 7e38c84 commit 03b23b9

File tree

2 files changed

+2
-0
lines changed

2 files changed

+2
-0
lines changed

src/ctf-writer/fields.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common *field,
184184
goto end;
185185
}
186186

187+
g_array_set_size(string->buf, 1);
187188
g_array_index(string->buf, char, 0) = '\0';
188189
BT_LOGD("Initialized common string field object: addr=%p, ft-addr=%p",
189190
field, type);

src/lib/trace-ir/field.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ struct bt_field *create_string_field(struct bt_field_class *fc)
398398
goto end;
399399
}
400400

401+
g_array_set_size(string_field->buf, 1);
401402
g_array_index(string_field->buf, char, 0) = '\0';
402403
BT_LIB_LOGD("Created string field object: %!+f", string_field);
403404

0 commit comments

Comments
 (0)