Copy the whole kstring_t even if it contains NULs

FASTQ files containing NULs are invalid but should not cause bwa to
crash, as it does if the quality line contains a NUL.  Fixes #122.
This commit is contained in:
John Marshall 2017-06-30 12:46:56 +01:00
parent a61b1dc610
commit 690649872b
1 changed files with 15 additions and 5 deletions

20
bwa.c
View File

@ -30,13 +30,23 @@ static inline void trim_readno(kstring_t *s)
s->l -= 2, s->s[s->l] = 0; s->l -= 2, s->s[s->l] = 0;
} }
static inline char *dupkstring(const kstring_t *str, int dupempty)
{
char *s = (str->l > 0 || dupempty)? malloc(str->l + 1) : NULL;
if (!s) return NULL;
memcpy(s, str->s, str->l);
s[str->l] = '\0';
return s;
}
static inline void kseq2bseq1(const kseq_t *ks, bseq1_t *s) static inline void kseq2bseq1(const kseq_t *ks, bseq1_t *s)
{ // TODO: it would be better to allocate one chunk of memory, but probably it does not matter in practice { // TODO: it would be better to allocate one chunk of memory, but probably it does not matter in practice
s->name = strdup(ks->name.s); s->name = dupkstring(&ks->name, 1);
s->comment = ks->comment.l? strdup(ks->comment.s) : 0; s->comment = dupkstring(&ks->comment, 0);
s->seq = strdup(ks->seq.s); s->seq = dupkstring(&ks->seq, 1);
s->qual = ks->qual.l? strdup(ks->qual.s) : 0; s->qual = dupkstring(&ks->qual, 0);
s->l_seq = strlen(s->seq); s->l_seq = ks->seq.l;
} }
bseq1_t *bseq_read(int chunk_size, int *n_, void *ks1_, void *ks2_) bseq1_t *bseq_read(int chunk_size, int *n_, void *ks1_, void *ks2_)