From 4e18b842b3dae5486adb2c6cc11bdfdd53ee8646 Mon Sep 17 00:00:00 2001
From: Tobias Stoeckmann <tobias@stoeckmann.org>
Date: Wed, 14 Jan 2026 20:58:50 +0100
Subject: [PATCH] util: Add s390 31 bit mode support

Even though size_t is of the same size as a uint32_t, the s390
architecture is a bit picky about its 31 bit mode with size_t.

Use custom code to fix this issue in a rather architecture
independent but unfortunately slower way for s390 31 bit mode.

Reference: https://github.com/kmod-project/kmod/issues/402
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
---
 shared/util.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/shared/util.h b/shared/util.h
index bf3f1009..b785f5b1 100644
--- a/shared/util.h
+++ b/shared/util.h
@@ -123,7 +123,15 @@ static inline bool uaddsz_overflow(size_t a, size_t b, size_t *res)
 #if __SIZEOF_SIZE_T__ == 8
 	return uadd64_overflow(a, b, res);
 #elif __SIZEOF_SIZE_T__ == 4
+#ifdef __s390__
+	if (b < SIZE_MAX - a) {
+		*res = a + b;
+		return true;
+	}
+	return false;
+#else
 	return uadd32_overflow(a, b, res);
+#endif
 #else
 #error "Unknown sizeof(size_t)"
 #endif
@@ -167,7 +175,15 @@ static inline bool umulsz_overflow(size_t a, size_t b, size_t *res)
 #if __SIZEOF_SIZE_T__ == 8
 	return umul64_overflow(a, b, res);
 #elif __SIZEOF_SIZE_T__ == 4
+#ifdef __s390__
+	if (a == 0 || b <= SIZE_MAX / a) {
+		*res = a * b;
+		return true;
+	}
+	return false;
+#else
 	return umul32_overflow(a, b, res);
+#endif
 #else
 #error "Unknown sizeof(size_t)"
 #endif
