Index: src/Parser/PdbParser.cpp
===================================================================
--- src/Parser/PdbParser.cpp	(revision 029bb4e5c3a78c79d56801bff9ea647fbc143423)
+++ src/Parser/PdbParser.cpp	(revision 5fa2bac2fc0563ed676ba8178b07597a44189e58)
@@ -475,32 +475,71 @@
 }
 
+/** read a line starting with key ATOM.
+ *
+ * We check for line's length and parse only up to this value.
+ *
+ * @param atomInfo container to put information in
+ * @param line line containing key ATOM
+ */
 void PdbParser::readPdbAtomInfoContainer(PdbAtomInfoContainer &atomInfo, std::string &line) const
 {
-  LOG(4,"INFO: Parsing token from "+line.substr(0,6)+".");
-  atomInfo.set(PdbKey::token, line.substr(0,6));
-  LOG(4,"INFO: Parsing serial from "+line.substr(6,5)+".");
-  atomInfo.set(PdbKey::serial, line.substr(6,5));
-
-  LOG(4,"INFO: Parsing name from "+line.substr(12,4)+".");
-  atomInfo.set(PdbKey::name, line.substr(12,4));
-  LOG(4,"INFO: Parsing altLoc from "+line.substr(16,1)+".");
-  atomInfo.set(PdbKey::altLoc, line.substr(16,1));
-  LOG(4,"INFO: Parsing resName from "+line.substr(17,3)+".");
-  atomInfo.set(PdbKey::resName, line.substr(17,3));
-  LOG(4,"INFO: Parsing chainID from "+line.substr(21,1)+".");
-  atomInfo.set(PdbKey::chainID, line.substr(21,1));
-  LOG(4,"INFO: Parsing resSeq from "+line.substr(22,4)+".");
-  atomInfo.set(PdbKey::resSeq, line.substr(22,4));
-  LOG(4,"INFO: Parsing iCode from "+line.substr(26,1)+".");
-  atomInfo.set(PdbKey::iCode, line.substr(26,1));
-
-  LOG(4,"INFO: Parsing occupancy from "+line.substr(54,6)+".");
-  atomInfo.set(PdbKey::occupancy, line.substr(54,6));
-  LOG(4,"INFO: Parsing tempFactor from "+line.substr(60,6)+".");
-  atomInfo.set(PdbKey::tempFactor, line.substr(60,6));
-  LOG(4,"INFO: Parsing charge from "+line.substr(78,2)+".");
-  atomInfo.set(PdbKey::charge, line.substr(78,2));
-  LOG(4,"INFO: Parsing element from "+line.substr(76,2)+".");
-  atomInfo.set(PdbKey::element, line.substr(76,2));
+  const size_t length = line.length();
+  if (length < 80)
+    ELOG(2, "PdbParser::readPdbAtomInfoContainer() - pdb is mal-formed, containing less than 80 chars!");
+  if (length >= 6) {
+    LOG(4,"INFO: Parsing token from "+line.substr(0,6)+".");
+    atomInfo.set(PdbKey::token, line.substr(0,6));
+  }
+  if (length >= 11) {
+    LOG(4,"INFO: Parsing serial from "+line.substr(6,5)+".");
+    atomInfo.set(PdbKey::serial, line.substr(6,5));
+    ASSERT(atomInfo.get<int>(PdbKey::serial) != 0,
+        "PdbParser::readPdbAtomInfoContainer() - serial 0 is invalid (filler id for conect entries).");
+  }
+
+  if (length >= 16) {
+    LOG(4,"INFO: Parsing name from "+line.substr(12,4)+".");
+    atomInfo.set(PdbKey::name, line.substr(12,4));
+  }
+  if (length >= 17) {
+    LOG(4,"INFO: Parsing altLoc from "+line.substr(16,1)+".");
+    atomInfo.set(PdbKey::altLoc, line.substr(16,1));
+  }
+  if (length >= 20) {
+    LOG(4,"INFO: Parsing resName from "+line.substr(17,3)+".");
+    atomInfo.set(PdbKey::resName, line.substr(17,3));
+  }
+  if (length >= 22) {
+    LOG(4,"INFO: Parsing chainID from "+line.substr(21,1)+".");
+    atomInfo.set(PdbKey::chainID, line.substr(21,1));
+  }
+  if (length >= 26) {
+    LOG(4,"INFO: Parsing resSeq from "+line.substr(22,4)+".");
+    atomInfo.set(PdbKey::resSeq, line.substr(22,4));
+  }
+  if (length >= 27) {
+    LOG(4,"INFO: Parsing iCode from "+line.substr(26,1)+".");
+    atomInfo.set(PdbKey::iCode, line.substr(26,1));
+  }
+
+  if (length >= 60) {
+    LOG(4,"INFO: Parsing occupancy from "+line.substr(54,6)+".");
+    atomInfo.set(PdbKey::occupancy, line.substr(54,6));
+  }
+  if (length >= 66) {
+    LOG(4,"INFO: Parsing tempFactor from "+line.substr(60,6)+".");
+    atomInfo.set(PdbKey::tempFactor, line.substr(60,6));
+  }
+  if (length >= 80) {
+    LOG(4,"INFO: Parsing charge from "+line.substr(78,2)+".");
+    atomInfo.set(PdbKey::charge, line.substr(78,2));
+  }
+  if (length >= 78) {
+    LOG(4,"INFO: Parsing element from "+line.substr(76,2)+".");
+    atomInfo.set(PdbKey::element, line.substr(76,2));
+  } else {
+    LOG(4,"INFO: Trying to parse alternative element from name "+line.substr(12,4)+".");
+    atomInfo.set(PdbKey::element, line.substr(12,4));
+  }
 }
 
@@ -708,6 +747,8 @@
       int otherid = -1;
       PdbAtomInfoContainer::ScanKey(otherid, line.substr(index,5));
-      if (otherid >= 0)
-        ListOfNeighbors.push_back((size_t)otherid);
+      if (otherid > 0)
+        ListOfNeighbors.push_back(otherid);
+      else
+        ELOG(2, "PdbParser::readNeighbors() - discarding conect entry with id 0.");
     } else  {
       break;
