Fix potential segfault on malloc failure