Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read violation crash in psutil.cpu_count(logical=False) #1480

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ psutil_cpu_count_phys(PyObject *self, PyObject *args) {
DWORD length = 0;
DWORD offset = 0;
DWORD ncpus = 0;
DWORD prev_processor_info_size = 0;

// GetLogicalProcessorInformationEx() is available from Windows 7
// onward. Differently from GetLogicalProcessorInformation()
Expand Down Expand Up @@ -525,13 +526,20 @@ psutil_cpu_count_phys(PyObject *self, PyObject *args) {
}

ptr = buffer;
while (ptr->Size > 0 && offset + ptr->Size <= length) {
while (offset < length) {
// Advance ptr by the size of the previous
// SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX struct.
ptr = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)\
(((char*)ptr) + prev_processor_info_size);

if (ptr->Relationship == RelationProcessorCore) {
ncpus += 1;
}

// When offset == length, we've reached the last processor
// info struct in the buffer.
offset += ptr->Size;
ptr = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)\
(((char*)ptr) + ptr->Size);
prev_processor_info_size = ptr->Size;
}

free(buffer);
Expand Down