From e58dfd9c9fd5ebde13a904b8ccdccbe576e440b4 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Fri, 5 Jul 2019 11:19:32 -0400 Subject: [PATCH] Fix: src.ctf.fs: pointer arithmetics on non-adjacent memory Issue ===== When indexing a trace using `*.idx` files, if streams span more than one packet the user may witness the following warning message: W PLUGIN/SRC.CTF.FS/DS build_index_from_idx_file@data-stream-file.c:400 [source-ctf-fs] Invalid, non-monotonic, packet offset encountered in LTTng trace index file: previous offset=14757395258967641292, current offset=4096 This is caused by the fact the we're using pointer arithmetics to get the pointer to the previous entry of an array. This ends returning garbage because it's a pointer array and not a regular array storing the objects as the code expects to. This regression was most probably introduced by the following commit: commit 7ed5243afe12c7c12fa5305fff99b93ad23bbdff Author: Francis Deslauriers Date: Wed May 15 14:59:10 2019 -0400 src.ctf.fs: merge all indexes to the fs_ds_group level This problematic commit changes the `entries` fields of the `struct ctf_fs_ds_index` from a `GArray *` to `GPtrArray * without changing the pointer arithmetics. This was not an error because indexing using `*.idx` files is optional and indexing may fail back to direct packet indexing. Solution ======== Use a local variable to store a pointer to the previous index_entry rather than using pointer arithmetics. Drawbacks ========= None. Signed-off-by: Francis Deslauriers Change-Id: I3e5608d0359be2a447e79415517068c64f5a2817 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1637 Tested-by: jenkins Reviewed-by: Philippe Proulx --- src/plugins/ctf/fs-src/data-stream-file.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/plugins/ctf/fs-src/data-stream-file.c b/src/plugins/ctf/fs-src/data-stream-file.c index b4022c32..213ccc9b 100644 --- a/src/plugins/ctf/fs-src/data-stream-file.c +++ b/src/plugins/ctf/fs-src/data-stream-file.c @@ -282,7 +282,7 @@ struct ctf_fs_ds_index *build_index_from_idx_file( const char *mmap_begin = NULL, *file_pos = NULL; const struct ctf_packet_index_file_hdr *header = NULL; struct ctf_fs_ds_index *index = NULL; - struct ctf_fs_ds_index_entry *index_entry = NULL; + struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL; uint64_t total_packets_size = 0; size_t file_index_entry_size; size_t file_entry_count; @@ -394,7 +394,7 @@ struct ctf_fs_ds_index *build_index_from_idx_file( index_entry->packet_size = packet_size; index_entry->offset = be64toh(file_index->offset); - if (i != 0 && index_entry->offset < (index_entry - 1)->offset) { + if (i != 0 && index_entry->offset < prev_index_entry->offset) { BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " "previous offset=%" PRIu64 ", current offset=%" PRIu64, (index_entry - 1)->offset, index_entry->offset); @@ -431,6 +431,7 @@ struct ctf_fs_ds_index *build_index_from_idx_file( file_pos += file_index_entry_size; g_ptr_array_add(index->entries, index_entry); + prev_index_entry = index_entry; } /* Validate that the index addresses the complete stream. */ -- 2.34.1