Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e84b740

Browse files
committed
Obscure marshal fixes:
When reading a short, sign-extend on platforms where shorts are bigger than 16 bits. When reading a long, repair the unportable sign extension that was being done for 64-bit machines (it assumed that signed right shift sign-extends).
1 parent a3c6a8a commit e84b740

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

Python/marshal.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ r_short(RFILE *p)
307307
register short x;
308308
x = r_byte(p);
309309
x |= r_byte(p) << 8;
310-
/* XXX If your short is > 16 bits, add sign-extension here!!! */
310+
/* Sign-extension, in case short greater than 16 bits */
311+
x |= -(x & 0x8000);
311312
return x;
312313
}
313314

@@ -330,8 +331,7 @@ r_long(RFILE *p)
330331
}
331332
#if SIZEOF_LONG > 4
332333
/* Sign extension for 64-bit machines */
333-
x <<= (8*sizeof(long) - 32);
334-
x >>= (8*sizeof(long) - 32);
334+
x |= -(x & 0x80000000L);
335335
#endif
336336
return x;
337337
}
@@ -342,7 +342,7 @@ r_long64(RFILE *p)
342342
register long x;
343343
x = r_long(p);
344344
#if SIZEOF_LONG > 4
345-
x = (x & 0xFFFFFFFF) | (r_long(p) << 32);
345+
x = (x & 0xFFFFFFFFL) | (r_long(p) << 32);
346346
#else
347347
if (r_long(p) != 0) {
348348
PyObject *f = PySys_GetObject("stderr");

0 commit comments

Comments
 (0)