From e86976b752d3e5b8ef1fd3290f48d8a872492d45 Mon Sep 17 00:00:00 2001 From: James Bonfield Date: Thu, 16 Nov 2023 16:48:40 +0000 Subject: [PATCH] Prevent extend_ref from making huge mallocs on very sparse data. 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 #1699 --- cram/cram_encode.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cram/cram_encode.c b/cram/cram_encode.c index 7fb5e228ea..14b62850c4 100644 --- a/cram/cram_encode.c +++ b/cram/cram_encode.c @@ -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;