Fix: ctf.c fscanf missing integer length check
[babeltrace.git] / formats / ctf / ctf.c
index 9f807c9e2d9910dcfe0a089c2ffaf200da766f12..5ddf2f78ddc6e73e85e1936f50dad57672635607 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);
@@ -1198,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;
@@ -1247,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;
@@ -1258,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;
@@ -1300,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) {
@@ -1808,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;
@@ -1849,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);
@@ -1868,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",
@@ -2040,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;
@@ -2069,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;
@@ -2280,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;
        }
@@ -2297,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;
 }
 
@@ -2324,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.026199 seconds and 4 git commands to generate.