]> granicus.if.org Git - python/commitdiff
Issue #6540: Fixed crash for bytearray.translate() with invalid parameters.
authorGeorg Brandl <georg@python.org>
Wed, 22 Jul 2009 11:57:15 +0000 (11:57 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 22 Jul 2009 11:57:15 +0000 (11:57 +0000)
Lib/test/test_builtin.py
Misc/NEWS
Objects/bytearrayobject.c

index 80e1b814358dc4d3006d526419a00922964615fd..05556169b7dfecf50d771bd0007be0dd9ad55872 100644 (file)
@@ -1472,6 +1472,11 @@ class BuiltinTest(unittest.TestCase):
         self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
         self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
 
+    def test_bytearray_translate(self):
+        x = bytearray("abc")
+        self.assertRaises(ValueError, x.translate, "1", 1)
+        self.assertRaises(TypeError, x.translate, "1"*256, 1)
+
 class TestSorted(unittest.TestCase):
 
     def test_basic(self):
index 6b4dac506bc1af343f970cc42a8a8b85a6ce164f..f04956cba18870ff72bf7c91f6692a28b3267cd7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.7 alpha 1
 Core and Builtins
 -----------------
 
+- Issue #6540: Fixed crash for bytearray.translate() with invalid parameters.
+
 - Issue #1616979: Added the cp720 (Arabic DOS) encoding.
 
 - Issue #6070: On posix platforms import no longer copies the execute bit
index 5591847640d21ca0500a4540120bf15c3824b3d7..27b41bbf49d2942896258358847a8e7db5b4cf93 100644 (file)
@@ -1465,15 +1465,17 @@ bytearray_translate(PyByteArrayObject *self, PyObject *args)
         if (vtable.len != 256) {
             PyErr_SetString(PyExc_ValueError,
                             "translation table must be 256 characters long");
-            goto done;
+            PyBuffer_Release(&vtable);
+            return NULL;
         }
         table = (const char*)vtable.buf;
     }
 
     if (delobj != NULL) {
         if (_getbuffer(delobj, &vdel) < 0) {
-            delobj = NULL;  /* don't try to release vdel buffer on exit */
-            goto done;
+            if (tableobj != NULL)
+                PyBuffer_Release(&vtable);
+            return NULL;
         }
     }
     else {