buffer: Use end_pos instead of strlen to find out when to grow the buffer