Fix non-matching packet context wrt last packet event read
[babeltrace.git] / formats / ctf / ctf.c
index fee9a5578d809de68f233c2b855980a6f1fb0173..f078735c3ea9fd5307c46333b28b2c473867a7e1 100644 (file)
@@ -125,8 +125,11 @@ int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
        uint64_t id = 0;
        int ret;
 
+       ctf_pos_get_event(pos);
+
        if (pos->offset == EOF)
                return EOF;
+       assert(pos->offset < pos->content_size);
 
        /* Read event header */
        if (stream->stream_event_header) {
@@ -390,8 +393,11 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
                assert(off >= 0);
                pos->offset = 0;
        } else {
+       read_next_packet:
                switch (whence) {
                case SEEK_CUR:
+                       if (pos->offset == EOF)
+                               return;
                        /* The reader will expect us to skip padding */
                        assert(pos->offset + offset == pos->content_size);
                        ++pos->cur_index;
@@ -415,8 +421,13 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
                file_stream->parent.timestamp = index->timestamp_begin;
                pos->content_size = index->content_size;
                pos->packet_size = index->packet_size;
-               if (index->data_offset <= index->content_size) {
+               if (index->data_offset < index->content_size) {
                        pos->offset = 0;        /* will read headers */
+               } else if (index->data_offset == index->content_size) {
+                       /* empty packet */
+                       pos->offset = index->data_offset;
+                       offset = 0;
+                       goto read_next_packet;
                } else {
                        pos->offset = EOF;
                        return;
@@ -568,10 +579,19 @@ int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
                toread -= readlen;
                if (!toread) {
                        ret = 0;        /* continue reading next packet */
-                       break;
+                       goto read_padding;
                }
        }
        return ret;
+
+read_padding:
+       toread = (header.packet_size - header.content_size) / CHAR_BIT;
+       ret = fseek(in, toread, SEEK_CUR);
+       if (ret < 0) {
+               fprintf(stdout, "[warning] Missing padding at end of file\n");
+               ret = 0;
+       }
+       return ret;
 }
 
 static
This page took 0.025274 seconds and 4 git commands to generate.