Fix reading of empty packets
[babeltrace.git] / formats / ctf / ctf.c
index d685b6cd70f4b9ef9362747bb7fa8862a430f086..7120207ddb5a47dafeb50089b13f1c542563ea7e 100644 (file)
@@ -415,9 +415,9 @@ 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 {
+               else {
                        pos->offset = EOF;
                        return;
                }
@@ -458,12 +458,13 @@ int packet_metadata(struct ctf_trace *td, FILE *fp)
        if (magic == TSDL_MAGIC) {
                ret = 1;
                td->byte_order = BYTE_ORDER;
+               CTF_TRACE_SET_FIELD(td, byte_order);
        } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
                ret = 1;
                td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
                                        LITTLE_ENDIAN : BIG_ENDIAN;
+               CTF_TRACE_SET_FIELD(td, byte_order);
        }
-       CTF_TRACE_SET_FIELD(td, byte_order);
 end:
        rewind(fp);
        return ret;
@@ -612,7 +613,17 @@ int ctf_open_trace_metadata_read(struct ctf_trace *td)
                if (ret)
                        goto end_packet_read;
        } else {
+               char buf[sizeof("/* TSDL")];    /* Includes \0 */
+               ssize_t readlen;
+
                td->byte_order = BYTE_ORDER;
+
+               /* Check text-only metadata header */
+               buf[sizeof("/* TSDL") - 1] = '\0';
+               readlen = fread(buf, sizeof("/* TSDL") - 1, 1, fp);
+               if (readlen < 1 || strcmp(buf, "/* TSDL") != 0)
+                       fprintf(stdout, "[warning] Missing \"/* TSDL\" header for text-only metadata.\n");
+               rewind(fp);
        }
 
        scanner = ctf_scanner_alloc(fp);
This page took 0.025495 seconds and 4 git commands to generate.