if no eol is found in the string we set "next" to NULL, assign it to "buf" and then check the loop condition by dereferencing it - which does not work well. Change the loop condition to first check for buf == NULL.