Repository: ros/ros_comm

Issue:
Empty bag file corrupt after `rosbag --reindex`
After running `rosbag reindex` on the attached (empty/no messages) `unindexed.bag` file, the resulting file causes a traceback when fed to `rosbag info`.  `rosbag` version is `1.11.20`

To reproduce:
```
$ unzip rosbag_reindex_issue.zip
$ rosbag info unindexed.bag
ERROR bag unindexed: unindexed.bag.  Run rosbag reindex.
$ rosbag reindex unindexed_bag
$ rosbag info unindexed.bag
Traceback (most recent call last):
  File "/path/redacted/rosbag/scripts/rosbag_bin.py", line 35, in <module>
    rosbag.rosbagmain()
  File "/path/redacted/rosbag/src/rosbag/rosbag_main.py", line 863, in rosbagmain
    cmds[cmd](argv[2:])
  File "/path/redacted/rosbag/src/rosbag/rosbag_main.py", line 149, in info_cmd
    print(b)
  File "/path/redacted/rosbag/src/rosbag/bag.py", line 628, in __str__
    start_stamp = min([index[ 0].time.to_sec() for index in self._connection_indexes.values()])
IndexError: list index out of range
```
[rosbag_reindex_issue.zip](https://github.com/ros/ros_comm/files/1142949/rosbag_reindex_issue.zip)




Discussion:

Resolving patch:
diff --git a/tools/rosbag/src/rosbag/bag.py b/tools/rosbag/src/rosbag/bag.py
--- a/tools/rosbag/src/rosbag/bag.py
+++ b/tools/rosbag/src/rosbag/bag.py
@@ -496,7 +496,8 @@ def get_start_time(self):
         else:
             if not self._connection_indexes:
                 raise ROSBagException('Bag contains no message')
-            start_stamp = min([index[0].time.to_sec() for index in self._connection_indexes.values()])
+            start_stamps = [index[0].time.to_sec() for index in self._connection_indexes.values() if index]
+            start_stamp = min(start_stamps) if start_stamps else 0
         
         return start_stamp
     
@@ -512,7 +513,8 @@ def get_end_time(self):
         else:
             if not self._connection_indexes:
                 raise ROSBagException('Bag contains no message')
-            end_stamp = max([index[-1].time.to_sec() for index in self._connection_indexes.values()])
+            end_stamps = [index[-1].time.to_sec() for index in self._connection_indexes.values() if index]
+            end_stamp = max(end_stamps) if end_stamps else 0
         
         return end_stamp
     
@@ -625,8 +627,10 @@ def __str__(self):
                     start_stamp = self._chunks[ 0].start_time.to_sec()
                     end_stamp   = self._chunks[-1].end_time.to_sec()
                 else:
-                    start_stamp = min([index[ 0].time.to_sec() for index in self._connection_indexes.values()])
-                    end_stamp   = max([index[-1].time.to_sec() for index in self._connection_indexes.values()])
+                    start_stamps = [index[0].time.to_sec() for index in self._connection_indexes.values() if index]
+                    start_stamp = min(start_stamps) if start_stamps else 0
+                    end_stamps = [index[-1].time.to_sec() for index in self._connection_indexes.values() if index]
+                    end_stamp = max(end_stamps) if end_stamps else 0
     
                 # Show duration
                 duration = end_stamp - start_stamp
@@ -808,8 +812,10 @@ def _get_yaml_info(self, key=None):
                     start_stamp = self._chunks[ 0].start_time.to_sec()
                     end_stamp   = self._chunks[-1].end_time.to_sec()
                 else:
-                    start_stamp = min([index[ 0].time.to_sec() for index in self._connection_indexes.values()])
-                    end_stamp   = max([index[-1].time.to_sec() for index in self._connection_indexes.values()])
+                    start_stamps = [index[0].time.to_sec() for index in self._connection_indexes.values() if index]
+                    start_stamp = min(start_stamps) if start_stamps else 0
+                    end_stamps = [index[-1].time.to_sec() for index in self._connection_indexes.values() if index]
+                    end_stamp = max(end_stamps) if end_stamps else 0
                 
                 duration = end_stamp - start_stamp
                 s += 'duration: %.6f\n' % duration


Test patch: