Fix: Unchecked array index when importing trace indexes
[babeltrace.git] / formats / ctf / ctf.c
index 9f807c9e2d9910dcfe0a089c2ffaf200da766f12..6d21b876dad422628f0884ea7e9e21be7c2db063 100644 (file)
@@ -887,9 +887,12 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                                return;
                        }
                        assert(pos->cur_index < pos->packet_index->len);
-                       if (index > 0) {
+                       packet_index = &g_array_index(pos->packet_index,
+                                       struct packet_index, pos->cur_index);
+                       if (pos->cur_index > 0) {
                                prev_index = &g_array_index(pos->packet_index,
-                                               struct packet_index, index - 1);
+                                               struct packet_index,
+                                               pos->cur_index - 1);
                        }
                        ctf_update_current_packet_index(&file_stream->parent,
                                        prev_index, packet_index);
@@ -1808,7 +1811,6 @@ static
 int import_stream_packet_index(struct ctf_trace *td,
                struct ctf_file_stream *file_stream)
 {
-       struct ctf_stream_declaration *stream;
        struct ctf_stream_pos *pos;
        struct ctf_packet_index ctf_index;
        struct ctf_packet_index_file_hdr index_hdr;
@@ -1849,6 +1851,7 @@ int import_stream_packet_index(struct ctf_trace *td,
        while (fread(&ctf_index, index_hdr.packet_index_len, 1,
                        pos->index_fp) == 1) {
                uint64_t stream_id;
+               struct ctf_stream_declaration *stream = NULL;
 
                memset(&index, 0, sizeof(index));
                index.offset = be64toh(ctf_index.offset);
@@ -1868,7 +1871,9 @@ int import_stream_packet_index(struct ctf_trace *td,
                }
 
                file_stream->parent.stream_id = stream_id;
-               stream = g_ptr_array_index(td->streams, stream_id);
+               if (stream_id < td->streams->len) {
+                       stream = g_ptr_array_index(td->streams, stream_id);
+               }
                if (!stream) {
                        fprintf(stderr, "[error] Stream %" PRIu64
                                        " is not declared in metadata.\n",
@@ -2324,6 +2329,7 @@ struct bt_trace_descriptor *ctf_open_mmap_trace(
                goto error;
        }
        td = g_new0(struct ctf_trace, 1);
+       td->dirfd = -1;
        ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
        if (ret)
                goto error_free;
This page took 0.024057 seconds and 4 git commands to generate.