Skip to content

Commit cef68bc

Browse files
committed
Check the return status of bcf_hdr_append()
to make sure the program does not produce invalid contig ids and tags. Note that the check was added only in places relying on user input, not the ones trivially true. Resolves #2345
1 parent 37eea22 commit cef68bc

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

mpileup.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mpileup.c -- mpileup subcommand. Previously bam_plcmd.c from samtools
22
3-
Copyright (C) 2008-2024 Genome Research Ltd.
3+
Copyright (C) 2008-2025 Genome Research Ltd.
44
Portions copyright (C) 2009-2012 Broad Institute.
55
66
Author: Heng Li <[email protected]>
@@ -766,20 +766,20 @@ static int mpileup(mplp_conf_t *conf)
766766
if (conf->record_cmd_line)
767767
{
768768
ksprintf(&conf->buf, "##bcftoolsVersion=%s+htslib-%s\n",bcftools_version(),hts_version());
769-
bcf_hdr_append(conf->bcf_hdr, conf->buf.s);
769+
if ( bcf_hdr_append(conf->bcf_hdr, conf->buf.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
770770

771771
conf->buf.l = 0;
772772
ksprintf(&conf->buf, "##bcftoolsCommand=mpileup");
773773
for (i=1; i<conf->argc; i++) ksprintf(&conf->buf, " %s", conf->argv[i]);
774774
kputc('\n', &conf->buf);
775-
bcf_hdr_append(conf->bcf_hdr, conf->buf.s);
775+
if ( bcf_hdr_append(conf->bcf_hdr, conf->buf.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
776776
}
777777

778778
if (conf->fai_fname)
779779
{
780780
conf->buf.l = 0;
781781
ksprintf(&conf->buf, "##reference=file://%s\n", conf->fai_fname);
782-
bcf_hdr_append(conf->bcf_hdr, conf->buf.s);
782+
if ( bcf_hdr_append(conf->bcf_hdr, conf->buf.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
783783
}
784784

785785
// Translate BAM @SQ tags to BCF ##contig tags
@@ -788,7 +788,7 @@ static int mpileup(mplp_conf_t *conf)
788788
{
789789
conf->buf.l = 0;
790790
ksprintf(&conf->buf, "##contig=<ID=%s,length=%d>", hdr->target_name[i], hdr->target_len[i]);
791-
bcf_hdr_append(conf->bcf_hdr, conf->buf.s);
791+
if ( bcf_hdr_append(conf->bcf_hdr, conf->buf.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
792792
}
793793
conf->buf.l = 0;
794794

vcfannotate.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,7 +2415,7 @@ static void init_columns(args_t *args)
24152415
col->hdr_key_dst = strdup(str.s+5);
24162416
tmp.l = 0;
24172417
ksprintf(&tmp,"##INFO=<ID=%s,Number=.,Type=String,Description=\"Transferred FILTER column\">",col->hdr_key_dst);
2418-
bcf_hdr_append(args->hdr_out, tmp.s);
2418+
if ( bcf_hdr_append(args->hdr_out, tmp.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
24192419
if (bcf_hdr_sync(args->hdr_out) < 0) error_errno("[%s] Failed to update header", __func__);
24202420
int hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, col->hdr_key_dst);
24212421
col->number = bcf_hdr_id2length(args->hdr_out,BCF_HL_INFO,hdr_id);
@@ -2447,7 +2447,7 @@ static void init_columns(args_t *args)
24472447
if ( k<0 ) error("[%s] Failed to parse the header, the ID attribute not found", __func__);
24482448
tmp.l = 0;
24492449
bcf_hrec_format(hrec, &tmp);
2450-
bcf_hdr_append(args->hdr_out, tmp.s);
2450+
if ( bcf_hdr_append(args->hdr_out, tmp.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
24512451
}
24522452
if (bcf_hdr_sync(args->hdr_out) < 0)
24532453
error_errno("[%s] Failed to update header", __func__);
@@ -2481,7 +2481,7 @@ static void init_columns(args_t *args)
24812481
if ( skip_info && khash_str2int_has_key(skip_info,hrec->vals[k]) ) continue;
24822482
tmp.l = 0;
24832483
bcf_hrec_format(hrec, &tmp);
2484-
bcf_hdr_append(args->hdr_out, tmp.s);
2484+
if ( bcf_hdr_append(args->hdr_out, tmp.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
24852485
if (bcf_hdr_sync(args->hdr_out) < 0)
24862486
error_errno("[%s] Failed to update header", __func__);
24872487
int hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, hrec->vals[k]);
@@ -2517,7 +2517,7 @@ static void init_columns(args_t *args)
25172517
if ( skip_fmt && khash_str2int_has_key(skip_fmt,hrec->vals[k]) ) continue;
25182518
tmp.l = 0;
25192519
bcf_hrec_format(hrec, &tmp);
2520-
bcf_hdr_append(args->hdr_out, tmp.s);
2520+
if ( bcf_hdr_append(args->hdr_out, tmp.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
25212521
if (bcf_hdr_sync(args->hdr_out) < 0)
25222522
error_errno("[%s] Failed to update header", __func__);
25232523
int hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, hrec->vals[k]);
@@ -2565,7 +2565,7 @@ static void init_columns(args_t *args)
25652565
if ( !hrec ) error("No such annotation \"%s\" in %s\n", key_src,args->targets_fname);
25662566
tmp.l = 0;
25672567
bcf_hrec_format_rename(hrec, key_dst, &tmp);
2568-
bcf_hdr_append(args->hdr_out, tmp.s);
2568+
if ( bcf_hdr_append(args->hdr_out, tmp.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
25692569
if (bcf_hdr_sync(args->hdr_out) < 0)
25702570
error_errno("[%s] Failed to update header", __func__);
25712571
}
@@ -2698,7 +2698,7 @@ static void init_columns(args_t *args)
26982698
tmp.l = 0;
26992699
bcf_hrec_format_rename(hrec, key_dst, &tmp);
27002700
}
2701-
bcf_hdr_append(args->hdr_out, tmp.s);
2701+
if ( bcf_hdr_append(args->hdr_out, tmp.s) ) error("[%s:%d] failed to update the header\n",__FILE__,__LINE__);
27022702
if (bcf_hdr_sync(args->hdr_out) < 0)
27032703
error_errno("[%s] Failed to update header", __func__);
27042704
hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, key_dst);

0 commit comments

Comments
 (0)