[PATCH i-g-t v3] lib/igt_core: fix overflow in micro/nano-seconds calculations
Zbigniew Kempczyński
zbigniew.kempczynski at intel.com
Wed Dec 18 06:07:30 UTC 2024
Multiplication without using casting to long (long) for at least
one argument in expression may cause overflow especially when we're
using this for calculating timeouts in nanoseconds precision.
Ensure we're picking valid ul/ull suffix for base definitions for
all dependents.
Reported-by: Ilia Levi <ilia.levi at intel.com>
Closes: https://gitlab.freedesktop.org/drm/igt-gpu-tools/-/issues/163
Cc: Lucas De Marchi <lucas.demarchi at intel.com>
Cc: Ilia Levi <ilia.levi at intel.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi at intel.com>
Reviewed-by: Kamil Konieczny <kamil.konieczny at linux.intel.com>
Link: https://lore.kernel.org/r/20241217105807.94489-1-zbigniew.kempczynski@intel.com
Signed-off-by: Zbigniew Kempczyński <zbigniew.kempczynski at intel.com>
---
v2: Define __WORDSIZE if not defined (Lucas)
v3: Eh, forgot to reprase the title (Kamil)
---
lib/igt_core.h | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/lib/igt_core.h b/lib/igt_core.h
index 90f57402f0..f0ba1a3818 100644
--- a/lib/igt_core.h
+++ b/lib/igt_core.h
@@ -1517,11 +1517,21 @@ void igt_kmsg(const char *format, ...);
#define le32_to_cpu(x) (x)
#endif
-#define MSEC_PER_SEC (1000u)
+#ifndef __WORDSIZE
+#define __WORDSIZE (__SIZEOF_LONG__ * 8)
+#endif
+
+#if __WORDSIZE == 64
+#define MSEC_PER_SEC (1000ul)
+#define USEC_PER_MSEC (1000ul)
+#else
+#define MSEC_PER_SEC (1000ull)
+#define USEC_PER_MSEC (1000ull)
+#endif
+
#define USEC_PER_SEC (1000u * MSEC_PER_SEC)
#define USEC_PER_DECISEC (100u * MSEC_PER_SEC)
#define NSEC_PER_SEC (1000u * USEC_PER_SEC)
-#define USEC_PER_MSEC (1000u)
#define NSEC_PER_MSEC (1000u * USEC_PER_MSEC)
#define for_if(expr__) if (!(expr__)) {} else
--
2.34.1
More information about the igt-dev
mailing list