Blob Blame History Raw

# HG changeset patch
# User Benjamin Peterson <benjamin@python.org>
# Date 1403579547 25200
# Node ID 8d963c7db507be561e26bbbb852e3a2be3327c3f
# Parent  8e0b7393e921fb5e05c40265f9272dec90512ef6
avoid overflow with large buffer sizes and/or offsets (closes #21831)

diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py
--- a/Lib/test/test_buffer.py
+++ b/Lib/test/test_buffer.py
@@ -4,6 +4,7 @@ For now, tests just new or changed funct
 
 """
 
+import sys
 import unittest
 from test import test_support
 
@@ -29,6 +30,11 @@ class BufferTests(unittest.TestCase):
         m = memoryview(b) # Should not raise an exception
         self.assertEqual(m.tobytes(), s)
 
+    def test_large_buffer_size_and_offset(self):
+        data = bytearray('hola mundo')
+        buf = buffer(data, sys.maxsize, sys.maxsize)
+        self.assertEqual(buf[:4096], "")
+
 
 def test_main():
     with test_support.check_py3k_warnings(("buffer.. not supported",
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c
--- a/Objects/bufferobject.c
+++ b/Objects/bufferobject.c
@@ -88,7 +88,7 @@ get_buf(PyBufferObject *self, void **ptr
             *size = count;
         else
             *size = self->b_size;
-        if (offset + *size > count)
+        if (*size > count - offset)
             *size = count - offset;
     }
     return 1;
@@ -875,4 +875,4 @@ PyTypeObject PyBuffer_Type = {
     0,                                          /* tp_init */
     0,                                          /* tp_alloc */
     buffer_new,                                 /* tp_new */
-};
\ No newline at end of file
+};