From 73433cf4992127c558e72cf75147a9e599330d3f Mon Sep 17 00:00:00 2001
From: Derek <derekwbarnett@gmail.com>
Date: Mon, 7 Jun 2010 16:18:10 -0400
Subject: [PATCH] This fixes the out-of-range exception. Though there's still a
 discrepancy with some (but, maddeningly, not all) tags following a string
 tag. Will look into to more detail, but at least it shouldn't crash in the
 meantime

---
 bamtools_convert.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/bamtools_convert.cpp b/bamtools_convert.cpp
index de86599..e881d43 100644
--- a/bamtools_convert.cpp
+++ b/bamtools_convert.cpp
@@ -288,6 +288,8 @@ void BamTools::PrintJSON(ostream& out, const BamAlignment& a) {
                     ++index; 
                     break;      
             }
+            
+            if ( tagData[index] == '\0') break;
         }
 
         out << "}";
@@ -348,9 +350,7 @@ void BamTools::PrintSAM(ostream& out, const BamAlignment& a) {
     size_t index = 0;
     while ( index < tagDataLength ) {
 
-        cerr << tagDataLength << " " << index << endl;
-        
-        // write tag name
+        // write tag name        
         out << "\t" << a.TagData.substr(index, 2) << ":";
         index += 2;
         
@@ -414,8 +414,9 @@ void BamTools::PrintSAM(ostream& out, const BamAlignment& a) {
                 ++index; 
                 break;      
         }
+        
+        if ( tagData[index] == '\0') break;
     }
 
     out << endl;
-    
 }
-- 
2.39.5