[Tux3] [PATCH] Make sure ileaf offsets are in non-descending order

Daniel Phillips phillips at phunq.net
Tue Sep 2 02:27:55 PDT 2008


On Monday 01 September 2008 23:56, Conrad Meyer wrote:
> +int isinorder(BTREE, struct ileaf *leaf)
> +{
> +	int offset = 0;
> +	u16 *dict = (void *)leaf + btree->sb->blocksize;
> +	int correct = 1;
> +	for (int i = 0; i > -leaf->count; i--)
> +	{
> +		int limit = dict[i];
> +		if (limit < offset)
> +		{
> +			correct = 0;
> +			break;
> +		}
> +		offset = limit;
> +	}
> +	return correct;
> +}

Your negative for loop is stylish.  Let's try crunching down the loop
body a little:

int isinorder(BTREE, struct ileaf *leaf)
{
	u16 *dict = (void *)leaf + btree->sb->blocksize;
	for (int i = 0, offset = 0, limit; i > -leaf->count; i--, offset = limit)
		if ((limit = dict[i]) < offset)
			return 0;
	return 1;
}

Wow, it compresses nicely :-)

It is still your nice clear and correct algorithm, just poked a little.
You can integrate it with the existing (lame) check like this:

int ileaf_check(BTREE, struct ileaf *leaf)
{
	char *why;
	why = "not an inode table leaf";
	if (leaf->magic != 0x90de)
		goto eek;
	why = "dict out of order";
	if (!isinorder(btree, leaf))
		goto eek;
	return 0;
eek:
	printf("%s!\n", why);
	return -1;
}

_______________________________________________
Tux3 mailing list
Tux3 at tux3.org
http://tux3.org/cgi-bin/mailman/listinfo/tux3



More information about the Tux3 mailing list