8000 [3.6] bpo-29714: Fix a regression that bytes format may fail when con… · python/cpython@df6d7b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit df6d7b4

Browse files
authored
[3.6] bpo-29714: Fix a regression that bytes format may fail when containing zero bytes inside. (GH-504)
1 parent 4e1a065 commit df6d7b4

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
@@ -507,6 +507,11 @@ def test_mod(self):
507507
a = b % (b'seventy-nine', 79)
508508
self.assertEqual(a, b'seventy-nine / 100 = 79%')
509509
self.assertIs(type(a), self.type2test)
510+
# issue 29714
511+
b = self.type2test(b'hello,\x00%b!')
512+
b = b % b'world'
513+
self.assertEqual(b, b'hello,\x00world!')
514+
self.assertIs(type(b), self.type2test)
510515

511516
def test_imod(self):
512517
b = self.type2test(b'hello, %b!')
@@ -519,6 +524,11 @@ def test_imod(self):
519524
b %= (b'seventy-nine', 79)
520525
self.assertEqual(b, b'seventy-nine / 100 = 79%')
521526
self.assertIs(type(b), self.type2test)
527+
# issue 29714
528+
b = self.type2test(b'hello,\x00%b!')
529+
b %= b'world'
530+
self.assertEqual(b, b'hello,\x00world!')
531+
self.assertIs(type(b), self.type2test)
522532

523533
def test_rmod(self):
524534
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 EDFB Python 3.6.1 final?
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
Library
1417
-------
1518

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)
0