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

From:
"Todd C. Miller" <millert@openbsd.org>
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:
Fri, 18 Dec 2020 09:06:59 -0700

Download raw body.

Thread
On Fri, 18 Dec 2020 17:04:10 +0100, Stefan Sperling wrote:

> Macro question aside, do I understand correctly that casting sizeof(x) to
> ssize_t is preferred over casting to an int?
>
> Where my patch uses (int)sizeof(msg) to compare to an int, it should do
> (ssize_t)sizeof(msg) instead?
>
> I guess we'd otherwise risk truncation when the result of sizeof(x) exceeds
> the size of an int? Or is there another problem?

I generally cast to the signed equivalent of the type in question
to avoid truncation.  Realistically, this is a non-issue with
sizeof(foo) since you are not going to have data types with a size
larger than INT_MAX.  So I don't think it really matters in practice.

 - todd