Skip to content

Commit 995069d

Browse files
committed
Use hts_pos_t in tweak_overlap_quality() and related functions
While not strictly necessary (the positions in question are relative to that of read 'b') it makes data types consistent and reduces the possibility of accidental overflow. Also adds a check that the position in the sequence is valid before trying to use it for array look-ups.
1 parent 4f1a3fc commit 995069d

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

sam.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4015,9 +4015,9 @@ void bam_plp_destructor(bam_plp_t plp,
40154015
* Returns BAM_CMATCH, -1 when there is no more cigar to process or the requested position is not covered,
40164016
* or -2 on error.
40174017
*/
4018-
static inline int cigar_iref2iseq_set(uint32_t **cigar, uint32_t *cigar_max, int *icig, int *iseq, hts_pos_t *iref)
4018+
static inline int cigar_iref2iseq_set(uint32_t **cigar, uint32_t *cigar_max, hts_pos_t *icig, hts_pos_t *iseq, hts_pos_t *iref)
40194019
{
4020-
int pos = *iref;
4020+
hts_pos_t pos = *iref;
40214021
if ( pos < 0 ) return -1;
40224022
*icig = 0;
40234023
*iseq = 0;
@@ -4050,7 +4050,7 @@ static inline int cigar_iref2iseq_set(uint32_t **cigar, uint32_t *cigar_max, int
40504050
*iseq = -1;
40514051
return -1;
40524052
}
4053-
static inline int cigar_iref2iseq_next(uint32_t **cigar, uint32_t *cigar_max, int *icig, int *iseq, hts_pos_t *iref)
4053+
static inline int cigar_iref2iseq_next(uint32_t **cigar, uint32_t *cigar_max, hts_pos_t *icig, hts_pos_t *iseq, hts_pos_t *iref)
40544054
{
40554055
while ( *cigar < cigar_max )
40564056
{
@@ -4079,8 +4079,8 @@ static int tweak_overlap_quality(bam1_t *a, bam1_t *b)
40794079
{
40804080
uint32_t *a_cigar = bam_get_cigar(a), *a_cigar_max = a_cigar + a->core.n_cigar;
40814081
uint32_t *b_cigar = bam_get_cigar(b), *b_cigar_max = b_cigar + b->core.n_cigar;
4082-
int a_icig = 0, a_iseq = 0;
4083-
int b_icig = 0, b_iseq = 0;
4082+
hts_pos_t a_icig = 0, a_iseq = 0;
4083+
hts_pos_t b_icig = 0, b_iseq = 0;
40844084
uint8_t *a_qual = bam_get_qual(a), *b_qual = bam_get_qual(b);
40854085
uint8_t *a_seq = bam_get_seq(a), *b_seq = bam_get_seq(b);
40864086

@@ -4114,6 +4114,9 @@ static int tweak_overlap_quality(bam1_t *a, bam1_t *b)
41144114
iref++;
41154115
if ( a_iref+a->core.pos != b_iref+b->core.pos ) continue; // only CMATCH positions, don't know what to do with indels
41164116

4117+
if (a_iseq > a->core.l_qseq || b_iseq > b->core.l_qseq)
4118+
return -1; // Fell off end of sequence, bad CIGAR?
4119+
41174120
if ( bam_seqi(a_seq,a_iseq) == bam_seqi(b_seq,b_iseq) )
41184121
{
41194122
#if DBG

0 commit comments

Comments
 (0)