@@ -85,6 +85,8 @@ struct strbuf;
8585
8686#define bitsizeof (x ) (CHAR_BIT * sizeof(x))
8787
88+ #define is_unsigned_type (a ) ((typeof(a))-1 >= 0)
89+
8890#define maximum_signed_value_of_type (a ) \
8991 (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
9092
@@ -111,6 +113,28 @@ struct strbuf;
111113#define unsigned_mult_overflows (a , b ) \
112114 ((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
113115
116+ /*
117+ * Returns true if the multiplication of "a" and "b" will
118+ * overflow or underflow a signed type. The types of "a" and "b"
119+ * must match and must be signed. Note that this macro evaluates
120+ * both "a" and "b" twice!
121+ */
122+ #define signed_mult_overflows (a , b ) \
123+ (((a) > 0 && (b) > 0 && (a) > maximum_signed_value_of_type(a) / (b)) || \
124+ ((a) < 0 && (b) < 0 && (a) < maximum_signed_value_of_type(a) / (b)) || \
125+ ((a) > 0 && (b) < 0 && (b) < -(maximum_signed_value_of_type(a) / (a))) || \
126+ ((a) < 0 && (b) > 0 && (a) < -(maximum_signed_value_of_type(b) / (b))))
127+
128+ /*
129+ * Returns true if the multiplication of "a" and "b" will overflow,
130+ * regardless of whether the type is signed or unsigned. Note that
131+ * this macro evaluates both "a" and "b" twice!
132+ */
133+ #define mult_overflows (a , b ) \
134+ (is_unsigned_type(a) \
135+ ? unsigned_mult_overflows(a, b) \
136+ : signed_mult_overflows(a, b))
137+
114138/*
115139 * Returns true if the left shift of "a" by "shift" bits will
116140 * overflow. The type of "a" must be unsigned.
@@ -502,8 +526,19 @@ void set_die_is_recursing_routine(int (*routine)(void));
502526 *
503527 * See the skip_prefix macro below for an example of use.
504528 */
529+ /*
530+ * Coverity's EVALUATION_ORDER checker mistakes the dead ternary branch
531+ * for a live side effect: in skip_prefix(p, "x", &p) the expansion
532+ * contains *(out) = (in) in a 0-conditional, which Coverity reads as a
533+ * write to p while p is also read as the first argument. Simplify the
534+ * macro for Coverity to suppress 120+ false positives.
535+ */
536+ #ifdef __COVERITY__
537+ #define CONST_OUTPARAM (in , out ) (out)
538+ #else
505539#define CONST_OUTPARAM (in , out ) \
506540 ((const char **)(0 ? ((*(out) = (in)),(out)) : (out)))
541+ #endif
507542
508543/*
509544 * If the string "str" begins with the string found in "prefix", return true.
0 commit comments