[pypy-svn] r78152 - in pypy/branch/fast-forward/pypy/module/array: . test

afa at codespeak.net afa at codespeak.net
Thu Oct 21 08:45:30 CEST 2010


Author: afa
Date: Thu Oct 21 08:45:29 2010
New Revision: 78152

Modified:
   pypy/branch/fast-forward/pypy/module/array/interp_array.py
   pypy/branch/fast-forward/pypy/module/array/test/test_array.py
Log:
Add overflow checks in array module


Modified: pypy/branch/fast-forward/pypy/module/array/interp_array.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/array/interp_array.py	(original)
+++ pypy/branch/fast-forward/pypy/module/array/interp_array.py	Thu Oct 21 08:45:29 2010
@@ -6,6 +6,7 @@
 from pypy.rlib.jit import dont_look_inside
 from pypy.rlib import rgc
 from pypy.rlib.unroll import unrolling_iterable
+from pypy.rlib.rarithmetic import ovfcheck
 from pypy.rlib.rstruct.runpack import runpack
 from pypy.interpreter.argument import Arguments, Signature
 from pypy.interpreter.baseobjspace import ObjSpace, W_Root, Wrappable
@@ -488,7 +489,11 @@
             raise
         a = mytype.w_class(space)
         repeat = max(repeat, 0)
-        a.setlen(self.len * repeat)
+        try:
+            newlen = ovfcheck(self.len * repeat)
+        except OverflowError:
+            raise MemoryError
+        a.setlen(newlen)
         for r in range(repeat):
             for i in range(self.len):
                 a.buffer[r * self.len + i] = self.buffer[i]
@@ -506,7 +511,11 @@
             raise
         oldlen = self.len
         repeat = max(repeat, 0)
-        self.setlen(self.len * repeat)
+        try:
+            newlen = ovfcheck(self.len * repeat)
+        except OverflowError:
+            raise MemoryError
+        self.setlen(newlen)
         for r in range(1, repeat):
             for i in range(oldlen):
                 self.buffer[r * oldlen + i] = self.buffer[i]
@@ -544,7 +553,10 @@
             raise OperationError(space.w_TypeError, space.wrap(msg))
         n = space.int_w(w_n)
 
-        size = self.itemsize * n
+        try:
+            size = ovfcheck(self.itemsize * n)
+        except OverflowError:
+            raise MemoryError
         w_item = space.call_method(w_f, 'read', space.wrap(size))
         item = space.str_w(w_item)
         if len(item) < size:

Modified: pypy/branch/fast-forward/pypy/module/array/test/test_array.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/array/test/test_array.py	(original)
+++ pypy/branch/fast-forward/pypy/module/array/test/test_array.py	Thu Oct 21 08:45:29 2010
@@ -1,4 +1,5 @@
 from pypy.conftest import gettestobjspace
+import sys
 import py
 import py.test
 
@@ -590,6 +591,9 @@
         a += self.array('i', (7,))
         assert repr(a) == "array('i', [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 7])"
 
+        raises(MemoryError, "a * self.maxint")
+        raises(MemoryError, "a *= self.maxint")
+
         raises(TypeError, "a = self.array('i') + 2")
         raises(TypeError, "self.array('i') + self.array('b')")
         a = self.array('i')
@@ -812,7 +816,8 @@
         cls.array = array.array
         import struct
         cls.struct = struct
-        cls.tempfile = str(py.test.ensuretemp('array').join('tmpfile'))        
+        cls.tempfile = str(py.test.ensuretemp('array').join('tmpfile'))
+        cls.maxint = sys.maxint
 
 class AppTestArray(BaseArrayTests):
     def setup_class(cls):
@@ -831,6 +836,7 @@
         """)
         cls.w_tempfile = cls.space.wrap(
             str(py.test.ensuretemp('array').join('tmpfile')))
+        cls.w_maxint = cls.space.wrap(sys.maxint)
 
 
 



More information about the Pypy-commit mailing list