Repository navigation
Closed
Labels
Description
| BPO | 8271 |
|---|---|
| Nosy | @malemburg, @abalkin, @pitrou, @vstinner, @ezio-melotti, @serhiy-storchaka |
| Files |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/ezio-melotti'
closed_at = <Date 2012-11-04.21:37:03.868>
created_at = <Date 2010-03-31.02:28:10.626>
labels = ['type-bug', 'expert-unicode']
title = "str.decode('utf8', 'replace') -- conformance with Unicode 5.2.0"
updated_at = <Date 2014-03-31.23:38:33.951>
user = 'https://bugs.python.org/sjmachin'bugs.python.org fields:
activity = <Date 2014-03-31.23:38:33.951>
actor = 'jmehnle'
assignee = 'ezio.melotti'
closed = True
closed_date = <Date 2012-11-04.21:37:03.868>
closer = 'ezio.melotti'
components = ['Unicode']
creation = <Date 2010-03-31.02:28:10.626>
creator = 'sjmachin'
dependencies = []
files = ['16720', '16741', '16754', '16794', '17552', '21719', '26118']
hgrepos = []
issue_num = 8271
keywords = ['patch', 'needs review']
message_count = 66.0
messages = ['101972', '102013', '102024', '102061', '102062', '102063', '102064', '102065', '102066', '102068', '102076', '102077', '102085', '102089', '102090', '102093', '102094', '102095', '102098', '102099', '102101', '102209', '102239', '102265', '102320', '102516', '102522', '102523', '107074', '107163', '109015', '109070', '109155', '109159', '109170', '129495', '129647', '129685', '134046', '142132', '160980', '160981', '160989', '160990', '160991', '160993', '160998', '161000', '161001', '161002', '161004', '161005', '161622', '161627', '161650', '161655', '163587', '163588', '163591', '163674', '163677', '174828', '174831', '174832', '174834', '174839']
nosy_count = 12.0
nosy_names = ['lemburg', 'sjmachin', 'belopolsky', 'pitrou', 'vstinner', 'ezio.melotti', 'Ringding', 'dangra', 'python-dev', 'jmehnle', 'spatz123', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue8271'
versions = ['Python 3.3', 'Python 3.4']
Activity
sjmachin commentedon Mar 31, 2010
Unicode 5.2.0 chapter 3 (Conformance) has a new section (headed "Constraints on Conversion Processes) after requirement D93. Recent Pythons e.g. 3.1.2 don't comply. Using the Unicode example:
>>> print(ascii(b"\xc2\x41\x42".decode('utf8', 'replace'))) '\ufffdB' # should produce u'\ufffdAB'Resynchronisation currently starts at a position derived by considering the length implied by the start byte:
>>> print(ascii(b"\xf1ABCD".decode('utf8', 'replace'))) '\ufffdD' # should produce u'\ufffdABCD'; resync should start from the *failing* byte.Notes: This applies to the 'ignore' option as well as the 'replace' option. The Unicode discussion mentions "security exploits".
dangra commentedon Mar 31, 2010
Some background for this report at http://stackoverflow.com/questions/2547262/why-is-python-decode-replacing-more-than-the-invalid-bytes-from-an-encoded-string/2548480
malemburg commentedon Mar 31, 2010
I guess the term "failing" byte somewhat underdefined.
Page 95 of the standard PDF (http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf) suggests to "Replace each maximal subpart of an ill-formed subsequence by a single U+FFFD".
Fortunately, they explain what they are after: if a subsequent byte in the sequence does not have the high bit set, it's not to be considered part of the UTF-8 sequence of the code point.
Implementing that should be fairly straight-forward by adjusting the endinpos variable accordingly.
Any takers ?
sjmachin commentedon Apr 1, 2010
@lemburg: "failing byte" seems rather obvious: first byte that you meet that is not valid in the current state. I don't understand your explanation, especially "does not have the high bit set". I think you mean "is a valid starter byte". See example 3 below.
Example 1: F1 80 41 42 43. F1 implies a 4-byte character. 80 is OK. 41 is not in 80-BF. It is the "failing byte"; high bit not set. Required action is to emit FFFD then resync on the 41, causing 0041 0042 0043 to be emitted. Total output: FFFD 0041 0042 0043. Current code emits FFFD 0043.
Example 2: F1 80 FF 42 43. F1 implies a 4-byte character. 80 is OK. FF is not in 80-BF. It is the "failing byte". Required action is to emit FFFD then resync on the FF. FF is not a valid starter byte, so emit FFFD, and resync on the 42, causing 0042 0043 to be emitted. Total output: FFFD FFFD 0042 0043. Current code emits FFFD 0043.
Example 3: F1 80 C2 81 43. F1 implies a 4-byte character. 80 is OK. C2 is not in 80-BF. It is the "failing byte". Required action is to emit FFFD then resync on the C2. C2 and 81 have the high bit set, but C2 is a valid starter byte, and remaining bytes are OK, causing 0081 0043 to be emitted. Total output: FFFD 0081 0043. Current code emits FFFD 0043.
ezio-melotti commentedon Apr 1, 2010
Having the 'high bit set' means that the first bit is set to 1.
All the continuation bytes (i.e. the 2nd, 3rd or 4th byte in a sequence) have the first two bits set to 1 and 0 respectively, so if the first bit is not set to 1 then the byte shouldn't be considered part of the sequence.
I'm trying to work on a patch.
sjmachin commentedon Apr 1, 2010
@ezio.melotti: Your second sentence is true, but it is not the whole truth. Bytes in the range C0-FF (whose high bit *is* set) ALSO shouldn't be considered part of the sequence because they (like 00-7F) are invalid as continuation bytes; they are either starter bytes (C2-F4) or invalid for any purpose (C0-C2 and F5-FF). Further, some bytes in the range 80-BF are NOT always valid as the first continuation byte, it depends on what starter byte they follow.
The simple way of summarising the above is to say that a byte that is not a valid continuation byte in the current state ("failing byte") is not a part of the current (now known to be invalid) sequence, and the decoder must try again ("resync") with the failing byte.
Do you agree with my example 3?
ezio-melotti commentedon Apr 1, 2010
Yes, right now I'm considering valid all the bytes that start with '10...'. C2 starts with '11...' so it's a "failing byte".
sjmachin commentedon Apr 1, 2010
#ezio.melotti: """I'm considering valid all the bytes that start with '10...'"""
Sorry, WRONG. Read what I wrote: """Further, some bytes in the range 80-BF are NOT always valid as the first continuation byte, it depends on what starter byte they follow."""
Consider these sequences: (1) E0 80 80 (2) E0 9F 80. Both are invalid sequences (over-long). Specifically the first continuation byte may not be in 80-9F. Those bytes start with '10...' but they are invalid after an E0 starter byte.
Please read "Table 3-7. Well-Formed UTF-8 Byte Sequences" and surrounding text in Unicode 5.2.0 chapter 3 (bearing in mind that CPython (for good reasons) doesn't implement the surrogates restriction, so that the special case for starter byte ED is not used in CPython). Note the other 3 special cases for the first continuation byte.
ezio-melotti commentedon Apr 1, 2010
That's why I'm writing tests that cover all the cases, including overlong sequences. If the test will fail I'll change the patch :)
malemburg commentedon Apr 1, 2010
John Machin wrote:
I just had a quick look at the code and saw that it's testing for the high
bit on the subsequent bytes.
Looking closer, you're right and the situation is a bit more complex,
but the solution still looks simple: only the endinpos
has to be adjusted more carefully depending on what the various
checks find.
That said, I find the Unicode consortium solution a bit awkward.
In UTF-8 the first byte in a multi-byte sequence defines the number
of bytes that make up a sequence. If some of those bytes are invalid,
the whole sequence is invalid and the fact that some of those
bytes may be interpretable as regular code points does not necessarily
result in better results - the reason is that loss of bytes in a
stream is far more unlikely than flipping a few bits in the data.
[-]str.decode('utf8', 'replace') -- conformance with Unicode 5.2.0[/-][+]str.decode('utf8', 'replace') -- conformance with Unicode 5.2.0[/+]ezio-melotti commentedon Apr 1, 2010
Here is an incomplete patch. It seems to solve the problem but I still have to add more tests and check it better.
I also wonder if the sequences with the first byte in range F5-FD (start of 4/5/6-byte sequences, restricted by RFC 3629) should behave in the same way. Right now they just "eat" the following 4/5/6 chars without checking.
45 remaining items
ezio-melotti commentedon May 25, 2012
Do you have any benchmark results?
serhiy-storchaka commentedon May 26, 2012
Here are the benchmark results (numbers are speed, MB/s).
On 32-bit Linux, AMD Athlon 64 X2:
utf-8 'A'*10000 2016 (+5%) 2111
utf-8 '\x80'*10000 383 (+9%) 416
utf-8 '\x80'+'A'*9999 1283 (+1%) 1301
utf-8 '\u0100'*10000 383 (-8%) 354
utf-8 '\u0100'+'A'*9999 1258 (-6%) 1184
utf-8 '\u0100'+'\x80'*9999 383 (-8%) 354
utf-8 '\u8000'*10000 434 (-11%) 388
utf-8 '\u8000'+'A'*9999 1262 (-6%) 1180
utf-8 '\u8000'+'\x80'*9999 383 (-8%) 354
utf-8 '\u8000'+'\u0100'*9999 383 (-8%) 354
utf-8 '\U00010000'*10000 358 (+1%) 361
utf-8 '\U00010000'+'A'*9999 1168 (-5%) 1104
utf-8 '\U00010000'+'\x80'*9999 382 (-20%) 307
utf-8 '\U00010000'+'\u0100'*9999 382 (-20%) 307
utf-8 '\U00010000'+'\u8000'*9999 404 (-10%) 365
On 32-bit Linux, Intel Atom N570:
ascii 'A'*10000 789 (+1%) 800
latin1 'A'*10000 796 (-2%) 781
latin1 'A'*9999+'\x80' 779 (+1%) 789
latin1 '\x80'*10000 1739 (-3%) 1690
latin1 '\x80'+'A'*9999 1747 (+1%) 1773
utf-8 'A'*10000 623 (+1%) 631
utf-8 '\x80'*10000 145 (+14%) 165
utf-8 '\x80'+'A'*9999 354 (+1%) 358
utf-8 '\u0100'*10000 164 (-5%) 156
utf-8 '\u0100'+'A'*9999 343 (+2%) 350
utf-8 '\u0100'+'\x80'*9999 164 (-4%) 157
utf-8 '\u8000'*10000 175 (-5%) 166
utf-8 '\u8000'+'A'*9999 349 (+2%) 356
utf-8 '\u8000'+'\x80'*9999 164 (-4%) 157
utf-8 '\u8000'+'\u0100'*9999 164 (-4%) 157
utf-8 '\U00010000'*10000 152 (+7%) 163
utf-8 '\U00010000'+'A'*9999 313 (+6%) 332
utf-8 '\U00010000'+'\x80'*9999 161 (-13%) 140
utf-8 '\U00010000'+'\u0100'*9999 161 (-14%) 139
utf-8 '\U00010000'+'\u8000'*9999 160 (-1%) 159
serhiy-storchaka commentedon May 26, 2012
Fortunately, bpo-14923 (if accepted) will compensate for the slowdown.
On 32-bit Linux, AMD Athlon 64 X2:
utf-8 'A'*10000 2016 (+3%) 2111 (-2%) 2072
utf-8 '\x80'*10000 383 (+19%) 416 (+9%) 454
utf-8 '\x80'+'A'*9999 1283 (-7%) 1301 (-9%) 1190
utf-8 '\u0100'*10000 383 (+46%) 354 (+58%) 560
utf-8 '\u0100'+'A'*9999 1258 (-1%) 1184 (+5%) 1244
utf-8 '\u0100'+'\x80'*9999 383 (+46%) 354 (+58%) 558
utf-8 '\u8000'*10000 434 (+6%) 388 (+19%) 461
utf-8 '\u8000'+'A'*9999 1262 (-1%) 1180 (+5%) 1244
utf-8 '\u8000'+'\x80'*9999 383 (+46%) 354 (+58%) 559
utf-8 '\u8000'+'\u0100'*9999 383 (+45%) 354 (+57%) 555
utf-8 '\U00010000'*10000 358 (+5%) 361 (+4%) 375
utf-8 '\U00010000'+'A'*9999 1168 (-1%) 1104 (+5%) 1159
utf-8 '\U00010000'+'\x80'*9999 382 (+43%) 307 (+78%) 546
utf-8 '\U00010000'+'\u0100'*9999 382 (+43%) 307 (+79%) 548
utf-8 '\U00010000'+'\u8000'*9999 404 (+13%) 365 (+25%) 458
On 32-bit Linux, Intel Atom N570:
utf-8 'A'*10000 623 (+1%) 631 (+0%) 631
utf-8 '\x80'*10000 145 (+26%) 165 (+11%) 183
utf-8 '\x80'+'A'*9999 354 (-0%) 358 (-1%) 353
utf-8 '\u0100'*10000 164 (+10%) 156 (+16%) 181
utf-8 '\u0100'+'A'*9999 343 (+1%) 350 (-1%) 348
utf-8 '\u0100'+'\x80'*9999 164 (+10%) 157 (+15%) 181
utf-8 '\u8000'*10000 175 (-1%) 166 (+5%) 174
utf-8 '\u8000'+'A'*9999 349 (+0%) 356 (-2%) 349
utf-8 '\u8000'+'\x80'*9999 164 (+10%) 157 (+15%) 180
utf-8 '\u8000'+'\u0100'*9999 164 (+10%) 157 (+15%) 181
utf-8 '\U00010000'*10000 152 (+7%) 163 (+0%) 163
utf-8 '\U00010000'+'A'*9999 313 (+4%) 332 (-2%) 327
utf-8 '\U00010000'+'\x80'*9999 161 (+11%) 140 (+28%) 179
utf-8 '\U00010000'+'\u0100'*9999 161 (+11%) 139 (+28%) 178
utf-8 '\U00010000'+'\u8000'*9999 160 (+9%) 159 (+9%) 174
pitrou commentedon Jun 23, 2012
Why is this marked "fixed"? Is it fixed or not?
serhiy-storchaka commentedon Jun 23, 2012
I deleted a fast patch, since it unsafe. bpo-14923 should safer compensate a small slowdown.
I think this change is not a bugfix (this is not a bug, the standard allows such behavior), but a new feature, so I doubt the need to fix 2.7 and 3.2. Any chance to commit the patch today and to get this feature in Python 3.3?
serhiy-storchaka commentedon Jun 23, 2012
No, it is not fully fixed. Only one bug was fixed, but the current
behavior is still not conformed with the Unicode Standard
*recommendations*. Non-conforming with recommendations is not a bug,
conforming is a feature.
serhiy-storchaka commentedon Jun 23, 2012
Here is updated, a little faster, patch. It merged with decode_utf8_range_check.patch from bpo-14923.
Patch contains non-modified Ezio Melotti's tests which all successfully passed.
serhiy-storchaka commentedon Jun 23, 2012
Here is updated patch with resolved merge conflict with 3214c9ebcf5e.
serhiy-storchaka commentedon Nov 4, 2012
What about commit? All Ezio's tests passsed, microbenchmark shows less than 10% differences:
vanilla patched
MB/s MB/s
2076 (-3%) 2007 decode utf-8 'A'*10000
414 (-0%) 413 decode utf-8 '\x80'*10000
1283 (-1%) 1275 decode utf-8 '\x80'+'A'*9999
556 (-8%) 514 decode utf-8 '\u0100'*10000
1227 (-4%) 1172 decode utf-8 '\u0100'+'A'*9999
556 (-8%) 514 decode utf-8 '\u0100'+'\x80'*9999
406 (+10%) 447 decode utf-8 '\u8000'*10000
1225 (-5%) 1167 decode utf-8 '\u8000'+'A'*9999
554 (-7%) 513 decode utf-8 '\u8000'+'\x80'*9999
552 (-8%) 508 decode utf-8 '\u8000'+'\u0100'*9999
358 (-4%) 345 decode utf-8 '\U00010000'*10000
1173 (-5%) 1118 decode utf-8 '\U00010000'+'A'*9999
492 (+1%) 495 decode utf-8 '\U00010000'+'\x80'*9999
492 (+1%) 496 decode utf-8 '\U00010000'+'\u0100'*9999
383 (+5%) 401 decode utf-8 '\U00010000'+'\u8000'*9999
python-dev commentedon Nov 4, 2012
New changeset 5962f192a483 by Ezio Melotti in branch '3.3':
bpo-8271: the utf-8 decoder now outputs the correct number of U+FFFD characters when used with the "replace" error handler on invalid utf-8 sequences. Patch by Serhiy Storchaka, tests by Ezio Melotti.
http://hg.python.org/cpython/rev/5962f192a483
New changeset 5b205fff1972 by Ezio Melotti in branch 'default':
bpo-8271: merge with 3.3.
http://hg.python.org/cpython/rev/5b205fff1972
ezio-melotti commentedon Nov 4, 2012
Fixed, thanks for updating the patch!
I committed it on 3.3 too, and while this could have gone on 2.7/3.2 too IMHO, it's to much work to port it there and not worth it.
serhiy-storchaka commentedon Nov 4, 2012
Agree. In 2.7 UTF-8 codec still broken in corner cases (it accepts
surrogates) and 3.2 is coming to an end of maintaining. In any case it is
only recomendation, not demands.
python-dev commentedon Nov 4, 2012
New changeset 96f4cee8ea5e by Victor Stinner in branch '3.3':
Issue bpo-8271: Fix compilation on Windows
http://hg.python.org/cpython/rev/96f4cee8ea5e
New changeset 6f44f33460cd by Victor Stinner in branch 'default':
(Merge 3.3) Issue bpo-8271: Fix compilation on Windows
http://hg.python.org/cpython/rev/6f44f33460cd