]> xenbits.xensource.com Git - libvirt.git/commitdiff
internal: Add helper macro for checking multiply and add overflows
authorPeter Krempa <pkrempa@redhat.com>
Mon, 9 Sep 2024 14:46:07 +0000 (16:46 +0200)
committerPavel Hrdina <phrdina@redhat.com>
Tue, 10 Sep 2024 12:26:28 +0000 (14:26 +0200)
The macro does the two checks together so that it's obvious what we're
checking as doing it in place is really unpleasant.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/internal.h

index 01860efad9e968d108155cb1ebd3035a74b28933..20aa9b1d418982fdffddd1da9864c312d33e8c5e 100644 (file)
 
 #define VIR_INT_MULTIPLY_OVERFLOW(a,b) (G_UNLIKELY ((b) > 0 && (a) > G_MAXINT / (b)))
 
+/**
+ * VIR_MULTIPLY_ADD_IS_OVERFLOW:
+ * @limit: maximum value of data type
+ * @value: current value
+ * @multiply: number @value is going to be multiplied by
+ * @add: number that will be added to @value after multiplication
+ */
+#define VIR_MULTIPLY_ADD_IS_OVERFLOW(limit, value, multiply, add) \
+  (G_UNLIKELY(((multiply) > 0 && (value) > (limit) / (multiply)) || \
+              ((limit) - ((value) * (multiply)) < (add))))
+
 /* The library itself is allowed to use deprecated functions /
  * variables, so effectively undefine the deprecated attribute
  * which would otherwise be defined in libvirt.h.