File CVE-2017-12852-pad-empty-array-infinite-loop.patch of Package python-numpy.26461

Index: numpy-1.8.0/numpy/lib/arraypad.py
===================================================================
--- numpy-1.8.0.orig/numpy/lib/arraypad.py
+++ numpy-1.8.0/numpy/lib/arraypad.py
@@ -1396,6 +1396,14 @@ def pad(array, pad_width, mode=None, **k
 
     elif mode == 'reflect':
         for axis, (pad_before, pad_after) in enumerate(pad_width):
+            if narray.shape[axis] == 0:
+                # Axes with non-zero padding cannot be empty.
+                if pad_before > 0 or pad_after > 0:
+                    raise ValueError("There aren't any elements to reflect"
+                                     " in axis {} of `array`".format(axis))
+                # Skip zero padding on empty axes.
+                continue
+
             # Recursive padding along any axis where `pad_amt` is too large
             # for indexing tricks. We can only safely pad the original axis
             # length, to keep the period of the reflections consistent.
Index: numpy-1.8.0/numpy/lib/tests/test_arraypad.py
===================================================================
--- numpy-1.8.0.orig/numpy/lib/tests/test_arraypad.py
+++ numpy-1.8.0/numpy/lib/tests/test_arraypad.py
@@ -516,6 +516,12 @@ class ValueError1(TestCase):
         assert_raises(ValueError, pad, arr, ((-2, 3), (3, 2)),
                 **kwargs)
 
+    def test_check_empty_array(self):
+        assert_raises(ValueError, pad, [], 4, mode='reflect')
+        assert_raises(ValueError, pad, np.ndarray(0), 4, mode='reflect')
+        assert_raises(ValueError, pad, np.zeros((0, 3)), ((1,), (0,)),
+                      mode='reflect')
+
 
 class ValueError2(TestCase):
     def test_check_simple(self):
@@ -534,6 +540,11 @@ class ValueError3(TestCase):
         assert_raises(ValueError, pad, arr, ((-2, 3), (3, 2)),
                 **kwargs)
 
+    def test_check_padding_an_empty_array(self):
+        a = pad(np.zeros((0, 3)), ((0,), (1,)), mode='reflect')
+        b = np.zeros((0, 5))
+        assert_array_equal(a, b)
+
 
 if __name__ == "__main__":
     run_module_suite()
openSUSE Build Service is sponsored by