foxygit / doom Log in
commit 5e064562c09ceb5b93a4ddafe734cdb6c7b081c6
Author:     Fabian Greffrath <fabian+debian@greffrath.com>
AuthorDate: Tue Oct 28 06:27:33 2014 +0100
Commit:     Fabian Greffrath <fabian@greffrath.com>
CommitDate: Tue Oct 28 06:36:20 2014 +0100

    setup: dynamically set size of iwad_labels array

    With the addition of the Freedoom IWADs, the number of IWADs supported
    by chocolate-doom has been raised to 10. However, the iwad_labels[]
    array only holds place for up to 8 pointers. Incidently, I have all 10
    IWADs installed and trying to warp into a game from
    chocolate-doom-setup leads to an out-of-bounds access of this array
    and so the application crashes with a segmentation fault.

    Instead of increasing the array size to 10, which will bite us next
    time, I decided to set its size dynamically as soon as the number of
    IWADs of known.
---
 src/setup/multiplayer.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/setup/multiplayer.c b/src/setup/multiplayer.c
index f116ffb2..8c8fe0c5 100644
--- a/src/setup/multiplayer.c
+++ b/src/setup/multiplayer.c
@@ -55,7 +55,7 @@ static const iwad_t fallback_iwads[] = {
 // Array of IWADs found to be installed

 static const iwad_t **found_iwads;
-static char *iwad_labels[8];
+static char **iwad_labels;

 // Index of the currently selected IWAD

@@ -559,10 +559,16 @@ static txt_widget_t *IWADSelector(void)

     for (i=0; found_iwads[i] != NULL; ++i)
     {
-        iwad_labels[i] = found_iwads[i]->description;
          ++num_iwads;
     }

+    iwad_labels = malloc(sizeof(*iwad_labels) * num_iwads);
+
+    for (i=0; i < num_iwads; ++i)
+    {
+        iwad_labels[i] = found_iwads[i]->description;
+    }
+
     // If no IWADs are found, provide Doom 2 as an option, but
     // we're probably screwed.