From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Phillip Wood <phillip.wood123@gmail.com>
Cc: Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>,
git@vger.kernel.org
Subject: Re: [PATCH 4/5] git-compat-util: add `signed_mult_overflows` and `mult_overflows` macros
Date: Sun, 15 Nov 2026 20:29:57 +0100 (CET) [thread overview]
Message-ID: <c84cca33-2267-72de-1e7d-3251d35239ce@gmx.de> (raw)
In-Reply-To: <373f7186-3da6-4715-a4ce-cd5f56895176@gmail.com>
Hi Phillip,
On Thu, 15 Oct 2026, Phillip Wood wrote:
> On 14/10/2026 07:09, Johannes Schindelin via GitGitGadget wrote:
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> >
> > `unsigned_mult_overflows()` does not cover signed arithmetic, but types
> > such as `time_t` can be signed or unsigned. Add signed and type-generic
> > guards so callers can check multiplication overflow in either case.
>
> It would have been nice to mention that the type-generic macro is gcc only so
> we cannot rely on this for safety checks.
True. And that is really not satifying at all.
> > #define maximum_signed_value_of_type(a) \
> > (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
> > +/* Account for the extra negative value on two's-complement targets. */
> > +#define minimum_signed_value_of_type(a) \
> > + (-maximum_signed_value_of_type(a) - (INTMAX_MIN < -INTMAX_MAX))
>
> Do we support any platforms that do not use two's complement arithmetic? C23
> makes it mandatory so presumably there are not may contemporary platforms out
> there that do not use it.
Honestly, I had not even thought about that. I just assumed that it was
the C standard, not something C23 introduced. Sorry about that.
> > +/*
> > + * Returns true if the multiplication of "a" and "b" will
> > + * overflow or underflow a signed type. The types of "a" and "b"
> > + * must match and must be signed. Note that this macro evaluates
> > + * both "a" and "b" twice!
>
> At least twice I think
>
> > + */
> > +#define signed_mult_overflows(a, b) \
>
> As the macro relies on both operands being the same width, can we use
> BUILD_ASSERT_OR_ZERO() to ensure sizeof(a) == sizeof(b) here?
>
> > + (((a) > 0 && (b) > 0 && (a) > maximum_signed_value_of_type(a) / (b)) ||
> > \
> > + ((a) < 0 && (b) < 0 && (a) < maximum_signed_value_of_type(a) / (b)) ||
> > \
> > + ((a) > 0 && (b) < 0 && (b) < minimum_signed_value_of_type(a) / (a)) ||
> > \
> > + ((a) < 0 && (b) > 0 && (a) < minimum_signed_value_of_type(b) / (b)))
Thank you for reviewing to thoroughly, and essentially proving that this
is a doomed approach.
I dropped these macros from v2.
Ciao,
Johannes
>
> Thanks
>
> Phillip
>
> > +
> > +/*
> > + * Returns true if the product of "a" and "b", after the usual arithmetic
> > + * conversions, cannot be represented in the multiplication expression's
> > + * type. The operands must be integers and may be evaluated more than once.
> > + */
> > +#if GIT_GNUC_PREREQ(3, 1)
> > +#define mult_overflows(a, b) \
> > + (is_unsigned_type((a) * (b)) \
> > + ? unsigned_mult_overflows((__typeof__((a) * (b)))(a), \
> > + (__typeof__((a) * (b)))(b)) \
> > + : signed_mult_overflows((__typeof__((a) * (b)))(a), \
> > + (__typeof__((a) * (b)))(b)))
> > +#else
> > +/*
> > + * This implementation needs GNU `typeof` to convert both operands to the
> > + * multiplication's type. Skip the check without that extension.
> > + */
> > +#define mult_overflows(a, b) 0
> > +#endif
> > +
> > /*
> > * Returns true if the left shift of "a" by "shift" bits will
> > * overflow. The type of "a" must be unsigned.
>
>
next prev parent reply other threads:[~2026-11-15 19:30 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-14 6:09 [PATCH 0/5] Address overflows pointed out by Coverity Johannes Schindelin via GitGitGadget
2026-10-14 6:09 ` [PATCH 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-10-14 6:09 ` [PATCH 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-10-16 8:38 ` Patrick Steinhardt
2026-10-16 14:30 ` Junio C Hamano
2026-10-17 0:18 ` Simon Richter
2026-10-17 4:10 ` Junio C Hamano
2026-10-17 4:11 ` Junio C Hamano
2026-10-14 6:09 ` [PATCH 3/5] wrapper: reject insanely large byte counts when reading/writing Johannes Schindelin via GitGitGadget
2026-10-14 11:37 ` Phillip Wood
2026-11-15 19:24 ` Johannes Schindelin
2026-10-14 6:09 ` [PATCH 4/5] git-compat-util: add `signed_mult_overflows` and `mult_overflows` macros Johannes Schindelin via GitGitGadget
2026-10-15 9:27 ` Phillip Wood
2026-11-15 19:29 ` Johannes Schindelin [this message]
2026-10-14 6:09 ` [PATCH 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-10-14 21:44 ` Junio C Hamano
2026-10-15 9:28 ` Phillip Wood
2026-11-15 19:35 ` Johannes Schindelin
2026-10-16 8:43 ` [PATCH 0/5] Address overflows pointed out by Coverity Harald Nordgren
2026-11-07 22:36 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c84cca33-2267-72de-1e7d-3251d35239ce@gmx.de \
--to=johannes.schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=phillip.wood123@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox