pezmaster31/bamtools — Issue #35: Problem writing BAM files in BigEndian architectures
Author: davidcarrera
Created: 2012-02-13 15:43:03+00:00
State: open

Hi,

I think I've found a problem writing bam files in a BigEndian architecture. The code in BamWriter_p.cpp (WriteSamHeaderText) looks as follows:

```
uint32_t samHeaderLen = samHeaderText.size();
if ( m_isBigEndian ) BamTools::SwapEndian_32(samHeaderLen);
m_stream.Write((char*)&samHeaderLen, Constants::BAM_SIZEOF_INT);

// write the SAM header text
if ( samHeaderLen > 0 )
m_stream.Write(samHeaderText.data(), samHeaderLen);
```

Notice that when "m_stream.Write" is called, "samHeaderLen" has already been swapped, and is not the right value anymore in a big endian machine. It leads to segmentation faults. It would be better to use "samHeaderText.size()" instead.

Something similar happens in WriteReferences with referenceSequenceNameLen. Maybe in other places also... I'm looking into it.

Thanks,
-David