Fix possible memory leak when malloc() fails