Fix: ctf.c fscanf missing integer length check
[babeltrace.git] / formats / ctf / ctf.c
index 7d98eb7a8f9ae61cd055ccea6ed37b95f20600f6..5ddf2f78ddc6e73e85e1936f50dad57672635607 100644 (file)
@@ -781,6 +781,42 @@ int ctf_fini_pos(struct ctf_stream_pos *pos)
        return 0;
 }
 
+void ctf_update_current_packet_index(struct ctf_stream_definition *stream,
+               struct packet_index *prev_index,
+               struct packet_index *cur_index)
+{
+       uint64_t events_discarded_diff;
+
+       /* Update packet index time information */
+       stream->prev_cycles_timestamp_end =
+               cur_index->ts_cycles.timestamp_end;
+       stream->prev_cycles_timestamp =
+               cur_index->ts_cycles.timestamp_begin;
+       stream->prev_real_timestamp_end =
+               cur_index->ts_real.timestamp_end;
+       stream->prev_real_timestamp =
+               cur_index->ts_real.timestamp_begin;
+
+       stream->prev_real_timestamp =
+               stream->real_timestamp;
+       stream->prev_cycles_timestamp =
+               stream->cycles_timestamp;
+
+       /* Update packet index discarded event information */
+       events_discarded_diff = cur_index->events_discarded;
+       if (prev_index) {
+               events_discarded_diff -= prev_index->events_discarded;
+               /*
+                * Deal with 32-bit wrap-around if the tracer provided a
+                * 32-bit field.
+                */
+               if (prev_index->events_discarded_len == 32) {
+                       events_discarded_diff = (uint32_t) events_discarded_diff;
+               }
+       }
+       stream->events_discarded = events_discarded_diff;
+}
+
 /*
  * for SEEK_CUR: go to next packet.
  * for SEEK_SET: go to packet numer (index).
@@ -845,42 +881,22 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                switch (whence) {
                case SEEK_CUR:
                {
-                       uint64_t events_discarded_diff;
+                       struct packet_index *prev_index = NULL;
 
                        if (pos->offset == EOF) {
                                return;
                        }
                        assert(pos->cur_index < pos->packet_index->len);
-
-                       /* For printing discarded event count */
                        packet_index = &g_array_index(pos->packet_index,
                                        struct packet_index, pos->cur_index);
-                       file_stream->parent.prev_cycles_timestamp_end =
-                                       packet_index->ts_cycles.timestamp_end;
-                       file_stream->parent.prev_cycles_timestamp =
-                                       packet_index->ts_cycles.timestamp_begin;
-                       file_stream->parent.prev_real_timestamp_end =
-                                       packet_index->ts_real.timestamp_end;
-                       file_stream->parent.prev_real_timestamp =
-                                       packet_index->ts_real.timestamp_begin;
-
-                       events_discarded_diff = packet_index->events_discarded;
                        if (pos->cur_index > 0) {
-                               packet_index = &g_array_index(pos->packet_index,
+                               prev_index = &g_array_index(pos->packet_index,
                                                struct packet_index,
                                                pos->cur_index - 1);
-                               events_discarded_diff -= packet_index->events_discarded;
-                               /*
-                                * Deal with 32-bit wrap-around if the
-                                * tracer provided a 32-bit field.
-                                */
-                               if (packet_index->events_discarded_len == 32) {
-                                       events_discarded_diff = (uint32_t) events_discarded_diff;
-                               }
                        }
-                       file_stream->parent.events_discarded = events_discarded_diff;
-                       file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp;
-                       file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp;
+                       ctf_update_current_packet_index(&file_stream->parent,
+                                       prev_index, packet_index);
+
                        /* The reader will expect us to skip padding */
                        ++pos->cur_index;
                        break;
@@ -1185,9 +1201,9 @@ int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
 static
 int ctf_open_trace_metadata_read(struct ctf_trace *td,
                void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
-                       int whence), FILE *metadata_fp)
+                       int whence), FILE *metadata_fp,
+                       struct ctf_scanner *scanner)
 {
-       struct ctf_scanner *scanner;
        struct ctf_file_stream *metadata_stream;
        FILE *fp;
        char *buf = NULL;
@@ -1234,7 +1250,7 @@ int ctf_open_trace_metadata_read(struct ctf_trace *td,
                if (ret) {
                        /* Warn about empty metadata */
                        fprintf(stderr, "[warning] Empty metadata.\n");
-                       goto end_packet_read;
+                       goto end;
                }
                td->metadata_string = buf;
                td->metadata_packetized = 1;
@@ -1245,23 +1261,17 @@ int ctf_open_trace_metadata_read(struct ctf_trace *td,
                td->byte_order = BYTE_ORDER;
 
                /* Check text-only metadata header and version */
-               nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
+               nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
                if (nr_items < 2)
                        fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
                if (check_version(major, minor) < 0) {
                        ret = -EINVAL;
-                       goto end_packet_read;
+                       goto end;
                }
                rewind(fp);
        }
 
-       scanner = ctf_scanner_alloc(fp);
-       if (!scanner) {
-               fprintf(stderr, "[error] Error allocating scanner\n");
-               ret = -ENOMEM;
-               goto end_scanner_alloc;
-       }
-       ret = ctf_scanner_append_ast(scanner);
+       ret = ctf_scanner_append_ast(scanner, fp);
        if (ret) {
                fprintf(stderr, "[error] Error creating AST\n");
                goto end;
@@ -1287,9 +1297,6 @@ int ctf_open_trace_metadata_read(struct ctf_trace *td,
                goto end;
        }
 end:
-       ctf_scanner_free(scanner);
-end_scanner_alloc:
-end_packet_read:
        if (fp) {
                closeret = fclose(fp);
                if (closeret) {
@@ -1795,7 +1802,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;
@@ -1836,6 +1842,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);
@@ -1855,7 +1862,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",
@@ -2027,6 +2036,7 @@ int ctf_open_trace_read(struct ctf_trace *td,
                void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
                        int whence), FILE *metadata_fp)
 {
+       struct ctf_scanner *scanner;
        int ret, closeret;
        struct dirent *dirent;
        struct dirent *diriter;
@@ -2056,8 +2066,15 @@ int ctf_open_trace_read(struct ctf_trace *td,
        /*
         * Keep the metadata file separate.
         */
-
-       ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
+       scanner = ctf_scanner_alloc();
+       if (!scanner) {
+               fprintf(stderr, "[error] Error allocating scanner\n");
+               ret = -ENOMEM;
+               goto error_metadata;
+       }
+       ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp,
+                       scanner);
+       ctf_scanner_free(scanner);
        if (ret) {
                fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
                goto error_metadata;
@@ -2179,12 +2196,13 @@ void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
        pos->offset = 0;
        pos->dummy = false;
        pos->cur_index = 0;
-       pos->packet_index = NULL;
        pos->prot = PROT_READ;
        pos->flags = MAP_PRIVATE;
        pos->parent.rw_table = read_dispatch_table;
        pos->parent.event_cb = ctf_read_event;
        pos->priv = mmap_info->priv;
+       pos->packet_index = g_array_new(FALSE, TRUE,
+                       sizeof(struct packet_index));
 }
 
 static
@@ -2266,8 +2284,16 @@ int ctf_open_mmap_trace_read(struct ctf_trace *td,
 {
        int ret;
        struct bt_mmap_stream *mmap_info;
+       struct ctf_scanner *scanner;
 
-       ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
+       scanner = ctf_scanner_alloc();
+       if (!scanner) {
+               fprintf(stderr, "[error] Error allocating scanner\n");
+               ret = -ENOMEM;
+               goto error_scanner_alloc;
+       }
+       ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp,
+                       scanner);
        if (ret) {
                goto error;
        }
@@ -2283,10 +2309,12 @@ int ctf_open_mmap_trace_read(struct ctf_trace *td,
                        goto error;
                }
        }
-
+       ctf_scanner_free(scanner);
        return 0;
 
 error:
+       ctf_scanner_free(scanner);
+error_scanner_alloc:
        return ret;
 }
 
@@ -2310,6 +2338,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.026294 seconds and 4 git commands to generate.