Propagate error from packet_seek in case of truncated packet
[babeltrace.git] / formats / ctf / ctf.c
index b74cddd7da07114702bc75ccd81218df1a6324d9..980ebc9a9602d47323524194e60752089851134e 100644 (file)
@@ -477,6 +477,29 @@ void ctf_print_discarded_lost(FILE *fp, struct ctf_stream_definition *stream)
        fflush(fp);
 }
 
+static
+void ctf_print_truncated_packet(FILE *fp, struct ctf_stream_definition *stream,
+               uint64_t packet_size, uint64_t remaining_file_size)
+{
+       fprintf(fp, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits) in trace with UUID \"",
+                       packet_size, remaining_file_size);
+       print_uuid(fp, stream->stream_class->trace->uuid);
+       fprintf(fp, "\"");
+
+       if (stream->stream_class->trace->parent.path[0] != '\0') {
+               fprintf(fp, ", at path: \"%s\"",
+                               stream->stream_class->trace->parent.path);
+       }
+
+       fprintf(fp, ", within stream id %" PRIu64, stream->stream_id);
+       if (stream->path[0] != '\0') {
+               fprintf(fp, ", at relative path: \"%s\"", stream->path);
+       }
+
+       fprintf(fp, ".\n");
+       fflush(fp);
+}
+
 static
 int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
 {
@@ -491,8 +514,12 @@ int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *str
        if (unlikely(pos->offset == EOF))
                return EOF;
 
-       if (ctf_pos_get_event(pos))
+       ret = ctf_pos_get_event(pos);
+       if (ret == -BT_PACKET_SEEK_ERROR_TRUNCATED_PACKET) {
+               return -ERANGE;
+       } else if (ret) {
                return EOF;
+       }
 
        /* save the current position as a restore point */
        pos->last_offset = pos->offset;
@@ -1059,7 +1086,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
        case SEEK_SET:  /* Fall-through */
                break;  /* OK */
        default:
-               ret = -1;
+               ret = -BT_PACKET_SEEK_ERROR;
                goto end;
        }
 
@@ -1072,7 +1099,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                if (ret) {
                        fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                strerror(errno));
-                       ret = -1;
+                       ret = -BT_PACKET_SEEK_ERROR;
                        goto end;
                }
                pos->base_mma = NULL;
@@ -1093,7 +1120,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                        pos->cur_index = 0;
                        break;
                default:
-                       ret = -1;
+                       ret = -BT_PACKET_SEEK_ERROR;
                        goto end;
                }
                pos->content_size = -1U;        /* Unknown at this point */
@@ -1105,6 +1132,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                assert(ret == 0);
                pos->offset = 0;
        } else {
+               uint64_t remaining_file_size;
        read_next_packet:
                switch (whence) {
                case SEEK_CUR:
@@ -1127,7 +1155,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                        pos->cur_index = index;
                        break;
                default:
-                       ret = -1;
+                       ret = -BT_PACKET_SEEK_ERROR;
                        goto end;
                }
 
@@ -1176,15 +1204,19 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                if (packet_index->data_offset == -1) {
                        ret = find_data_offset(pos, file_stream, packet_index);
                        if (ret < 0) {
-                               ret = -1;
+                               ret = -BT_PACKET_SEEK_ERROR;
                                goto end;
                        }
                }
 
-               if (packet_index->packet_size > ((uint64_t) pos->file_length - packet_index->offset) * CHAR_BIT) {
-                       fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
-                               packet_index->packet_size, ((uint64_t) pos->file_length - packet_index->offset) * CHAR_BIT);
-                       ret = -1;
+               remaining_file_size = (pos->file_length - ((uint64_t) packet_index->offset)) * CHAR_BIT;
+               if (packet_index->packet_size > remaining_file_size) {
+                       fflush(stdout);
+                       ctf_print_truncated_packet(stderr, &file_stream->parent,
+                                       packet_index->packet_size,
+                                       remaining_file_size);
+                       pos->offset = EOF;
+                       ret = -BT_PACKET_SEEK_ERROR_TRUNCATED_PACKET;
                        goto end;
                }
 
This page took 0.024486 seconds and 4 git commands to generate.