[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gcc and mint-libs PL46



Chris Herborth wrote:

> What?!?  So, I should do this:
> 
> if( fname ) {
> 	fp = fopen( fname, "r" );
> 	if( !fp ) {
> 		fprintf( stderr, "oops.  No filename here at line %d.\n", __LINE__ );
> 		exit( ENOENT );
> 	}
> } else {
> 	fprintf( stderr, "oops, didn't pass a filename.\n" );
> }

	There is no way to decide at the application level if "fname"
points to a valid string. Only the kernel knows the memory layout of a
user process, which means that the error can only be detected when the
final system call is issued (open() in Unix, Fopen() in MiNT).

	What you propose is _exactly_ what should be avoided: putting
tests in places where they don't belong. Duplicating tests at all layers
of a library is not only a waste of CPU, but it also pollutes the
conceptual model. You should think of library function as "contracts":
if you give them valid input, then they will behave in a predictable
way. If you give them garbage, you'll get what you deserve :-)

Thierry.