shithub: choc

Download patch

ref: 12a2c9441eff63f8cd3db594b826305454736182
parent: 174fe185ec8b05a1dc6ce8c49ffc5a0aa1c81855
author: Simon Howard <fraggle@gmail.com>
date: Wed Apr 9 20:29:11 EDT 2014

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).

--- a/src/midifile.c
+++ b/src/midifile.c
@@ -176,9 +176,10 @@
     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)
     {