
Originally Posted by
Salem
I'm baffled as to how it's even remotely close to answering the original question.
He wanted to check if a pointer actually has X bytes so I mentioned a solution I know of, I know it's imperfect but it does avoid segfaults caused by just trying to access the memory, doesn't check for situations like this though:
Code:
int main() { char a = 0, b = 0, c = 0; stringcopy( &a, "12", 2 ); }
int stringcopy( char *a, char *b, size_t n )
{
int err = isPointerValid( &a, n );
if ( err ) return err;
memcpy( a, b, n );
return 0;
}
Corruption will still occur in such a situation, checking if n bytes should be accessible for the pointer is still impossible without correct information being fed to the function