Skip to content

Commit 2cf6c79

Browse files
committed
Use correct CPU count in CPU widget to prevent a crash in 32-bit
1 parent dbe57bc commit 2cf6c79

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

src/widgets/cpu_widget.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@
2727

2828
extern iSK_INI* osd_ini;
2929

30+
31+
typedef enum _SYSTEM_INFORMATION_CLASS {
32+
SystemProcessorPerformanceInformation = 8,
33+
} SYSTEM_INFORMATION_CLASS;
34+
35+
typedef NTSTATUS(WINAPI* NtQuerySystemInformation_pfn)(
36+
_In_ SYSTEM_INFORMATION_CLASS SystemInformationClass,
37+
_Inout_ PVOID SystemInformation,
38+
_In_ ULONG SystemInformationLength,
39+
_Out_opt_ PULONG ReturnLength
40+
);
41+
42+
static NtQuerySystemInformation_pfn NtQuerySystemInformation =
43+
(NtQuerySystemInformation_pfn)SK_GetProcAddress(L"NtDll",
44+
"NtQuerySystemInformation");
45+
46+
3047
ULONG
3148
CALLBACK
3249
SK_CPU_DeviceNotifyCallback (
@@ -1717,14 +1734,21 @@ class SKWG_CPU_Monitor : public SK_Widget
17171734
SK_TLS_Bottom ();
17181735

17191736
// Processor count is not going to change at run-time or I will eat my hat
1737+
static const unsigned int cpu_count = []() -> unsigned int {
1738+
unsigned long len = 0;
1739+
NtQuerySystemInformation(SystemProcessorPerformanceInformation,
1740+
nullptr, 0, &len);
1741+
return len / sizeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION__SK);
1742+
}();
1743+
17201744
static SYSTEM_INFO sinfo = { };
17211745
SK_RunOnce (SK_GetSystemInfo (&sinfo));
17221746

17231747
PPROCESSOR_POWER_INFORMATION pwi =
17241748
reinterpret_cast <PPROCESSOR_POWER_INFORMATION> (
17251749
pTLS != nullptr ?
17261750
pTLS->scratch_memory->cpu_info.alloc (
1727-
sizeof (PROCESSOR_POWER_INFORMATION) * sinfo.dwNumberOfProcessors )
1751+
sizeof (PROCESSOR_POWER_INFORMATION) * cpu_count)
17281752
: nullptr );
17291753

17301754
if (pwi == nullptr) return; // Ohnoes, I No Can Haz RAM (!!)
@@ -1735,7 +1759,7 @@ class SKWG_CPU_Monitor : public SK_Widget
17351759
const NTSTATUS ntStatus =
17361760
CallNtPowerInformation ( ProcessorInformation,
17371761
nullptr, 0,
1738-
pwi, sizeof (PROCESSOR_POWER_INFORMATION) * sinfo.dwNumberOfProcessors );
1762+
pwi, sizeof (PROCESSOR_POWER_INFORMATION) * cpu_count );
17391763

17401764
bUseNtPower =
17411765
NT_SUCCESS (ntStatus);

0 commit comments

Comments
 (0)