From 7fa4f17cc183e04b10684b28219cf15780910206 Mon Sep 17 00:00:00 2001
From: Mingli Yu <mingli.yu@windriver.com>
Date: Mon, 30 Jun 2025 16:11:16 +0800
Subject: [PATCH] timeout.py: use qq format when time_t is 64bit on 32bit
 platform

Fixes:
   # python3
  Python 3.13.2 (main, Feb  4 2025, 14:51:09) [GCC 14.2.0] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import socket
  >>> import struct
  >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  >>> seconds = 5
  >>> microseconds = 0
  >>> timeval_packed = struct.pack('ll', seconds, microseconds)
  >>> s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeval_packed)
Traceback (most recent call last):
  File "<python-input-6>", line 1, in <module>
    s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeval_packed)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument

Upstream-Status: Submitted [https://lists.sr.ht/~mcepl/m2crypto/patches/60463]

Signed-off-by: Mingli Yu <mingli.yu@windriver.com>
---
 src/M2Crypto/SSL/timeout.py | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/M2Crypto/SSL/timeout.py b/src/M2Crypto/SSL/timeout.py
index 298a9ca..0b38329 100644
--- a/src/M2Crypto/SSL/timeout.py
+++ b/src/M2Crypto/SSL/timeout.py
@@ -15,7 +15,7 @@ __all__ = [
 import sys
 import struct
 
-from M2Crypto import m2
+from M2Crypto import m2, util
 
 DEFAULT_TIMEOUT: int = 600
 
@@ -40,7 +40,10 @@ class timeout(object):
             if m2.time_t_bits() == 32:
                 binstr = struct.pack('ii', self.sec, self.microsec)
             else:
-                binstr = struct.pack('ll', self.sec, self.microsec)
+                if util.is_32bit():
+                    binstr = struct.pack('qq', self.sec, self.microsec)
+                else:
+                    binstr = struct.pack('ll', self.sec, self.microsec)
         return binstr
 
 
@@ -52,7 +55,10 @@ def struct_to_timeout(binstr: bytes) -> timeout:
         sec = int(millisec / 1000)
         microsec = (millisec % 1000) * 1000
     else:
-        (sec, microsec) = struct.unpack('ll', binstr)
+        if sys.platform == 'linux' and util.is_32bit() and m2.time_t_bits() == 64:
+             (sec, microsec) = struct.unpack('qq', binstr)
+        else:
+            (sec, microsec) = struct.unpack('ll', binstr)
     return timeout(sec, microsec)
 
 
@@ -60,4 +66,8 @@ def struct_size() -> int:
     if sys.platform == 'win32':
         return struct.calcsize('l')
     else:
-        return struct.calcsize('ll')
+        if sys.platform == 'linux' and util.is_32bit() and m2.time_t_bits() == 64:
+            return struct.calcsize('qq')
+        else:
+            return struct.calcsize('ll')
+
-- 
2.34.1

