Issue 36311: Flaw in Windows code page decoder for large input (original) (raw)

Created on 2019-03-16 08:06 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15083 merged steve.dower,2019-08-02 22:04
PR 15374 merged miss-islington,2019-08-21 23:22
PR 15375 merged miss-islington,2019-08-21 23:22
Messages (8)
msg338061 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-03-16 08:06
There is a flaw in PyUnicode_DecodeCodePageStateful() (exposed as _codecs.code_page_decode() at Python level). Since MultiByteToWideChar() takes the size of the input as C int, it can not be used for decoding more than 2 GiB. Large input is split on chunks of size 2 GiB which are decoded separately. The problem is if it split in the middle of a multibyte character. In this case decoding chunks will always fail or replace incomplete parts of the multibyte character at both ends with what the error handler returns. It is hard to reproduce this bug, because you need to decode more than 2 GiB, and you will need at least 14 GiB of RAM for this (maybe more).
msg338626 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-03-22 23:30
I have 24G if all working and would be willing to try to run a test case.
msg348921 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-08-02 21:34
If we reduce our chunk size below INT_MAX, then we avoid the issue entirely. Our logic for hitting the middle of a multibyte character is fine (perhaps fixed since this issue was opened?), there's just a weird edge case at 2 GiB in the API call. As a bonus, smaller chunks seems to have a performance benefit too. It seems like INT_MAX/4 is the sweet spot - it took about a quarter of the time for my 2GiB test case as INT_MAX (and we're measuring in tens of seconds here, so I'm pretty comfortable with the direction of the result). INT_MAX/2 and INT_MAX/8 were both slower than INT_MAX/4.
msg350131 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-08-21 23:22
New changeset 7ebdda0dbee7df6f0c945a7e1e623e47676e112d by Steve Dower in branch 'master': bpo-36311: Fixes decoding multibyte characters around chunk boundaries and improves decoding performance (GH-15083) https://github.com/python/cpython/commit/7ebdda0dbee7df6f0c945a7e1e623e47676e112d
msg350133 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-08-21 23:29
I'll get the 3.7 and 3.8 backports merged - looks like they're trivial. Going to need some help with the 2.7 backport, but I'm happy to approve a PR.
msg350136 - (view) Author: miss-islington (miss-islington) Date: 2019-08-21 23:53
New changeset f93c15aedc2ea2cb8b56fc9dbb0d412918992e86 by Miss Islington (bot) in branch '3.8': bpo-36311: Fixes decoding multibyte characters around chunk boundaries and improves decoding performance (GH-15083) https://github.com/python/cpython/commit/f93c15aedc2ea2cb8b56fc9dbb0d412918992e86
msg350137 - (view) Author: miss-islington (miss-islington) Date: 2019-08-21 23:56
New changeset 735a960ac98cf414caf910565220ab2761fa542a by Miss Islington (bot) in branch '3.7': bpo-36311: Fixes decoding multibyte characters around chunk boundaries and improves decoding performance (GH-15083) https://github.com/python/cpython/commit/735a960ac98cf414caf910565220ab2761fa542a
msg351391 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-09-09 09:53
Declaring this out-of-scope for 2.7, unless someone wants to insist (and provide a PR).
History
Date User Action Args
2022-04-11 14:59:12 admin set github: 80492
2019-09-09 09:53:49 steve.dower set status: open -> closedversions: - Python 2.7messages: + resolution: fixedstage: backport needed -> resolved
2019-08-21 23:56:01 miss-islington set messages: +
2019-08-21 23:53:59 miss-islington set nosy: + miss-islingtonmessages: +
2019-08-21 23:29:04 steve.dower set messages: + stage: patch review -> backport needed
2019-08-21 23:22:53 miss-islington set pull_requests: + <pull%5Frequest15086>
2019-08-21 23:22:46 miss-islington set pull_requests: + <pull%5Frequest15085>
2019-08-21 23:22:36 steve.dower set messages: +
2019-08-02 22:04:07 steve.dower set keywords: + patchstage: test needed -> patch reviewpull_requests: + <pull%5Frequest14828>
2019-08-02 21:58:26 steve.dower set assignee: steve.dowerversions: + Python 3.9
2019-08-02 21:34:20 steve.dower set messages: +
2019-03-22 23:30:25 terry.reedy set nosy: + terry.reedymessages: + stage: test needed
2019-03-16 08:06:40 serhiy.storchaka create