]> Git Repo - linux.git/blobdiff - include/linux/overflow.h
Merge tag 'trace-v6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[linux.git] / include / linux / overflow.h
index 1d3be1a2204c324df62350505cf160ab6d0369b9..0e33b5cbdb9f66d558f1af65039f629fdcf417e7 100644 (file)
@@ -128,6 +128,53 @@ static inline bool __must_check __must_check_overflow(bool overflow)
        (*_d >> _to_shift) != _a);                                      \
 }))
 
+#define __overflows_type_constexpr(x, T) (                     \
+       is_unsigned_type(typeof(x)) ?                           \
+               (x) > type_max(typeof(T)) :                     \
+       is_unsigned_type(typeof(T)) ?                           \
+               (x) < 0 || (x) > type_max(typeof(T)) :          \
+       (x) < type_min(typeof(T)) || (x) > type_max(typeof(T)))
+
+#define __overflows_type(x, T)         ({      \
+       typeof(T) v = 0;                        \
+       check_add_overflow((x), v, &v);         \
+})
+
+/**
+ * overflows_type - helper for checking the overflows between value, variables,
+ *                 or data type
+ *
+ * @n: source constant value or variable to be checked
+ * @T: destination variable or data type proposed to store @x
+ *
+ * Compares the @x expression for whether or not it can safely fit in
+ * the storage of the type in @T. @x and @T can have different types.
+ * If @x is a constant expression, this will also resolve to a constant
+ * expression.
+ *
+ * Returns: true if overflow can occur, false otherwise.
+ */
+#define overflows_type(n, T)                                   \
+       __builtin_choose_expr(__is_constexpr(n),                \
+                             __overflows_type_constexpr(n, T), \
+                             __overflows_type(n, T))
+
+/**
+ * castable_to_type - like __same_type(), but also allows for casted literals
+ *
+ * @n: variable or constant value
+ * @T: variable or data type
+ *
+ * Unlike the __same_type() macro, this allows a constant value as the
+ * first argument. If this value would not overflow into an assignment
+ * of the second argument's type, it returns true. Otherwise, this falls
+ * back to __same_type().
+ */
+#define castable_to_type(n, T)                                         \
+       __builtin_choose_expr(__is_constexpr(n),                        \
+                             !__overflows_type_constexpr(n, T),        \
+                             __same_type(n, T))
+
 /**
  * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX
  * @factor1: first factor
This page took 0.036446 seconds and 4 git commands to generate.