Patch #2 to Pcomm v1.1

Network News news at amdcad.AMD.COM
Thu Sep 22 06:37:02 AEST 1988


In article <7782 at bcsaic.UUCP> paula at bcsaic.UUCP (Paul Allen) writes:
| In article <416 at quintus.UUCP> ok at quintus.UUCP (Richard A. O'Keefe) writes:
| >In article <13900004 at osiris.cso.uiuc.edu> hood at osiris.cso.uiuc.edu writes:
| >>This patch will fix the problem that some people are having with 
| >>seqmentation faults (although I'm not convinced the problem isn't
| >>in their compiler).
| >
| >It isn't.  It's the famous *NULL bug.
| >
| >>! 	if (*lock_path != NULL && lock_path != NULL) {
| >
| >This only tests whether lock_path is legal *after* trying to use it!
| 
| I've now seen a couple postings about this bug, but nobody has got it
| right yet!  What has been missed is that C makes no guarantee about the
| order of expression evaluation.  The only safe way to perform this test
| is using two nested if statements.

No.  Check your reference again: the operators '&&' '||' ',' all
guarantee left-to-right evaluation of the operands.  This type of
expression is the main reason why && and || are short-circuit
evaluators.  The fix is correctly written:

	if (lock_path != NULL && *lock_path != NULL)

or, if you prefer:

	if (lock_path && *lock_path)


	-- Tim Olson
	Advanced Micro Devices
	(tim at crackle.amd.com)



More information about the Comp.sources.bugs mailing list