Browse Source

Bam reader robustness; allow threaded bam reading from Python

rdb 10 years ago
parent
commit
ea368dd559
3 changed files with 31 additions and 3 deletions
  1. 3 0
      panda/src/pgraph/pandaNode.cxx
  2. 25 0
      panda/src/putil/bamReader.cxx
  3. 3 3
      panda/src/putil/bamReader.h

+ 3 - 0
panda/src/pgraph/pandaNode.cxx

@@ -4465,6 +4465,9 @@ complete_pointers(TypedWritable **p_list, BamReader *manager) {
   set_fancy_bit(FB_effects, !_effects->is_empty());
   set_fancy_bit(FB_tag, !_tag_data.empty());
 
+  // Mark the bounds stale.
+  ++_next_update;
+
   return pi;
 }
 

+ 25 - 0
panda/src/putil/bamReader.cxx

@@ -142,6 +142,12 @@ init() {
     _file_stdfloat_double = scan.get_bool();
   }
 
+  if (scan.get_current_index() > header.get_length()) {
+    bam_cat.error()
+      << "Bam header is too short.\n";
+    return false;
+  }
+
   return true;
 }
 
@@ -1230,6 +1236,12 @@ p_read_object() {
 
   int object_id = read_object_id(scan);
 
+  if (scan.get_current_index() > dg.get_length()) {
+    bam_cat.error()
+      << "Found truncated datagram in bam stream\n";
+    return 0;
+  }
+
   // There are two cases (not counting the special _remove_flag case,
   // above).  Either this is a new object definition, or this is a
   // reference to an object that was previously defined.
@@ -1271,6 +1283,12 @@ p_read_object() {
       created_obj._ptr->fillin(scan, this);
       _now_creating = was_creating;
 
+      if (scan.get_remaining_size() > 0) {
+        bam_cat.warning()
+          << "Skipping " << scan.get_remaining_size() << " remaining bytes "
+          << "in datagram containing type " << type << "\n";
+      }
+
     } else {
       // We are receiving a new object.  Now we can call the factory
       // to create the object.
@@ -1371,6 +1389,13 @@ p_read_object() {
         }
       }
     }
+
+    // Sanity check that we read the expected number of bytes.
+    if (scan.get_current_index() > dg.get_length()) {
+      bam_cat.error()
+        << "End of datagram reached while reading bam object "
+        << type << ": " << (void *)created_obj._ptr << "\n";
+    }
   }
 
   return object_id;

+ 3 - 3
panda/src/putil/bamReader.h

@@ -143,9 +143,9 @@ PUBLISHED:
 
   INLINE const LoaderOptions &get_loader_options() const;
   INLINE void set_loader_options(const LoaderOptions &options);
-  
-  TypedWritable *read_object();
-  bool read_object(TypedWritable *&ptr, ReferenceCount *&ref_ptr);
+
+  BLOCKING TypedWritable *read_object();
+  BLOCKING bool read_object(TypedWritable *&ptr, ReferenceCount *&ref_ptr);
 
   INLINE bool is_eof() const;
   bool resolve();