Skip to content

Commit

Permalink
Merge vcf_parse_format() end-of-vector bug fix (PR #370)
Browse files Browse the repository at this point in the history
Clarify loop invariant: fmt[j] is the first unfilled-in fmt_aux_t.
  • Loading branch information
jmarshall committed Apr 21, 2016
2 parents 50c4f39 + 0e5af18 commit eeec649
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions vcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,7 @@ static int vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p
j = 0; // j-th format field, m-th sample
while ( t < end )
{
fmt_aux_t *z = &fmt[j];
fmt_aux_t *z = &fmt[j++];
if ((z->y>>4&0xf) == BCF_HT_STR) {
if (z->is_gt) { // genotypes
int32_t is_phased = 0, *x = (int32_t*)(z->buf + z->size * m);
Expand Down Expand Up @@ -1765,33 +1765,11 @@ static int vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p
if ( !l ) bcf_float_set_missing(x[l++]); // An empty field, insert missing value
for (; l < z->size>>2; ++l) bcf_float_set_vector_end(x[l]);
} else abort();

if (*t == '\0') {
for (++j; j < v->n_fmt; ++j) { // fill end-of-vector values
z = &fmt[j];
if ((z->y>>4&0xf) == BCF_HT_STR) {
if (z->is_gt) {
int32_t *x = (int32_t*)(z->buf + z->size * m);
if (z->size) x[0] = bcf_int32_missing;
for (l = 1; l < z->size>>2; ++l) x[l] = bcf_int32_vector_end;
} else {
char *x = (char*)z->buf + z->size * m;
if ( z->size ) x[0] = '.';
for (l = 1; l < z->size; ++l) x[l] = 0;
}
} else if ((z->y>>4&0xf) == BCF_HT_INT) {
int32_t *x = (int32_t*)(z->buf + z->size * m);
x[0] = bcf_int32_missing;
for (l = 1; l < z->size>>2; ++l) x[l] = bcf_int32_vector_end;
} else if ((z->y>>4&0xf) == BCF_HT_REAL) {
float *x = (float*)(z->buf + z->size * m);
bcf_float_set_missing(x[0]);
for (l = 1; l < z->size>>2; ++l) bcf_float_set_vector_end(x[l]);
}
}
break;
}
else if (*t == ':') {
j++;
t++;
}
else {
Expand All @@ -1801,6 +1779,30 @@ static int vcf_parse_format(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v, char *p
return -1;
}
}

for (; j < v->n_fmt; ++j) { // fill end-of-vector values
fmt_aux_t *z = &fmt[j];
if ((z->y>>4&0xf) == BCF_HT_STR) {
if (z->is_gt) {
int32_t *x = (int32_t*)(z->buf + z->size * m);
if (z->size) x[0] = bcf_int32_missing;
for (l = 1; l < z->size>>2; ++l) x[l] = bcf_int32_vector_end;
} else {
char *x = (char*)z->buf + z->size * m;
if ( z->size ) x[0] = '.';
for (l = 1; l < z->size; ++l) x[l] = 0;
}
} else if ((z->y>>4&0xf) == BCF_HT_INT) {
int32_t *x = (int32_t*)(z->buf + z->size * m);
x[0] = bcf_int32_missing;
for (l = 1; l < z->size>>2; ++l) x[l] = bcf_int32_vector_end;
} else if ((z->y>>4&0xf) == BCF_HT_REAL) {
float *x = (float*)(z->buf + z->size * m);
bcf_float_set_missing(x[0]);
for (l = 1; l < z->size>>2; ++l) bcf_float_set_vector_end(x[l]);
}
}

m++; t++;
}

Expand Down

0 comments on commit eeec649

Please sign in to comment.