From b328795051c83958b85d929357e76df3193598ef Mon Sep 17 00:00:00 2001 From: Heng Li Date: Tue, 13 Feb 2018 10:00:22 -0500 Subject: [PATCH] r706: don't segfault upon wrong FASTA/Q (#111) The lack of robustness cost me several hours to identify. --- bseq.c | 14 +++++++++++--- main.c | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/bseq.c b/bseq.c index db79299..6bf55f5 100644 --- a/bseq.c +++ b/bseq.c @@ -54,15 +54,23 @@ void mm_bseq_close(mm_bseq_file_t *fp) free(fp); } +static inline char *kstrdup(const kstring_t *s) +{ + char *t; + t = (char*)malloc(s->l + 1); + memcpy(t, s->s, s->l + 1); + return t; +} + static inline void kseq2bseq(kseq_t *ks, mm_bseq1_t *s, int with_qual) { int i; - s->name = strdup(ks->name.s); - s->seq = strdup(ks->seq.s); + s->name = kstrdup(&ks->name); + s->seq = kstrdup(&ks->seq); for (i = 0; i < ks->seq.l; ++i) // convert U to T if (s->seq[i] == 'u' || s->seq[i] == 'U') --s->seq[i]; - s->qual = with_qual && ks->qual.l? strdup(ks->qual.s) : 0; + s->qual = with_qual && ks->qual.l? kstrdup(&ks->qual) : 0; s->l_seq = ks->seq.l; } diff --git a/main.c b/main.c index 9fb8115..da7931b 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ #include "mmpriv.h" #include "getopt.h" -#define MM_VERSION "2.8-r703-dirty" +#define MM_VERSION "2.8-r706-dirty" #ifdef __linux__ #include