From 80a6e7eecc2e5d284f5aef6573042fce4d23975d Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Mon, 6 Mar 2017 15:52:29 +0800 Subject: [PATCH 1/3] fix bytes format regression when containing zero bytes inside --- Lib/test/test_bytes.py | 10 ++++++++++ Objects/bytesobject.c | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 671c35efabd6ca..8e111403f6da86 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -515,6 +515,11 @@ def test_mod(self): 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 @@ def test_imod(self): 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 % b'world' + self.assertEqual(b, b'hello,\x00world!') + self.assertIs(type(b), self.type2test) def test_rmod(self): with self.assertRaises(TypeError): diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index a30ac0c37970e1..f0ddb95de57833 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -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); From 75adc60044a77774f68db9a759bcca68647f9bfc Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Mon, 6 Mar 2017 15:53:57 +0800 Subject: [PATCH 2/3] add news entry --- Misc/NEWS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Misc/NEWS b/Misc/NEWS index d542cf1032e5c1..81b02fdd4a48b3 100644 --- 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. From a14575703aa085068a2e69082ee2942db89922e9 Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Mon, 6 Mar 2017 16:03:57 +0800 Subject: [PATCH 3/3] fix an error --- Lib/test/test_bytes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 8e111403f6da86..8a3b8055ff7d48 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -534,7 +534,7 @@ def test_imod(self): self.assertIs(type(b), self.type2test) # issue 29714 b = self.type2test(b'hello,\x00%b!') - b = b % b'world' + b %= b'world' self.assertEqual(b, b'hello,\x00world!') self.assertIs(type(b), self.type2test)