At first glance, there is a potential memory leak. When
buf2
allocation fails you don't care about previous one (
buf1
) and you immediately return from function without freeing
buf1
. You should take care of
buf1
.
Eg.:
if (!buf2) {
free(buf1);
return -1;
}