@@ -2982,7 +2982,7 @@ _winapi.RegisterEventSource -> HANDLE
29822982
29832983 unc_server_name: LPCWSTR(accept={str, NoneType})
29842984 The UNC name of the server on which the event source should be registered.
2985- If NULL , registers the event source on the local computer.
2985+ If None , registers the event source on the local computer.
29862986 source_name: LPCWSTR
29872987 The name of the event source to register.
29882988 /
@@ -2993,7 +2993,7 @@ Retrieves a registered handle to the specified event log.
29932993static HANDLE
29942994_winapi_RegisterEventSource_impl (PyObject * module , LPCWSTR unc_server_name ,
29952995 LPCWSTR source_name )
2996- /*[clinic end generated code: output=e376c8950a89ae8f input=9642e69236d0a14e ]*/
2996+ /*[clinic end generated code: output=e376c8950a89ae8f input=9d01059ac2156d0c ]*/
29972997{
29982998 HANDLE handle ;
29992999
@@ -3068,40 +3068,58 @@ _winapi_ReportEvent_impl(PyObject *module, HANDLE handle,
30683068 DWORD data_size = 0 ;
30693069
30703070 // Handle strings list
3071- if (strings != Py_None && PyList_Check (strings )) {
3072- Py_ssize_t size = PyList_Size (strings );
3073- num_strings = (WORD )size ;
3074-
3075- if (num_strings > 0 ) {
3076- string_array = (LPCWSTR * )PyMem_Malloc (num_strings * sizeof (LPCWSTR ));
3077- if (!string_array ) {
3078- return PyErr_NoMemory ();
3079- }
3080-
3081- for (WORD i = 0 ; i < num_strings ; i ++ ) {
3082- PyObject * item = PyList_GetItem (strings , i );
3083- if (!PyUnicode_Check (item )) {
3084- PyMem_Free (string_array );
3085- PyErr_SetString (PyExc_TypeError , "All strings must be unicode" );
3086- return NULL ;
3087- }
3088- string_array [i ] = PyUnicode_AsWideCharString (item , NULL );
3089- if (!string_array [i ]) {
3090- // Clean up already allocated strings
3091- for (WORD j = 0 ; j < i ; j ++ ) {
3092- PyMem_Free ((void * )string_array [j ]);
3093- }
3094- PyMem_Free (string_array );
3095- return NULL ;
3096- }
3097- }
3098- }
3071+ Py_ssize_t size = PyList_Size (strings );
3072+ if (size > USHRT_MAX ) {
3073+ PyErr_SetString (PyExc_ValueError , "strings is too long" );
3074+ return NULL ;
30993075 }
3076+ num_strings = (WORD )size ;
31003077
31013078 // Handle raw data
3079+ if (raw_data -> len > PY_DWORD_MAX ) {
3080+ PyErr_SetString (PyExc_ValueError , "raw_data is too large" );
3081+ return NULL ;
3082+ }
31023083 if (raw_data -> buf != NULL ) {
31033084 data = raw_data -> buf ;
3104- data_size = (DWORD ) raw_data -> len ;
3085+ data_size = (DWORD )raw_data -> len ;
3086+ }
3087+
3088+ if (num_strings > 0 ) {
3089+ string_array = (LPCWSTR * )PyMem_New (LPCWSTR , num_strings );
3090+ if (string_array == NULL ) {
3091+ return PyErr_NoMemory ();
3092+ }
3093+
3094+ for (WORD i = 0 ; i < num_strings ; i ++ ) {
3095+ PyObject * item = PyList_GetItemRef (strings , i );
3096+ if (item == NULL ) {
3097+ // Clean up already allocated strings
3098+ for (WORD j = 0 ; j < i ; j ++ ) {
3099+ PyMem_Free ((void * )string_array [j ]);
3100+ }
3101+ PyMem_Free (string_array );
3102+ return NULL ;
3103+ }
3104+ if (!PyUnicode_Check (item )) {
3105+ for (WORD j = 0 ; j < i ; j ++ ) {
3106+ PyMem_Free ((void * )string_array [j ]);
3107+ }
3108+ PyMem_Free (string_array );
3109+ PyErr_Format (PyExc_TypeError ,
3110+ "expected a list of strings, not %T" , item );
3111+ return NULL ;
3112+ }
3113+ string_array [i ] = PyUnicode_AsWideCharString (item , NULL );
3114+ Py_DECREF (item );
3115+ if (!string_array [i ]) {
3116+ for (WORD j = 0 ; j < i ; j ++ ) {
3117+ PyMem_Free ((void * )string_array [j ]);
3118+ }
3119+ PyMem_Free (string_array );
3120+ return NULL ;
3121+ }
3122+ }
31053123 }
31063124
31073125 Py_BEGIN_ALLOW_THREADS
@@ -3110,6 +3128,7 @@ _winapi_ReportEvent_impl(PyObject *module, HANDLE handle,
31103128 string_array , data );
31113129 Py_END_ALLOW_THREADS
31123130
3131+ int ret = GetLastError ();
31133132 // Clean up allocated strings
31143133 if (string_array ) {
31153134 for (WORD i = 0 ; i < num_strings ; i ++ ) {
@@ -3119,7 +3138,7 @@ _winapi_ReportEvent_impl(PyObject *module, HANDLE handle,
31193138 }
31203139
31213140 if (!success )
3122- return PyErr_SetFromWindowsErr (0 );
3141+ return PyErr_SetFromWindowsErr (ret );
31233142
31243143 Py_RETURN_NONE ;
31253144}
0 commit comments