Add NULL check for malloc in buffer.c