aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Wang2023-07-19 09:42:08 -0500
committerAnthony Wang2023-07-19 09:42:08 -0500
commit1f36fa09fa178cd8e7d6ba3ce346abe6faa838b1 (patch)
tree8c78e14265f2e7ce7bce7f0cab6ab68c5b075f1a
parent012a500fdb7aab0b00e2bf80855485a0800abf94 (diff)
Make sure the sum of weights is positive to avoid undefined behavior
-rw-r--r--sd.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/sd.c b/sd.c
index 7d00989..cd8195d 100644
--- a/sd.c
+++ b/sd.c
@@ -59,6 +59,9 @@ int main(int argc, char* argv[]) {
assert(system("stty -F /dev/tty -echo") == 0);
while (true) {
+ /* Make sure sum of weights is positive */
+ assert(seg[1] > 0);
+
int x = (long long)rand() * rand() % seg[1];
int res[2];
query(res, x, 0, N-1, 1);