Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ca36677
Fix worst-case memory used by cram_huffman_encode_store.
jkbonfield Nov 11, 2026
31ed3b6
Avoid double free in cram_codec_decoder2encoder on error
jkbonfield Nov 11, 2026
3139dba
Fix cram_populate_ref / mfsteal interaction on empty files.
jkbonfield Nov 11, 2026
99ca6c7
Protect against recursion in cram_byte_array_len_decode_init.
jkbonfield Nov 11, 2026
500b283
Fix bugs in tokenise_search_path.
jkbonfield Nov 12, 2026
805cc94
Basic validation on CRAM fai loading.
jkbonfield Nov 12, 2026
eb83d05
Check basic filename validity in expand_cache_path.
jkbonfield Nov 12, 2026
9f97468
Fix hts_pos_t vs int in cram_get_ref.
jkbonfield Nov 12, 2026
5e14072
Fix missing pthread unlocks in cram_get_ref error-on-bgzf_close case
jkbonfield Nov 12, 2026
fd2915d
Fix decode failure on large queries using bit-based codecs.
jkbonfield Nov 12, 2026
33bfa0f
Some adjustments to accommodate 64-bit values in the encodings.
jkbonfield Nov 12, 2026
0f48e8a
Fix cram-store_container's record_counter encoding.
jkbonfield Nov 12, 2026
8b8c852
Correct parameter sizing in cram_container_num2offset_
jkbonfield Nov 12, 2026
7bb5efc
Use 64-bit positions in cram_index_build_multiref.
jkbonfield Nov 12, 2026
0c125aa
Fix bounds checks in expand_cache_path expansion.
jkbonfield Nov 12, 2026
2747b45
Improve cram_read_file_def version validation.
jkbonfield Nov 12, 2026
917aed9
Improved fread error handling in mfload.
jkbonfield Nov 12, 2026
e7e6b68
Miscellaneous minor cram memory leak fixes.
jkbonfield Nov 12, 2026
d40dfd3
Avoid variable shadowing in cram_stats_encoding
jkbonfield Nov 23, 2026
f923e2d
Optimise reallocation in cram_stats_encoding
jkbonfield Nov 23, 2026
58bcfb3
Switch mfload to using fstat over stat.
jkbonfield Nov 26, 2026
2b42336
Simplify cram_stats_encoding.
jkbonfield Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 60 additions & 34 deletions cram/cram_codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,14 @@ int cram_external_encode_store(cram_codec *c, cram_block *b, char *prefix,
len += l;
}

tp += c->vv->varint_put32(tp, tpend, c->u.e_external.content_id);
len += (n = c->vv->varint_put32_blk(b, c->codec)); r |= n;
len += (n = c->vv->varint_put32_blk(b, tp-tmp)); r |= n;
tp += (n = c->vv->varint_put32(tp, tpend, c->u.e_external.content_id));
r |= n<=0;
len += (n = c->vv->varint_put32_blk(b, c->codec)); r |= n<=0;
len += (n = c->vv->varint_put32_blk(b, tp-tmp)); r |= n<=0;
BLOCK_APPEND(b, tmp, tp-tmp);
len += tp-tmp;

if (r > 0)
if (r == 0)
return len;

block_err:
Expand Down Expand Up @@ -720,15 +721,15 @@ int cram_beta_encode_store(cram_codec *c, cram_block *b,
len += l;
}

len += (n = c->vv->varint_put32_blk(b, c->codec)); r |= n;
len += (n = c->vv->varint_put32_blk(b, c->codec)); r |= n<=0;
// codec length
len += (n = c->vv->varint_put32_blk(b, c->vv->varint_size(c->u.e_beta.offset)
+ c->vv->varint_size(c->u.e_beta.nbits)));
r |= n;
len += (n = c->vv->varint_put32_blk(b, c->u.e_beta.offset)); r |= n;
len += (n = c->vv->varint_put32_blk(b, c->u.e_beta.nbits)); r |= n;
r |= n<=0;
len += (n = c->vv->varint_put32_blk(b, c->u.e_beta.offset)); r |= n<=0;
len += (n = c->vv->varint_put32_blk(b, c->u.e_beta.nbits)); r |= n<=0;

if (r > 0) return len;
if (r == 0) return len;

block_err:
return -1;
Expand Down Expand Up @@ -1378,7 +1379,7 @@ cram_codec *cram_huffman_decode_init(cram_block_compression_hdr *hdr,
else
h->decode = cram_huffman_decode_int;
} else {
return NULL;
goto malformed;
}
h->describe = cram_huffman_describe;

Expand Down Expand Up @@ -1475,7 +1476,7 @@ int cram_huffman_encode_long(cram_slice *slice, cram_codec *c,
int64_t *syms = (int64_t *)in;

while (in_size--) {
int sym = *syms++;
int64_t sym = *syms++;

if (sym >= -1 && sym < MAX_HUFF) {
i = c->u.e_huffman.val2code[sym+1];
Expand Down Expand Up @@ -1525,48 +1526,59 @@ int cram_huffman_encode_store(cram_codec *c, cram_block *b, char *prefix,
*
* Symbols themselves could be 5 bytes (eg -1 is 5 bytes in itf8).
*
* Therefore 6*ncodes + 5 + 5 + 1 + 5 is max memory
* Therefore 6*ncodes + 5 + 5 + 1 + 5 is max memory (ITF8)
* 10*ncodes + 5 + 5 + 1 + 5 for LTF8.
*/
char *tmp = hts_malloc_pse(6, c->u.e_huffman.nvals, 0, 16);
char *tp = tmp, *tpend = tmp+6*c->u.e_huffman.nvals+16;

size_t per_sym = c->u.e_huffman.option == E_LONG ? 10 : 6;
char *tmp = hts_malloc_pse(per_sym, c->u.e_huffman.nvals, 0, 16);
if (!tmp)
return -1;

char *tp = tmp, *tpend = tmp + per_sym*c->u.e_huffman.nvals + 16;

if (prefix) {
size_t l = strlen(prefix);
BLOCK_APPEND(b, prefix, l);
len += l;
}

tp += c->vv->varint_put32(tp, tpend, c->u.e_huffman.nvals);
tp += (n = c->vv->varint_put32(tp, tpend, c->u.e_huffman.nvals));
r |= n<=0;
if (c->u.e_huffman.option == E_LONG) {
for (i = 0; i < c->u.e_huffman.nvals; i++) {
tp += c->vv->varint_put64(tp, tpend, codes[i].symbol);
tp += (n = c->vv->varint_put64(tp, tpend, codes[i].symbol));
r |= n<=0;
}
} else if (c->u.e_huffman.option == E_INT || c->u.e_huffman.option == E_BYTE) {
for (i = 0; i < c->u.e_huffman.nvals; i++) {
tp += c->vv->varint_put32(tp, tpend, codes[i].symbol);
tp += (n = c->vv->varint_put32(tp, tpend, codes[i].symbol));
r |= n<=0;
}
} else {
free(tmp);
return -1;
}

tp += c->vv->varint_put32(tp, tpend, c->u.e_huffman.nvals);
for (i = 0; i < c->u.e_huffman.nvals; i++)
tp += c->vv->varint_put32(tp, tpend, codes[i].len);
tp += (n = c->vv->varint_put32(tp, tpend, c->u.e_huffman.nvals));
r |= n <= 0;
for (i = 0; i < c->u.e_huffman.nvals; i++) {
tp += (n = c->vv->varint_put32(tp, tpend, codes[i].len));
r |= n<=0;
}

len += (n = c->vv->varint_put32_blk(b, c->codec)); r |= n;
len += (n = c->vv->varint_put32_blk(b, tp-tmp)); r |= n;
len += (n = c->vv->varint_put32_blk(b, c->codec)); r |= n<=0;
len += (n = c->vv->varint_put32_blk(b, tp-tmp)); r |= n<=0;
BLOCK_APPEND(b, tmp, tp-tmp);
len += tp-tmp;

free(tmp);
tmp = NULL;

if (r > 0)
if (r == 0)
return len;

block_err:
free(tmp);
return -1;
}

Expand Down Expand Up @@ -1831,6 +1843,9 @@ cram_codec *cram_byte_array_len_decode_init(cram_block_compression_hdr *hdr,
char *cp = data;
char *endp = data + size;

if (option != E_BYTE_ARRAY && option != E_BYTE_ARRAY_BLOCK)
return NULL;

if (!(c = malloc(sizeof(*c))))
return NULL;

Expand Down Expand Up @@ -1924,12 +1939,12 @@ int cram_byte_array_len_encode_store(cram_codec *c, cram_block *b,
len3 = tc->store(tc, b_val, NULL, version);
if (len3 < 0) goto block_err;

len += (n = c->vv->varint_put32_blk(b, c->codec)); r |= n;
len += (n = c->vv->varint_put32_blk(b, len2+len3)); r |= n;
len += (n = c->vv->varint_put32_blk(b, c->codec)); r |= n<=0;
len += (n = c->vv->varint_put32_blk(b, len2+len3)); r |= n<=0;
BLOCK_APPEND(b, BLOCK_DATA(b_len), BLOCK_SIZE(b_len));
BLOCK_APPEND(b, BLOCK_DATA(b_val), BLOCK_SIZE(b_val));

if (r <= 0)
if (r)
goto block_err;

cram_free_block(b_len);
Expand Down Expand Up @@ -2147,7 +2162,7 @@ void cram_byte_array_stop_encode_free(cram_codec *c) {

int cram_byte_array_stop_encode_store(cram_codec *c, cram_block *b,
char *prefix, int version) {
int len = 0;
int len = 0, r = 0, n;
char buf[20], *cp = buf;

if (prefix) {
Expand All @@ -2156,25 +2171,34 @@ int cram_byte_array_stop_encode_store(cram_codec *c, cram_block *b,
len += l;
}

cp += c->vv->varint_put32(cp, buf+20, c->codec);
cp += (n = c->vv->varint_put32(cp, buf+20, c->codec));
r |= n<=0;

if (CRAM_MAJOR_VERS(version) == 1) {
cp += c->vv->varint_put32(cp, buf+20, 5);
cp += (n = c->vv->varint_put32(cp, buf+20, 5));
r |= n <= 0;
*cp++ = c->u.e_byte_array_stop.stop;
*cp++ = (c->u.e_byte_array_stop.content_id >> 0) & 0xff;
*cp++ = (c->u.e_byte_array_stop.content_id >> 8) & 0xff;
*cp++ = (c->u.e_byte_array_stop.content_id >> 16) & 0xff;
*cp++ = (c->u.e_byte_array_stop.content_id >> 24) & 0xff;
} else {
cp += c->vv->varint_put32(cp, buf+20, 1 +
c->vv->varint_size(c->u.e_byte_array_stop.content_id));
cp += (n = c->vv->varint_put32
(cp, buf+20,
1 + c->vv->varint_size(c->u.e_byte_array_stop.content_id)));
r |= n<=0;
*cp++ = c->u.e_byte_array_stop.stop;
cp += c->vv->varint_put32(cp, buf+20, c->u.e_byte_array_stop.content_id);
cp += (n = c->vv->varint_put32(cp, buf+20,
c->u.e_byte_array_stop.content_id));
r |= n<=0;
}

BLOCK_APPEND(b, buf, cp-buf);
len += cp-buf;

if (r)
return -1;

return len;

block_err:
Expand Down Expand Up @@ -2400,7 +2424,7 @@ int cram_codec_decoder2encoder(cram_fd *fd, cram_codec *c) {
t->u.e_huffman.nvals = c->u.huffman.ncodes;
t->u.e_huffman.option = c->u.huffman.option;
for (j = 0; j < t->u.e_huffman.nvals; j++) {
int32_t sym = t->u.e_huffman.codes[j].symbol;
int64_t sym = t->u.e_huffman.codes[j].symbol;
if (sym >= -1 && sym < MAX_HUFF)
t->u.e_huffman.val2code[sym+1] = j;
}
Expand Down Expand Up @@ -2452,6 +2476,8 @@ int cram_codec_decoder2encoder(cram_fd *fd, cram_codec *c) {
t->u.e_byte_array_len.val_codec = c->u.byte_array_len.val_codec;
if (cram_codec_decoder2encoder(fd, t->u.e_byte_array_len.len_codec) == -1 ||
cram_codec_decoder2encoder(fd, t->u.e_byte_array_len.val_codec) == -1) {
t->u.e_byte_array_len.len_codec = NULL; // avoid double free
t->u.e_byte_array_len.val_codec = NULL;
t->free(t);
return -1;
}
Expand Down
12 changes: 5 additions & 7 deletions cram/cram_codecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,12 @@ cram_codec *cram_encoder_init(enum cram_encoding codec, cram_stats *st,
* Return 0 if there are enough
* 1 if not.
*/

static inline int cram_not_enough_bits(cram_block *blk, uint64_t nbits) {
if ((blk->byte >= blk->uncomp_size && nbits > 0) ||
(blk->uncomp_size - blk->byte <= INT32_MAX / 8 + 1 &&
(blk->uncomp_size - blk->byte) * 8 + blk->bit - 7 < nbits)) {
return 1;
}
return 0;
if (blk->byte >= (size_t)blk->uncomp_size)
return nbits > 0;

uint64_t rem = (uint64_t)(blk->uncomp_size-blk->byte) * 8 + blk->bit - 7;
return rem < nbits;
}

/*
Expand Down
12 changes: 6 additions & 6 deletions cram/cram_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ static int cram_index_build_multiref(cram_fd *fd,
ref_end = INT_MIN;

int32_t last_ref = -9;
int32_t last_pos = -9;
int64_t last_pos = -9;
for (i = 0; i < s->hdr->num_records; i++) {
if (s->crecs[i].ref_id == last_ref && s->crecs[i].apos < last_pos) {
hts_log_error("CRAM file is not sorted by chromosome / position");
Expand Down Expand Up @@ -801,7 +801,7 @@ int cram_index_build(cram_fd *fd, const char *fn_base, const char *fn_idx) {
while ((c = cram_read_container(fd))) {
if (fd->err) {
perror("Cram container read");
return -1;
goto err;
}

hpos = htell(fd->fp);
Expand Down Expand Up @@ -928,8 +928,8 @@ int64_t cram_num_containers(cram_fd *fd) {
*
* The index must have previously been loaded, otherwise <0 is returned.
*/
static cram_index *cram_container_num2offset_(cram_index *e, int num,
int64_t *last_pos, int *nc) {
static cram_index *cram_container_num2offset_(cram_index *e, int64_t num,
int64_t *last_pos, int64_t *nc) {
if (e->offset) {
if (e->offset != *last_pos) {
if (*nc == num)
Expand All @@ -953,8 +953,8 @@ static cram_index *cram_container_num2offset_(cram_index *e, int num,
}

off_t cram_container_num2offset(cram_fd *fd, int64_t num) {
int nc = 0, i;
int64_t last_pos = -9;
int i;
int64_t nc = 0, last_pos = -9;
cram_index *e = NULL;

for (i = 0; i < fd->index_sz; i++) {
Expand Down
Loading
Loading