#define SZSIZE 8192 SV* LZW_zread( ... ) { SV* retval; u_char bp, end_bp; u_int offset; ... retval = NEWSV( ..., SZSIZE ); SvPOK_only( retval ); bp = (u_char*)SvPVX( retval ); end_bp = SZSIZE + bp; ... while ( ... ) { if ( bp == end_bp ) { /* When bp is == end_bp then the buffer must be grown. bp is currently just off the end of the buffer and any operations on it is unsafe. */ offset = bp - PvPVX(retval); sv_grow(sv, offset + SZSIZE); bp = PvPVX(retval) + offset; end_bp = SZSIZE + bp; } ... *bp++ = ...; } ... SvCUR_set(sv, bp - SvPVX(retval)); return retval; }