Skip to content

Commit

Permalink
Prevent extend_ref from making huge mallocs on very sparse data.
Browse files Browse the repository at this point in the history
If we have a few reads with 64-bit positions widly far apart, we could
attempt to allocate huge sums of memory.  The embedded reference isn't
even helpful in this scenario, so we can limit this allocation to
something more respectable.

Fixes samtools#1699
  • Loading branch information
jkbonfield committed Nov 17, 2023
1 parent 3ece0c1 commit e86976b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cram/cram_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,12 @@ static inline int extend_ref(char **ref, uint32_t (**hist)[5], hts_pos_t pos,
if (pos < *ref_end)
return 0;

// Refuse to work on excessively large blocks.
// We'll just switch to referenceless encoding, which is probably better
// here as this must be very sparse data anyway.
if (pos - ref_start > UINT_MAX)
return -1;

// realloc
hts_pos_t old_end = *ref_end ? *ref_end : ref_start;
hts_pos_t new_end = *ref_end = ref_start + 1000 + (pos-ref_start)*1.5;
Expand Down

0 comments on commit e86976b

Please sign in to comment.