]> git.ipfire.org Git - thirdparty/haproxy.git/commit
MINOR: intops: add a multiply overflow detection for ulong and size_t
authorWilly Tarreau <w@1wt.eu>
Wed, 20 May 2026 14:22:51 +0000 (16:22 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 20 May 2026 15:05:19 +0000 (17:05 +0200)
commitb62ba7592adf68dc594723f0eaabe69e68a30e71
tree72c9f6ca900ebbea3c172e97d63f4fcd55fdcce0
parent3e25104a9ced06ba43b262a7863a0c2f0ccd245a
MINOR: intops: add a multiply overflow detection for ulong and size_t

Sometimes we'd like to know if some products overflow, so let's add a
pair of functions for this, for ulong and for size_t. For recent enough
compilers (gcc >= 5, clang >= 3.4) we just use __builtin_mul_overflow()
otherwise we rely on a division and a comparison before performing the
operation.

A third function, array_size_or_fail() computes the size of an array
of m elements of n bytes each, and returns the total size if it fits
in a size_t, otherwise ~0 if it does not so that passing this to
malloc() or any other variant would fail by trying to exhaust the
entire memory space.
include/haproxy/intops.h