summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Howard2014-04-10 00:29:11 -0400
committerSimon Howard2014-04-10 00:29:11 -0400
commit12a2c9441eff63f8cd3db594b826305454736182 (patch)
treea2a5bfd4c7e83d9fd7fefde0336dee1da8502f96
parent174fe185ec8b05a1dc6ce8c49ffc5a0aa1c81855 (diff)
downloadchocolate-doom-12a2c9441eff63f8cd3db594b826305454736182.tar.gz
chocolate-doom-12a2c9441eff63f8cd3db594b826305454736182.tar.bz2
chocolate-doom-12a2c9441eff63f8cd3db594b826305454736182.zip
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).
-rw-r--r--src/midifile.c5
1 files 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)
{