This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: zipimport.zipimporter.get_data() crashes when path.replace() returns a non-str
Type: crash Stage: resolved
Components: Extension Modules Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Oren Milman, brett.cannon, serhiy.storchaka, twouters
Priority: normal Keywords:

Created on 2017-08-28 08:44 by Oren Milman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3226 merged Oren Milman, 2017-08-28 09:22
PR 3243 merged Oren Milman, 2017-08-30 10:29
Messages (9)
msg300944 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-08-28 08:44
on Windows, assuming the file 'foo.zip' exists, the following would cause an
assertion failure in Modules/zipimport.c in zipimport_zipimporter_get_data_impl():

import zipimport

class BadStr(str):
    def replace(self, old, new):
        return 42

zipimport.zipimporter('foo.zip').get_data(BadStr('bar'))


this is because zipimport_zipimporter_get_data_impl() assumes that
BadStr('bar').replace('/', '\\') is a string.
msg300962 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-08-28 19:45
I don't think this is a bad thing. This is duck-typing at work to be flexible where the type doesn't really matter, just the provided interface. If someone happens to provide an object that doesn't do the right thing then that's their fault for using the wrong object and the resulting exception that will occur from a bad path is acceptable.

I appreciate the work to do a PR, Oren, but I'm closing this issue as "rejected". (And if it makes you feel any better, you helped find a bug in the Bedevere bot. :) .
msg300963 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-08-28 19:57
I understand that our goal is to make Python better, not to make me happier :)

anyway, I haven't checked, but I am quite sure that similar code might
crash the interpreter on a release build of Python.
(just wanted to clarify that, as you used the term 'exception'.)
msg300965 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-08-28 20:38
If it will crash the interpreter then that's different. If that turns out to be the case then please come back here and we can rework the issue and PR to prevent that by being better about checking return values or resulting exception cases.
msg300966 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-08-28 20:58
just checked, and indeed on my Windows 10 the original code I posted here
crashes the interpreter.

The patch in the PR undermines duck-typing, and that's why I added a comment
there, stating I wasn't sure about the patch.
an alternate solution would be to simply check whether the return value of
pathname.replace('/', '\') is a str.

do you think I would update the PR to do that?
msg300967 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-08-28 22:00
Could you paste in what the crash looks like? E.g. is it a segfault, SystemError, etc.?

And you can tweak the PR or just open a new one if you would rather that fixes the crash itself without taking away duck typing.
msg300972 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-29 04:10
Oren already fixed several similar problems and he is aware of difference between a crash and an exception.

The problem is Windows specific. The current code uses Unicode C API with the result of calling the replace method. This can cause a segfault (or may be an assertion failure in debug build).

The proposed solution LGTM. It undermines duck-typing, but I think this doesn't break any existing code. The duck-typing was not intentional and is Windows-only.
msg300995 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-29 17:40
New changeset 631fdee6e61b4ba8ce800f827fecdd536bfb04f3 by Serhiy Storchaka (Oren Milman) in branch 'master':
bpo-31291: Fixed an assertion failure in zipimport.zipimporter.get_data() (#3226)
https://github.com/python/cpython/commit/631fdee6e61b4ba8ce800f827fecdd536bfb04f3
msg301021 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-30 11:08
New changeset 095a421b1606ee27e00a5d9d253b05a9f0cfadb8 by Serhiy Storchaka (Oren Milman) in branch '3.6':
[3.6] bpo-31291: Fixed an assertion failure in zipimport.zipimporter.get_data() (GH-3226) (#3243)
https://github.com/python/cpython/commit/095a421b1606ee27e00a5d9d253b05a9f0cfadb8
History
Date User Action Args
2022-04-11 14:58:51adminsetgithub: 75472
2017-08-30 11:09:04serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-08-30 11:08:45serhiy.storchakasetmessages: + msg301021
2017-08-30 10:29:45Oren Milmansetpull_requests: + pull_request3287
2017-08-29 17:40:17serhiy.storchakasetmessages: + msg300995
2017-08-29 04:10:45serhiy.storchakasetversions: + Python 3.6
nosy: + serhiy.storchaka

messages: + msg300972

components: + Extension Modules, - Interpreter Core
stage: patch review
2017-08-28 22:00:49brett.cannonsettitle: an assertion failure in zipimport.zipimporter.get_data() -> zipimport.zipimporter.get_data() crashes when path.replace() returns a non-str
2017-08-28 22:00:24brett.cannonsetstatus: closed -> open
resolution: rejected -> (no value)
stage: resolved -> (no value)
2017-08-28 22:00:14brett.cannonsetmessages: + msg300967
2017-08-28 20:58:15Oren Milmansetmessages: + msg300966
2017-08-28 20:38:29brett.cannonsetmessages: + msg300965
2017-08-28 19:57:03Oren Milmansetmessages: + msg300963
2017-08-28 19:45:48brett.cannonsetstatus: open -> closed

nosy: + brett.cannon, twouters
messages: + msg300962

resolution: rejected
stage: resolved
2017-08-28 09:22:17Oren Milmansetpull_requests: + pull_request3269
2017-08-28 08:44:26Oren Milmancreate