"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
"Todd C. Miller" <Todd.Miller@sudo.ws>
Subject:
Re: use size_t for loop indices to avoid signedness warnings
To:
Stefan Sperling <stsp@stsp.name>
Cc:
Ed Maste <emaste@freebsd.org>, gameoftrees@openbsd.org
Date:
Thu, 17 Dec 2020 15:55:39 -0700

Download raw body.

Thread
On Thu, 17 Dec 2020 21:48:42 +0100, Stefan Sperling wrote:

> Note that the condition i >= 0 is always true for an unsigned type.
> The loop no longer stops iterating and eventually attempts to index
> the array path_list_input with a very large value after i overflows.
>
> I guess changing i to ssize_t would be one possibility, but I don't
> know if that will bring back the warnings you were trying to avoid.
> So instead I have removed the assumption that i can become negative:

You could also just iterate over the number of items instead of the
indexes into the array and move the decrement to immediately before
the call to got_pathlist_insert() to adjust from item number to
array index.  For example:

	for (i = nitems(path_list_input); i > 0; ) {
		i--;
		err = got_pathlist_insert(NULL, &paths, path_list_input[i],
		    NULL);
		if (err) {
			test_printf("%s\n", __func__, err->msg);
			return 0;
		}
	}

Or alternately without the standalone decrememnt:

		err = got_pathlist_insert(NULL, &paths, path_list_input[--i],
		    NULL);

 - todd