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

From:
Stefan Sperling <stsp@stsp.name>
Subject:
Re: use size_t for loop indices to avoid signedness warnings
To:
"Todd C. Miller" <Todd.Miller@sudo.ws>
Cc:
Ed Maste <emaste@freebsd.org>, gameoftrees@openbsd.org
Date:
Fri, 18 Dec 2020 17:04:10 +0100

Download raw body.

Thread
On Fri, Dec 18, 2020 at 08:54:33AM -0700, Todd C. Miller wrote:
> Something I find useful is a macro like the following:
> 
> /* sizeof() that returns a signed value */
> #define ssizeof(_x)	((ssize_t)sizeof(_x))
> 
> Then I just use ssizeof(foo) when I need to compare against a signed
> integer.
> 
> Opinions vary on whether it is better to hide the cast or not in
> this way :-)
> 
>  - todd
> 

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?