From bee71503efa3896a78651a8c05be3245e20dfce4 Mon Sep 17 00:00:00 2001 From: Nils Homer Date: Mon, 22 Dec 2025 16:01:24 -0700 Subject: [PATCH] fix: prevent integer overflow and handle negative distance in position calculation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Clamp distance `d` to valid range [min_dist, l] where min_dist = read1_len + read2_len - Use 64-bit arithmetic for range calculation to prevent overflow - Skip iteration if calculated range is invalid This fixes issues where extreme values from the normal distribution could cause: 1. Negative distance values leading to incorrect position calculations 2. Integer overflow in (l - d + 1) expression Closes #96 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/dwgsim.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/dwgsim.c b/src/dwgsim.c index 6d2bbd5..9edb3f9 100644 --- a/src/dwgsim.c +++ b/src/dwgsim.c @@ -646,14 +646,21 @@ void dwgsim_core(dwgsim_opt_t * opt) ran = ran_normal(); ran = ran * opt->std_dev + opt->dist; d = (int)(ran + 0.5); + // Clamp d to valid range to prevent overflow + int min_dist = s[0] + s[1]; + if (d < min_dist) d = min_dist; + if (d > l) d = l; } else { d = 0; } - pos = (int)((l - d + 1) * drand48()); - } while (pos < 0 - || pos >= seq.l - || pos + d - 1 >= seq.l + // Use 64-bit arithmetic to prevent overflow + int64_t range = (int64_t)l - d + 1; + if (range <= 0) continue; + pos = (int)(range * drand48()); + } while (pos < 0 + || pos >= seq.l + || pos + d - 1 >= seq.l || (0 < s[1] && 0 == opt->is_inner && ((0 < s[0] && d <= s[1]) || (d <= s[0] && 0 < s[1])))); } else { @@ -662,11 +669,18 @@ void dwgsim_core(dwgsim_opt_t * opt) ran = ran_normal(); ran = ran * opt->std_dev + opt->dist; d = (int)(ran + 0.5); + // Clamp d to valid range to prevent overflow + int min_dist = s[0] + s[1]; + if (d < min_dist) d = min_dist; + if (d > l) d = l; } else { d = 0; } - pos = (int)((l - d + 1) * drand48()); + // Use 64-bit arithmetic to prevent overflow + int64_t range = (int64_t)l - d + 1; + if (range <= 0) continue; + pos = (int)(range * drand48()); // convert in the bed file for(i=0;in;i++) { // TODO: regions are in sorted order... so optimize if(contig_i == regions_bed->contig[i]) {