[Python-checkins] cpython (2.7): Issue21349 Passing a memoryview to _winreg.SetValueEx now correctly raises a

tim.golden python-checkins at python.org
Sat Apr 26 16:49:01 CEST 2014


http://hg.python.org/cpython/rev/061db174baad
changeset:   90461:061db174baad
branch:      2.7
parent:      90456:4f79c3827adc
user:        Tim Golden <mail at timgolden.me.uk>
date:        Sat Apr 26 15:47:08 2014 +0100
summary:
  Issue21349 Passing a memoryview to _winreg.SetValueEx now correctly raises a TypeError where it previously crashed the interpreter. Patch by Brian Kearns

files:
  Lib/test/test_winreg.py |   9 +++++++++
  Misc/ACKS               |   1 +
  PC/_winreg.c            |  10 ++++++----
  3 files changed, 16 insertions(+), 4 deletions(-)


diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py
--- a/Lib/test/test_winreg.py
+++ b/Lib/test/test_winreg.py
@@ -329,6 +329,15 @@
         finally:
             DeleteKey(HKEY_CURRENT_USER, test_key_name)
 
+    def test_setvalueex_with_memoryview(self):
+        try:
+            with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
+                self.assertNotEqual(ck.handle, 0)
+                with self.assertRaises(TypeError):
+                    SetValueEx(ck, "test_name", None, REG_BINARY, memoryview('val'))
+        finally:
+            DeleteKey(HKEY_CURRENT_USER, test_key_name)
+
     def test_queryvalueex_return_value(self):
         # Test for Issue #16759, return unsigned int from QueryValueEx.
         # Reg2Py, which gets called by QueryValueEx, was returning a value
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -657,6 +657,7 @@
 Anton Kasyanov
 Lou Kates
 Hiroaki Kawai
+Brian Kearns
 Sebastien Keim
 Ryan Kelly
 Dan Kenigsberg
diff --git a/PC/_winreg.c b/PC/_winreg.c
--- a/PC/_winreg.c
+++ b/PC/_winreg.c
@@ -888,7 +888,7 @@
             else {
                 void *src_buf;
                 PyBufferProcs *pb = value->ob_type->tp_as_buffer;
-                if (pb==NULL) {
+                if (pb == NULL || pb->bf_getreadbuffer == NULL) {
                     PyErr_Format(PyExc_TypeError,
                         "Objects of type '%s' can not "
                         "be used as binary registry values",
@@ -896,9 +896,11 @@
                     return FALSE;
                 }
                 *retDataSize = (*pb->bf_getreadbuffer)(value, 0, &src_buf);
-                *retDataBuf = (BYTE *)PyMem_NEW(char,
-                                                *retDataSize);
-                if (*retDataBuf==NULL){
+                if (*retDataSize < 0) {
+                    return FALSE;
+                }
+                *retDataBuf = (BYTE *)PyMem_NEW(char, *retDataSize);
+                if (*retDataBuf == NULL){
                     PyErr_NoMemory();
                     return FALSE;
                 }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list