
<                                 cp += es;
---
>                                 if (cp + es < tf_buf + tf_bytesperrow) {
>                                     cp += es;
>                                 } else {
>                                     break;  // Exit loop if we're at the end of the buffer
>                                 }
