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: _winreg.EnumValue causes MemoryError
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, iritkatriel, kzmi, ocean-city, tim.golden, vstinner
Priority: normal Keywords: patch

Created on 2010-09-24 09:17 by kzmi, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
fonts.reg kzmi, 2010-09-24 09:19 Sample registory data
test.py kzmi, 2010-09-24 09:21 py file to reproduce
Messages (14)
msg117264 - (view) Author: Iwasa Kazmi (kzmi) Date: 2010-09-24 09:17
_winreg.EnumValue causes MemoryError if the name of the value contains multibyte unicode characters.

I'm using 2.6.6.
I think it is relevant to Isssue #2810.
msg117265 - (view) Author: Iwasa Kazmi (kzmi) Date: 2010-09-24 09:19
Added sample registory data file.
msg117266 - (view) Author: Iwasa Kazmi (kzmi) Date: 2010-09-24 09:21
Added py file to reproduce.
msg117289 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-09-24 15:11
I'm not able to reproduce any MemoryError on 2.6, 2.7, or 3.2 on Windows 7 using your registry data and test script.
msg117291 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-09-24 15:14
I can reproduce the issue on python2.x. I cannot on
python3.x.
msg117308 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-09-24 16:56
This happens because RegQueryInfoKeyA returns maximum value
name length not in *Ansi* characters but in *Unicode" characters.
I don't know clue why.

http://msdn.microsoft.com/en-us/library/ms724902%28VS.85%29.aspx

In multibyte environment, 2 or 3 bytes characters are
mapped to one size unicode. So usually, the length of *Ansi*
characters are larger than or equals to the length of *Unicode*
characters. If every Ansi characters are mapped to one size
Unicode, lengthes are equal, so probably this issue won't happen.

I'll attach the patch to fix this issue. But be care, this is not 
tested so densely. At least, the script to reproduce this issue
ran fine.
msg117310 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-09-24 17:00
Again, I'm not sure this is right fix.
msg117403 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-09-26 05:40
Sorry, my patch didn't pass test_dynamic_key(test_winreg).
(Apologize for not testing on unit test)

RegEnumValueA and RegQueryInfoKeyA always returned 0 for 
required data size if target is HKEY_PERFORMANCE_DATA.

The possible fixes I can imagine are...

1. Use GetCPInfo() to get MaxCharSize.
http://msdn.microsoft.com/en-us/library/dd317780%28v=VS.85%29.aspx
We can get a large value enough to store the key name in ANSI by
"The length in Unicode" * MaxCharSize.

2. Use RegEnumValueW and convert result via WideCharToMultiByte

I'll attach the patch with option 1.
msg117404 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-09-26 05:55
I shall create unit test later.
msg117438 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-09-27 08:25
I've withdrew my patch... It has still problem, and
I cannot fix it yet. I suppose option 2 is easy.
ANSI API's limitation makes it harder to implement
correctly.
msg117439 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-09-27 11:10
Can't we use RegEnumValueW and RegQueryInfoKeyW?
msg222618 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-09 14:25
Does anyone wish to follow this up as I no longer run 2.7?
msg409943 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-07 00:02
> I can reproduce the issue on python2.x. I cannot on python3.x.

Did anyone reproduce on python3.X? Why are versions 3.4/3.5 selected?
msg409956 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-01-07 10:06
> Did anyone reproduce on python3.X? 

In principle, winreg should not have a problem in 3.x because it only uses the UTF-16 wide-character API.
History
Date User Action Args
2022-04-11 14:57:06adminsetgithub: 54146
2022-01-07 10:06:55eryksunsetstatus: pending -> closed

nosy: + eryksun
messages: + msg409956

stage: resolved
2022-01-07 00:02:05iritkatrielsetstatus: open -> pending

nosy: + iritkatriel
messages: + msg409943

resolution: out of date
2019-04-26 18:37:05BreamoreBoysetnosy: - BreamoreBoy
2014-07-09 15:26:57brian.curtinsetnosy: - brian.curtin
2014-07-09 14:25:23BreamoreBoysetnosy: + BreamoreBoy

messages: + msg222618
versions: + Python 3.4, Python 3.5, - Python 3.1, Python 3.2
2010-09-27 11:10:14vstinnersetmessages: + msg117439
2010-09-27 08:25:03ocean-citysetmessages: + msg117438
2010-09-27 08:23:14ocean-citysetfiles: - py27_fix_winreg_EnumValue_op1.patch
2010-09-27 08:22:58ocean-citysetfiles: - py27_winreg_EnumValue.patch
2010-09-26 05:55:09ocean-citysetmessages: + msg117404
2010-09-26 05:40:56ocean-citysetfiles: + py27_fix_winreg_EnumValue_op1.patch

messages: + msg117403
2010-09-24 17:00:40ocean-citysetmessages: + msg117310
2010-09-24 16:56:28ocean-citysetfiles: + py27_winreg_EnumValue.patch
keywords: + patch
messages: + msg117308
2010-09-24 15:22:12ocean-citysetmessages: - msg117293
2010-09-24 15:21:34ocean-citysetmessages: + msg117293
2010-09-24 15:14:06ocean-citysetnosy: + ocean-city
messages: + msg117291
2010-09-24 15:11:52brian.curtinsetmessages: + msg117289
2010-09-24 12:23:55pitrousetnosy: + vstinner, tim.golden, brian.curtin

versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6
2010-09-24 09:21:07kzmisetfiles: + test.py

messages: + msg117266
2010-09-24 09:19:39kzmisetfiles: + fonts.reg

messages: + msg117265
2010-09-24 09:17:15kzmicreate