www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Cockroach in my D soup

Today, I went back to one of my earlier D projects and recompiled it
with the latest dmd toolchain, and ran into this regression:

	https://d.puremagic.com/issues/show_bug.cgi?id=11576

Fortunately, I was ticked off enough about this to actually dig into the
Phobos code to figure out what's wrong... and it turns out that it's one
of those things that bearophile constantly berates us about: unsafe
handling of unsigned quantities. In this case, it's this loop condition:

	while (left <= right)
		...

where left and right are size_t, and inside the loop body is this code:

	if (...)
	{
		...
		--right;	// <--- spot the bug!
		continue;
	}

Obviously, the code expected the loop condition to terminate the loop
when right < left, but it failed to account for the case where left == 0
and right == 0, and --right turns it into size_t.max.

Well, anyway, I've submitted a pull for this bug (linked in the bugzilla
comments). The frightening thing is, this buggy piece of code has been
in there since March this year, and apparently nobody caught it. I
wouldn't have, either, had I not just happened to try recompiling an old
project (that just happened to use remove!(SwapStrategy.unstable) on the
last element of an array).

Now I feel like I need to write a script that periodically recompiles
old stagnant projects with git HEAD and reports failures, since they
might be indicative of regressions.


T

-- 
Dogs have owners ... cats have staff. -- Krista Casada
Nov 21 2013