Skip to content

Commit 0e9953e

Browse files
committed
Update by review
1 parent 696ca3d commit 0e9953e

3 files changed

Lines changed: 54 additions & 35 deletions

File tree

Lib/test/test_winapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,6 @@ def test_report_event(self):
185185
# Test with empty strings list
186186
_winapi.ReportEvent(handle, _winapi.EVENTLOG_AUDIT_FAILURE, 2, 1003, [])
187187

188-
with self.assertRaisesRegex(TypeError, 'All strings must be unicode'):
188+
with self.assertRaisesRegex(TypeError, 'expected a list of strings, not int'):
189189
_winapi.ReportEvent(handle, _winapi.EVENTLOG_ERROR_TYPE, 0, 1001,
190190
["string", 123])

Modules/_winapi.c

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
29932993
static 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
}

Modules/clinic/_winapi.c.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)