Package: staden-io-lib / 1.14.11-6

37b33d3.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
From: James Bonfield <jkb@sanger.ac.uk>
Date: Thu, 6 Dec 2018 12:00:39 +0000
Origin: https://github.com/jkbonfield/io_lib/commit/37b33d3.patch
Bug-Debian: https://bugs.debian.org/915459
Subject: [PATCH] Bug multi-threaded CRAM decode on some systems.

The bulk_cram_to_bam function allocated a large array for the BAM
objects, but did not ensure the bam structures are aligned on a word
boundary.  This gave rise to bus errors.  Fixes debian Bug#915459
---
 io_lib/bam.h         | 3 ++-
 io_lib/cram_decode.c | 9 +++++----
 2 files changed, 7 insertions(+), 5 deletions(-)

--- a/io_lib/bam.h
+++ b/io_lib/bam.h
@@ -271,8 +271,9 @@ static inline void bam_set_bin(bam_seq_t
 #define bam_qual(b)      (bam_seq(b) + (int)(((b)->len+1)/2))
 #define bam_aux(b)       (bam_qual(b) + (b)->len)
 
-/* Rounds up to the next multiple of 4 */
+/* Rounds up to the next multiple of 4 or 8 */
 #define round4(v) (((v-1)&~3)+4)
+#define round8(v) (((v-1)&~7)+8)
 
 /* CIGAR operations, taken from samtools bam.h */
 #define BAM_CIGAR_SHIFT 4
--- a/io_lib/cram_decode.c
+++ b/io_lib/cram_decode.c
@@ -2297,14 +2297,15 @@ static int bulk_cram_to_bam(SAM_hdr *bfd
 	int sz = bam_size(bfd, fd, &s->crecs[i]);
 	if (i < 10000)
 	    sizes[i] = sz;
-	len += sz;
+	len += round8(sz);
     }
 
-    s->bl = (bam_seq_t **)malloc(s->hdr->num_records * sizeof(*s->bl) + len);
+    s->bl = (bam_seq_t **)malloc(s->hdr->num_records * sizeof(*s->bl) + len + 8);
     if (!s->bl)
 	return -1;
 
-    char *x = ((char *)s->bl) + s->hdr->num_records * sizeof(*s->bl);
+    // Round up to next multiple of 8, to ensure bam structs are 8-byte aligned.
+    char *x = ((char *)s->bl) + round8(s->hdr->num_records * sizeof(*s->bl));
     for (i = 0; i < s->hdr->num_records; i++) {
 	bam_seq_t *b = (bam_seq_t *)x, *o = b;
 	int bsize = i < 10000 ? sizes[i] : bam_size(bfd, fd, &s->crecs[i]);
@@ -2312,7 +2313,7 @@ static int bulk_cram_to_bam(SAM_hdr *bfd
 	r |= (cram_to_bam(fd->header, fd, s, &s->crecs[i], i, &b) < 0);
 	// if we allocated enough, the above won't have resized b
 	assert(o == b && o->alloc == bsize);
-	x += bsize;
+	x += round8(bsize);
 	s->bl[i] = b;
     }