foxygit / doom Log in
commit 12a2c9441eff63f8cd3db594b826305454736182
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Thu Apr 10 00:29:11 2014 -0400
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Thu Apr 10 00:29:11 2014 -0400

    midifile: Fix portability issue with malloc(0).

    In the C standards, malloc(0) may return NULL without indicating a
    failure to allocate. As values read from the MIDI file may be
    arbitrary, add one to the allocated length so that we always allocated
    a positive value. This fixes #165 (thanks nmain).
---
 src/midifile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/midifile.c b/src/midifile.c
index 00ff28d4..cb366774 100644
--- a/src/midifile.c
+++ b/src/midifile.c
@@ -176,9 +176,10 @@ static void *ReadByteSequence(unsigned int num_bytes, FILE *stream)
     unsigned int i;
     byte *result;

-    // Allocate a buffer:
+    // Allocate a buffer. Allocate one extra byte, as malloc(0) is
+    // non-portable.

-    result = malloc(num_bytes);
+    result = malloc(num_bytes + 1);

     if (result == NULL)
     {