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

changes to avoid segfault with uncompressed bam #1632

Merged
merged 2 commits into from
Jun 9, 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
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