Fix potential nil pointer error in msg allocation