diff -r ba1d7447bd1b Lib/pickle.py --- a/Lib/pickle.py Sat Nov 17 09:33:14 2012 -0500 +++ b/Lib/pickle.py Sat Nov 17 17:47:47 2012 +0200 @@ -263,7 +263,7 @@ if i < 256: return BINPUT + bytes([i]) else: - return LONG_BINPUT + pack(" maxsize: + raise UnpicklingError("BINBYTES exceeds system's maximum size of %d bytes" % maxsize); self.append(self.read(len)) dispatch[BINBYTES[0]] = load_binbytes @@ -974,8 +981,10 @@ self.append(str(self.readline()[:-1], 'raw-unicode-escape')) dispatch[UNICODE[0]] = load_unicode - def load_binunicode(self): - len = mloads(b'i' + self.read(4)) + def load_binunicode(self, unpack=struct.unpack, maxsize=sys.maxsize): + len, = unpack(' maxsize: + raise UnpicklingError("BINUNICODE exceeds system's maximum size of %d bytes" % maxsize); self.append(str(self.read(len), 'utf-8', 'surrogatepass')) dispatch[BINUNICODE[0]] = load_binunicode @@ -1106,6 +1115,9 @@ return key = _inverted_registry.get(code) if not key: + if code <= 0: # note that 0 is forbidden + # Corrupt or hostile pickle. + raise UnpicklingError("EXT specifies code <= 0"); raise ValueError("unregistered extension code %d" % code) obj = self.find_class(*key) _extension_cache[code] = obj @@ -1159,8 +1171,8 @@ self.append(self.memo[i]) dispatch[BINGET[0]] = load_binget - def load_long_binget(self): - i = mloads(b'i' + self.read(4)) + def load_long_binget(self, unpack=struct.unpack): + i, = unpack(' maxsize: raise ValueError("negative LONG_BINPUT argument") self.memo[i] = self.stack[-1] dispatch[LONG_BINPUT[0]] = load_long_binput diff -r ba1d7447bd1b Modules/_pickle.c --- a/Modules/_pickle.c Sat Nov 17 09:33:14 2012 -0500 +++ b/Modules/_pickle.c Sat Nov 17 17:47:47 2012 +0200 @@ -1589,7 +1589,7 @@ * byte at the start, and cut it back later if possible. */ nbytes = (nbits >> 3) + 1; - if (nbytes > INT_MAX) { + if (nbytes > 0x7fffffffL) { PyErr_SetString(PyExc_OverflowError, "long too large to pickle"); goto error;