]> granicus.if.org Git - python/commitdiff
[3.6] bpo-29714: Fix a regression that bytes format may fail when containing zero...
authorXiang Zhang <angwerzx@126.com>
Mon, 6 Mar 2017 10:17:10 +0000 (18:17 +0800)
committerGitHub <noreply@github.com>
Mon, 6 Mar 2017 10:17:10 +0000 (18:17 +0800)
Lib/test/test_bytes.py
Misc/NEWS
Objects/bytesobject.c

index a103a7d39cae2416109f8d6b039ebded7bdc1c27..cd82fa64570cb5ed4a73fd61415734d61aff1295 100644 (file)
@@ -507,6 +507,11 @@ class BaseBytesTest:
         a = b % (b'seventy-nine', 79)
         self.assertEqual(a, b'seventy-nine / 100 = 79%')
         self.assertIs(type(a), self.type2test)
+        # issue 29714
+        b = self.type2test(b'hello,\x00%b!')
+        b = b % b'world'
+        self.assertEqual(b, b'hello,\x00world!')
+        self.assertIs(type(b), self.type2test)
 
     def test_imod(self):
         b = self.type2test(b'hello, %b!')
@@ -519,6 +524,11 @@ class BaseBytesTest:
         b %= (b'seventy-nine', 79)
         self.assertEqual(b, b'seventy-nine / 100 = 79%')
         self.assertIs(type(b), self.type2test)
+        # issue 29714
+        b = self.type2test(b'hello,\x00%b!')
+        b %= b'world'
+        self.assertEqual(b, b'hello,\x00world!')
+        self.assertIs(type(b), self.type2test)
 
     def test_rmod(self):
         with self.assertRaises(TypeError):
index a12229857945c830499fcf8379e2ecdd8670e03f..1fb78365bb89be93149a4dabd1c30f00d4de43f2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.6.1 final?
 Core and Builtins
 -----------------
 
+- bpo-29714: Fix a regression that bytes format may fail when containing zero
+  bytes inside.
+
 Library
 -------
 
index 5d4844096008528074b17aa06507ecb2e1734387..801711f7e6ccaae8a0620617a2fefbea0a118d7f 100644 (file)
@@ -619,11 +619,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
             Py_ssize_t len;
             char *pos;
 
-            pos = strchr(fmt + 1, '%');
+            pos = (char *)memchr(fmt + 1, '%', fmtcnt);
             if (pos != NULL)
                 len = pos - fmt;
             else
-                len = format_len - (fmt - format);
+                len = fmtcnt + 1;
             assert(len != 0);
 
             memcpy(res, fmt, len);