Commit 6b70aed
committed
git-compat-util: add signed_mult_overflows and mult_overflows macros
The existing unsigned_mult_overflows macro detects multiplication
overflow for unsigned types by checking b > MAX / a. Add the
signed counterpart and a type-generic wrapper.
signed_mult_overflows handles all four sign combinations:
positive*positive, negative*negative, positive*negative, and
negative*positive. Each case checks against
maximum_signed_value_of_type to detect both overflow (result >
MAX) and underflow (result < MIN, via the negation trick).
mult_overflows dispatches to the unsigned or signed variant based
on the type's signedness, using a new is_unsigned_type macro.
This is useful for types like time_t whose signedness is
implementation-defined: POSIX does not mandate whether time_t is
signed or unsigned, and platforms differ.
is_unsigned_type uses the GCC typeof extension, which is
available on all compilers git targets (GCC, Clang, MSVC's
C11 mode). The existing codebase already depends on GCC
extensions in other places (e.g., __attribute__).
Assisted-by: Claude Opus 4.6
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>1 parent 94f0577 commit 6b70aed
1 file changed
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
88 | 90 | | |
89 | 91 | | |
90 | 92 | | |
| |||
111 | 113 | | |
112 | 114 | | |
113 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
114 | 138 | | |
115 | 139 | | |
116 | 140 | | |
| |||
0 commit comments