]> git.donarmstrong.com Git - samtools.git/commitdiff
Improve the invalid 'BAM\1' magic number error message, and also print it
authorOn behalf of John Marshall <lh3@sanger.ac.uk>
Sat, 27 Feb 2010 11:48:17 +0000 (11:48 +0000)
committerOn behalf of John Marshall <lh3@sanger.ac.uk>
Sat, 27 Feb 2010 11:48:17 +0000 (11:48 +0000)
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

diff --git a/bam.c b/bam.c
index 56c95db4e2c42c404627e41bd1ca2360a496af73..35e5863ca4fdbe85e2ef1226773f16703f05786b 100644 (file)
--- 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();