From: Stefan Sperling Subject: Re: use size_t for loop indices to avoid signedness warnings To: "Todd C. Miller" Cc: Ed Maste , gameoftrees@openbsd.org Date: Fri, 18 Dec 2020 17:04:10 +0100 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?