Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary workaround when excessive memory is required by FORMAT fields #1689

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions vcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,7 @@ int bcf_write(htsFile *hfp, bcf_hdr_t *h, bcf1_t *v)
if ( hfp->format.format == vcf || hfp->format.format == text_format )
return vcf_write(hfp,h,v);

if ( v->errcode )
if ( v->errcode & ~BCF_ERR_LIMITS ) // todo: unsure about the other BCF_ERR_LIMITS branches in vcf_parse_format_alloc4()
{
// vcf_parse1() encountered a new contig or tag, undeclared in the
// header. At this point, the header must have been printed,
Expand Down Expand Up @@ -3004,9 +3004,13 @@ static int vcf_parse_format_alloc4(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v,
// malformed VCF data is less likely to take excessive memory and/or
// time.
if ((uint64_t) mem->l + v->n_sample * (uint64_t)f->size > INT_MAX) {
hts_log_error("Excessive memory required by FORMAT fields at %s:%"PRIhts_pos, bcf_seqname_safe(h,v), v->pos+1);
static int warned = 0;
if ( !warned ) hts_log_warning("Excessive memory required by FORMAT fields at %s:%"PRIhts_pos, bcf_seqname_safe(h,v), v->pos+1);
warned = 1;
v->errcode |= BCF_ERR_LIMITS;
return -1;
f->size = -1;
f->offset = 0;
continue;
}

f->offset = mem->l;
Expand Down Expand Up @@ -3065,7 +3069,12 @@ static int vcf_parse_format_fill5(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v,
return -1;
}

if (htype == BCF_HT_STR) {
if ( z->size==-1 )
{
// this field is to be ignored, it's too big
while ( *t != ':' && *t ) t++;
}
else if (htype == BCF_HT_STR) {
int l;
if (z->is_gt) {
// Genotypes.
Expand Down Expand Up @@ -3237,10 +3246,14 @@ static int vcf_parse_format_fill5(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v,
static int vcf_parse_format_gt6(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v,
const char *p, const char *q, fmt_aux_t *fmt) {
kstring_t *str = &v->indiv;
int i;
int i, need_downsize = 0;
if (v->n_sample > 0) {
for (i = 0; i < v->n_fmt; ++i) {
fmt_aux_t *z = &fmt[i];
if ( z->size==-1 ) {
need_downsize = 1;
continue;
}
bcf_enc_int1(str, z->key);
if ((z->y>>4&0xf) == BCF_HT_STR && !z->is_gt) {
bcf_enc_size(str, z->size, BCF_BT_CHAR);
Expand All @@ -3257,6 +3270,19 @@ static int vcf_parse_format_gt6(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v,
}
}
}

}
if ( need_downsize ) {
i = 1;
while ( i < v->n_fmt ) {
if ( fmt[i].size==-1 )
{
memmove(&fmt[i-1],&fmt[i],sizeof(*fmt));
v->n_fmt--;
}
else
i++;
}
}

return 0;
Expand Down