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

index 671c35efabd6ca3f360d19601949e2d210139da4..8a3b8055ff7d48e7ab55b6ddc7da18de77dc1a78 100644 (file)
@@ -515,6 +515,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!')
@@ -527,6 +532,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 d542cf1032e5c1bb1775abe6ebf51acf8f8effa6..81b02fdd4a48b3f82aa3056fb1eb0ba59de98df8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
 Core and Builtins
 -----------------
 
+- bpo-29714: Fix a regression that bytes format may fail when containing zero
+  bytes inside.
+
 - bpo-29695: Using "x" as a keyword argument in int(), bool() and float() and
   using "sequence" as a keyword argument in list() and tuple() are deprecated.
   Specify the value as a positional argument instead.
index a30ac0c37970e12ff5d45c603285d9fb5aac9a22..f0ddb95de57833af334ae0843435105777e95ba3 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);