Unreachable code diagnostic

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

 



I was recently sent some code that looked like this:

int foo()
{
	lock();
	return bar();
	unlock();
}

When you're restructuring code that contains locks, this is a
*really* easy mistake to make.  I've done it myself.  But there's no
compiler warning for it!  gcc doesn't have it, sparse doesn't have it.
I've mentioned it to the gcc developers and they don't seem terribly
enthusiastic (they had a -Wunreachable-code at one point, but it
got disabled, probably due to too many false positives like their
-Wmaybe-uninitialized).

Maybe sparse could warn about code after an unconditional return
statement?  I wouldn't like to see it warn about code after a conditional
return statement where the condition is always true; I think that would
have a lot of false positives due to macros.  For example, something
like this:

int foo()
{
	int i = 0;
	for (;;) {
		if (i == FOO_MAX)
			return i;
		bar(i++);
	}
}

if FOO_MAX happens to be 0 should silently optimise to 'return 0' and
not emit a warning.

I would like to see it warn in this case:

int foo()
{
	return bar();
	do { } while (0);
}

As that can be generated by the preprocessor in the case of optimising
away locks for the !SMP case, for example.

Any takers for this idea?  :-)
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux