diff options
| author | Benjamin Linskey | 2026-05-30 03:53:22 -0400 |
|---|---|---|
| committer | Benjamin Linskey | 2026-05-30 04:21:56 -0400 |
| commit | e02952c65d11cc4b8791987e90d373119dbe037b (patch) | |
| tree | 2f2df0402b2324a88e249c8f6a13603dc9e4c790 | |
| parent | 1e4db09c137061623603c606f0e7e8ffdc807606 (diff) | |
| download | rogue-e02952c65d11cc4b8791987e90d373119dbe037b.tar.gz | |
Fix crash during initialization
Randomization code that swaps two string values using strlcpy(3) during initialization frequently selected the same string as both the source and the destination. This is supported on NetBSD but results in undefined behavior on other BSD-derived systems. As a result, the program usually crashed during startup during testing on macOS. Fix it by ensuring that the two random values differ.
| -rw-r--r-- | inventory.c | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/inventory.c b/inventory.c index 934e8aa..83d69a5 100644 --- a/inventory.c +++ b/inventory.c @@ -431,7 +431,11 @@ mix_colors(void) for (i = 0; i <= 32; i++) { j = get_rand(0, (POTIONS - 1)); - k = get_rand(0, (POTIONS - 1)); + k = get_rand(0, (POTIONS - 2)); + if (k >= j) { + k++; + } + strlcpy(t, id_potions[j].title, sizeof(t)); strlcpy(id_potions[j].title, id_potions[k].title, sizeof(id_potions[j].title)); |