Can't you replace the loop with just 
`p = (char_u *)malloc((size_t)size);` ?