Skip to content

Commit

Permalink
Changes to avoid segfault with uncompressed bam (PR #1632)
Browse files Browse the repository at this point in the history
Avoids segfault when writing bam/bcf with mode "wbu" by
changing "wbu" to "wb0".  The ensures the output file will
be properly wrapped in BGZF blocks, even though it's not
been compressed.

Fixes #1617
  • Loading branch information
vasudeva8 authored Jun 9, 2023
1 parent f613a93 commit 5e0ccef
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ char *hts_format_description(const htsFormat *format)

htsFile *hts_open_format(const char *fn, const char *mode, const htsFormat *fmt)
{
char smode[101], *cp, *cp2, *mode_c;
char smode[101], *cp, *cp2, *mode_c, *uncomp = NULL;
htsFile *fp = NULL;
hFILE *hfile = NULL;
char fmt_code = '\0';
Expand All @@ -853,8 +853,13 @@ htsFile *hts_open_format(const char *fn, const char *mode, const htsFormat *fmt)
fmt_code = 'b';
else if (*cp == 'c')
fmt_code = 'c';
else
else {
*cp2++ = *cp;
// Cache the uncompress flag 'u' pos if present
if (!uncomp && (*cp == 'u')) {
uncomp = cp2 - 1;
}
}
}
mode_c = cp2;
*cp2++ = fmt_code;
Expand All @@ -866,6 +871,11 @@ htsFile *hts_open_format(const char *fn, const char *mode, const htsFormat *fmt)
*mode_c = format_to_mode[fmt->format];
}

// Uncompressed bam/bcf is not supported, change 'u' to '0' on write
if (uncomp && *mode_c == 'b' && (strchr(smode, 'w') || strchr(smode, 'a'))) {
*uncomp = '0';
}

// If we really asked for a compressed text format then mode_c above will
// point to nul. We set to 'z' to enable bgzf.
if (strchr(mode, 'w') && fmt && fmt->compression == bgzf) {
Expand Down

0 comments on commit 5e0ccef

Please sign in to comment.