From 5e3e2b6137d13cd64f3118e86af9a9fcb745fcfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 11 Oct 2022 22:36:14 +0800 Subject: [PATCH 1/4] Windows: fix possible crashes --- src/detection/battery/battery_windows.cpp | 6 ++++-- src/detection/font/font_windows.cpp | 4 +--- src/detection/gpu/gpu_windows.cpp | 2 +- src/detection/host/host_windows.cpp | 4 +--- src/detection/localip/localip_windows.c | 17 ++++++++++++----- src/detection/users/users_windows.cpp | 2 +- src/util/windows/wmi.cpp | 7 ++++--- 7 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/detection/battery/battery_windows.cpp b/src/detection/battery/battery_windows.cpp index 48084720ab..fce0b5657d 100644 --- a/src/detection/battery/battery_windows.cpp +++ b/src/detection/battery/battery_windows.cpp @@ -26,7 +26,7 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) ffStrbufInit(&battery->modelName); ffGetWmiObjString(pclsObj, L"Name", &battery->modelName); - uint64_t chemistry; + uint64_t chemistry = 0; ffGetWmiObjUnsigned(pclsObj, L"Chemistry", &chemistry); switch(chemistry) { @@ -38,6 +38,7 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) case 6: ffStrbufInitS(&battery->technology, "Lithium-ion"); break; case 7: ffStrbufInitS(&battery->technology, "Zinc air"); break; case 8: ffStrbufInitS(&battery->technology, "Lithium Polymer"); break; + default: ffStrbufInit(&battery->technology); break; } uint64_t capacity; @@ -59,12 +60,13 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) case 9: ffStrbufInitS(&battery->status, "Charging and Critical"); break; case 10: ffStrbufInitS(&battery->status, "Undefined"); break; case 11: ffStrbufInitS(&battery->status, "Partially Charged"); break; + default: ffStrbufInit(&battery->status); break; } battery->temperature = FF_BATTERY_TEMP_UNSET; } - pclsObj->Release(); + if(pclsObj) pclsObj->Release(); pEnumerator->Release(); return nullptr; } diff --git a/src/detection/font/font_windows.cpp b/src/detection/font/font_windows.cpp index 1d4196bde3..0ade9812ea 100644 --- a/src/detection/font/font_windows.cpp +++ b/src/detection/font/font_windows.cpp @@ -19,9 +19,7 @@ void ffDetectFontImpl(const FFinstance* instance, FFFontResult* result) IWbemClassObject *pclsObj = NULL; ULONG uReturn = 0; - pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); - - if(uReturn == 0) + if(FAILED(pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn)) || uReturn == 0) { ffStrbufInitS(&result->error, "No WMI result returned"); pEnumerator->Release(); diff --git a/src/detection/gpu/gpu_windows.cpp b/src/detection/gpu/gpu_windows.cpp index 443ad422ec..d998856339 100644 --- a/src/detection/gpu/gpu_windows.cpp +++ b/src/detection/gpu/gpu_windows.cpp @@ -38,7 +38,7 @@ const char* ffDetectGPUImpl(FFlist* gpus, const FFinstance* instance) gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; } - pclsObj->Release(); + if(pclsObj) pclsObj->Release(); pEnumerator->Release(); return nullptr; } diff --git a/src/detection/host/host_windows.cpp b/src/detection/host/host_windows.cpp index 34141cf72d..60f3fcedfe 100644 --- a/src/detection/host/host_windows.cpp +++ b/src/detection/host/host_windows.cpp @@ -23,9 +23,7 @@ extern "C" void ffDetectHostImpl(FFHostResult* host) IWbemClassObject *pclsObj = NULL; ULONG uReturn = 0; - pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); - - if(uReturn == 0) + if(FAILED(pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn)) || uReturn == 0) { ffStrbufInitS(&host->error, "No Wmi result returned"); pEnumerator->Release(); diff --git a/src/detection/localip/localip_windows.c b/src/detection/localip/localip_windows.c index 881dd2d6ea..275987f10c 100644 --- a/src/detection/localip/localip_windows.c +++ b/src/detection/localip/localip_windows.c @@ -10,11 +10,18 @@ static void addNewIp(FFlist* list, const wchar_t* name, const char* addr, bool i FFLocalIpResult* ip = (FFLocalIpResult*) ffListAdd(list); int len = (int)wcslen(name); - int size_needed = WideCharToMultiByte(CP_UTF8, 0, name, len, NULL, 0, NULL, NULL); - ffStrbufInitA(&ip->name, (uint32_t)size_needed + 1); - WideCharToMultiByte(CP_UTF8, 0, name, len, ip->name.chars, size_needed, NULL, NULL); - ip->name.length = (uint32_t)size_needed; - ip->name.chars[size_needed] = '\0'; + if(len > 0) + { + int size_needed = WideCharToMultiByte(CP_UTF8, 0, name, len, NULL, 0, NULL, NULL); + ffStrbufInitA(&ip->name, (uint32_t)size_needed + 1); + WideCharToMultiByte(CP_UTF8, 0, name, len, ip->name.chars, size_needed, NULL, NULL); + ip->name.length = (uint32_t)size_needed; + ip->name.chars[size_needed] = '\0'; + } + else + { + ffStrbufInitS(&ip->name, "*"); + } ffStrbufInitS(&ip->addr, addr); ip->ipv6 = ipv6; diff --git a/src/detection/users/users_windows.cpp b/src/detection/users/users_windows.cpp index 43a6fb3d2a..41dd9674ba 100644 --- a/src/detection/users/users_windows.cpp +++ b/src/detection/users/users_windows.cpp @@ -37,6 +37,6 @@ void ffDetectUsers(FFlist* users, FFstrbuf* error) if(users->length == 0) ffStrbufAppendS(error, "Unable to detect users"); - pclsObj->Release(); + if(pclsObj) pclsObj->Release(); pEnumerator->Release(); } diff --git a/src/util/windows/wmi.cpp b/src/util/windows/wmi.cpp index cff1c9a978..925d428eef 100644 --- a/src/util/windows/wmi.cpp +++ b/src/util/windows/wmi.cpp @@ -2,6 +2,7 @@ #include #include +#include //https://learn.microsoft.com/en-us/windows/win32/wmisdk/example--getting-wmi-data-from-the-local-computer //https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/computer-system-hardware-classes @@ -232,7 +233,7 @@ bool ffGetWmiObjSigned(IWbemClassObject* obj, const wchar_t* key, int64_t* integ case VT_UI4: *integer = (int64_t)vtProp.uintVal; break; case VT_UI8: *integer = (int64_t)vtProp.ullVal; break; case VT_BOOL: *integer = vtProp.boolVal != VARIANT_FALSE; break; - default: result = false; + default: *integer = 0; result = false; } } VariantClear(&vtProp); @@ -266,7 +267,7 @@ bool ffGetWmiObjUnsigned(IWbemClassObject* obj, const wchar_t* key, uint64_t* in case VT_UI4: *integer = vtProp.uintVal; break; case VT_UI8: *integer = vtProp.ullVal; break; case VT_BOOL: *integer = vtProp.boolVal != VARIANT_FALSE; break; - default: result = false; + default: *integer = 0; result = false; } } VariantClear(&vtProp); @@ -302,7 +303,7 @@ bool ffGetWmiObjReal(IWbemClassObject* obj, const wchar_t* key, double* real) case VT_R4: *real = vtProp.fltVal; break; case VT_R8: *real = vtProp.dblVal; break; case VT_BOOL: *real = vtProp.boolVal != VARIANT_FALSE; break; - default: result = false; + default: *real = NAN; result = false; } } VariantClear(&vtProp); From 8c127a175cd8f0c354917d6e7f077d252b69c61c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 12 Oct 2022 00:13:49 +0800 Subject: [PATCH 2/4] TerminalShell: try detecting more shells --- src/detection/terminalshell/terminalshell_windows.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/detection/terminalshell/terminalshell_windows.cpp b/src/detection/terminalshell/terminalshell_windows.cpp index 7496fd6be0..8c1466974b 100644 --- a/src/detection/terminalshell/terminalshell_windows.cpp +++ b/src/detection/terminalshell/terminalshell_windows.cpp @@ -77,8 +77,15 @@ static uint32_t getShellInfo(FFTerminalShellResult* result, uint32_t pid) } else if(ffStrbufIgnCaseCompS(&result->shellPrettyName, "powershell") == 0) ffStrbufSetS(&result->shellPrettyName, "Windows PowerShell"); + else if(ffStrbufIgnCaseCompS(&result->shellPrettyName, "powershell_ise") == 0) + ffStrbufSetS(&result->shellPrettyName, "Windows PowerShell ISE"); else if(ffStrbufIgnCaseCompS(&result->shellPrettyName, "cmd") == 0) ffStrbufSetS(&result->shellPrettyName, "Command Prompt"); + else if(ffStrbufIgnCaseCompS(&result->terminalPrettyName, "explorer") == 0) + { + ffStrbufSetS(&result->terminalPrettyName, "Windows Explorer"); // Started without shell + return 0; + } return ppid; } @@ -99,6 +106,8 @@ static uint32_t getTerminalInfo(FFTerminalShellResult* result, uint32_t pid) ffStrbufSetS(&result->terminalPrettyName, "Windows Terminal"); else if(ffStrbufIgnCaseCompS(&result->terminalPrettyName, "conhost") == 0) ffStrbufSetS(&result->terminalPrettyName, "Console Window Host"); + else if(ffStrbufIgnCaseCompS(&result->terminalPrettyName, "explorer") == 0) + ffStrbufSetS(&result->terminalPrettyName, "Windows Explorer"); return ppid; } @@ -148,7 +157,6 @@ const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) return &result; // TODO: handle nested shells - // TODO: handle running without shells ( dblclick exe in Windows Explorer ) ppid = getTerminalInfo(&result, ppid); if(ppid == 0) From 34cd81b4d9ead7fd78fd38f59dce268810b712ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 12 Oct 2022 00:17:09 +0800 Subject: [PATCH 3/4] ci: run fastfetch on Windows --- .github/workflows/push.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 6b532c563c..76fd9ede9b 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -171,14 +171,13 @@ jobs: run: cmake --build . --target fastfetch --target flashfetch # Makes no sense to install exes to /usr/bin for Windows - name: copy necessary dlls - run: cp /usr/bin/msys-2.0.dll /clang64/bin/*.dll . + run: cp /usr/bin/msys-2.0.dll /clang64/bin/{libcjson,libOpenCL,vulkan-1}.dll . - # Crashes on start for some reason, but it provides binaries at least. Needs investigation. - # - name: run fastfetch - # run: ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all + - name: run fastfetch + run: ./fastfetch --recache --disable-linewrap false --hide-cursor false --show-errors true --load-config presets/all - # - name: run flashfetch - # run: ./flashfetch + - name: run flashfetch + run: ./flashfetch - name: upload artifacts uses: actions/upload-artifact@v3 From 4bae08c159cba3d0a319561b4ab97a8658a1b57e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 12 Oct 2022 00:36:01 +0800 Subject: [PATCH 4/4] OS: detect Windows Server --- src/detection/os/os_windows.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/detection/os/os_windows.cpp b/src/detection/os/os_windows.cpp index b1c595dd44..4df85791f9 100644 --- a/src/detection/os/os_windows.cpp +++ b/src/detection/os/os_windows.cpp @@ -33,13 +33,21 @@ void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance) return; } - ffGetWmiObjString(pclsObj, L"Caption", &os->variant); // Microsoft Windows 11 家庭中文版 + ffGetWmiObjString(pclsObj, L"Caption", &os->variant); if(ffStrbufStartsWithS(&os->variant, "Microsoft Windows ")) { ffStrbufAppendS(&os->name, "Microsoft Windows"); ffStrbufAppendS(&os->prettyName, "Windows"); - ffStrbufSubstrAfter(&os->variant, strlen("Microsoft Windows ") - 1); // 11 家庭中文版 + ffStrbufSubstrAfter(&os->variant, strlen("Microsoft Windows ") - 1); + + if(ffStrbufStartsWithS(&os->variant, "Server ")) + { + ffStrbufAppendS(&os->name, " Server"); + ffStrbufAppendS(&os->prettyName, " Server"); + ffStrbufSubstrAfter(&os->variant, strlen(" Server") - 1); + } + uint32_t index = ffStrbufFirstIndexC(&os->variant, ' '); ffStrbufAppendNS(&os->version, index, os->variant.chars); ffStrbufSubstrAfter(&os->variant, index);