[PATCH 6/9] internal: Add helper macro for checking multiply and add overflows

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 src/internal.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/internal.h b/src/internal.h
index 01860efad9..20aa9b1d41 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -43,6 +43,17 @@

 #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.
-- 
2.46.0




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux