From ce728f5471254d2bf6aaa0fbdabcf7ff173f4655 Mon Sep 17 00:00:00 2001 From: On behalf of John Marshall Date: Sat, 27 Feb 2010 11:48:17 +0000 Subject: [PATCH] Improve the invalid 'BAM\1' magic number error message, and also print it when no bytes can be read from the alleged BAM file, e.g., in the common user error case when a SAM file has accidentally been supplied. --- bam.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bam.c b/bam.c index 56c95db..35e5863 100644 --- a/bam.c +++ b/bam.c @@ -70,6 +70,7 @@ bam_header_t *bam_header_read(bamFile fp) { bam_header_t *header; char buf[4]; + int magic_len; int32_t i = 1, name_len; // check EOF i = bgzf_check_EOF(fp); @@ -80,9 +81,9 @@ bam_header_t *bam_header_read(bamFile fp) } else if (i == 0) fprintf(stderr, "[bam_header_read] EOF marker is absent.\n"); // read "BAM1" - if (bam_read(fp, buf, 4) != 4) return 0; - if (strncmp(buf, "BAM\001", 4)) { - fprintf(stderr, "[bam_header_read] wrong header\n"); + magic_len = bam_read(fp, buf, 4); + if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) { + fprintf(stderr, "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n"); return 0; } header = bam_header_init(); -- 2.39.2