Commits

Martin von Löwis committed 4d24229

Issue #4228: Pack negative values the same way as 2.4
in struct's L format.

Comments (0)

Files changed (3)

Lib/test/test_struct.py

 import struct
 import array
 import warnings
+warnings.filterwarnings("ignore", "struct integer overflow masking is deprecated",
+                        DeprecationWarning)
 
 import sys
 ISBIGENDIAN = sys.byteorder == "big"
 
 test_1530559()
 
+## Issue 4228. Packing a negative unsigned long warns,
+# but then still should give a value with the
+# topmost bit set.
+
+def test_issue4228():
+    # Packing a long may yield either 32 or 64 bits
+    x = struct.pack('L', -1)[:4]
+    vereq(x, '\xff'*4)
+
+test_issue4228()
+
 ###########################################################################
 # Packing and unpacking to/from buffers.
 
 Extension Modules
 -----------------
 
+- Issue #4228: Pack negative values the same way as 2.4 in struct's L format.
+
 - Security Issue #2: imageop did not validate arguments correctly and could
   segfault as a result.
 

Modules/_struct.c

 		return -1;
 #if (SIZEOF_LONG > SIZEOF_INT)
 	if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
-		return _range_error(f, 0);
+		RANGE_ERROR(x, f, 0, -1);
 #endif
 	y = (int)x;
 	memcpy(p, (char *)&y, sizeof y);
 {
 	unsigned long x;
 	unsigned int y;
-	if (get_ulong(v, &x) < 0)
-		return _range_error(f, 1);
+	if (get_wrapped_ulong(v, &x) < 0)
+		return -1;
 	y = (unsigned int)x;
 #if (SIZEOF_LONG > SIZEOF_INT)
 	if (x > ((unsigned long)UINT_MAX))
-		return _range_error(f, 1);
+		RANGE_ERROR(y, f, 1, -1);
 #endif
 	memcpy(p, (char *)&y, sizeof y);
 	return 0;
 np_ulong(char *p, PyObject *v, const formatdef *f)
 {
 	unsigned long x;
-	if (get_ulong(v, &x) < 0)
-		return _range_error(f, 1);
+	if (get_wrapped_ulong(v, &x) < 0)
+		return -1;
 	memcpy(p, (char *)&x, sizeof x);
 	return 0;
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.