I have the following routine for checking if a pointer has gone out of bounds and resetting it back to the start.
char boudary_configure(struct ff_file * stream, char ** boundary, char * violation){ // test for boundary violation if(((char *)*boundary - stream->directory_base) == cluster_size(stream)){ if(another_cluster(stream) == 0) return 0; printf("\n\rViolation Detected!!!"); *violation = 1; // reset working data pointer *boundary = (char *)stream->directory_base; } return 1; }
And example of its use if like so.
struct stream_extension * streamExt; if(boudary_configure(file, (char **)&streamExt, &violation) == 0) break;
So if streamExt is at the end of cluster_size then it is reset to the base address.