Skip to content

Commit b76ad51

Browse files
authored
bpo-29714: Fix a regression that bytes format may fail when containing zero bytes inside. (GH-499)
1 parent 86aa269 commit b76ad51

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Lib/test/test_bytes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,11 @@ def test_mod(self):
515515
a = b % (b'seventy-nine', 79)
516516
self.assertEqual(a, b'seventy-nine / 100 = 79%')
517517
self.assertIs(type(a), self.type2test)
518+
# issue 29714
519+
b = self.type2test(b'hello,\x00%b!')
520+
b = b % b'world'
521+
self.assertEqual(b, b'hello,\x00world!')
522+
self.assertIs(type(b), self.type2test)
518523

519524
def test_imod(self):
520525
b = self.type2test(b'hello, %b!')
@@ -527,6 +532,11 @@ def test_imod(self):
527532
b %= (b'seventy-nine', 79)
528533
self.assertEqual(b, b'seventy-nine / 100 = 79%')
529534
self.assertIs(type(b), self.type2test)
535+
# issue 29714
536+
b = self.type2test(b'hello,\x00%b!')
537+
b %= b'world'
538+
self.assertEqual(b, b'hello,\x00world!')
539+
self.assertIs(type(b), self.type2test)
530540

531541
def test_rmod(self):
532542
with self.assertRaises(TypeError):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-29714: Fix a regression that bytes format may fail when containing zero
14+
bytes inside.
15+
1316
- bpo-29695: Using "x" as a keyword argument in int(), bool() and float() and
1417
using "sequence" as a keyword argument in list() and tuple() are deprecated.
1518
Specify the value as a positional argument instead.

Objects/bytesobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,11 +619,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
619619
Py_ssize_t len;
620620
char *pos;
621621

622-
pos = strchr(fmt + 1, '%');
622+
pos = (char *)memchr(fmt + 1, '%', fmtcnt);
623623
if (pos != NULL)
624624
len = pos - fmt;
625625
else
626-
len = format_len - (fmt - format);
626+
len = fmtcnt + 1;
627627
assert(len != 0);
628628

629629
memcpy(res, fmt, len);

0 commit comments

Comments
 (0)