lfreist / hwinfo

cross platform C++ library for hardware information (CPU, RAM, GPU, ...)
MIT License
438 stars 76 forks source link

GPU: try to retrive 0 element from vector #38

Closed KennyProgrammer closed 1 year ago

KennyProgrammer commented 1 year ago

In GPU class in one of Win32 getVendor, getName, getDriverVersion, you try retrive 0 element from vector, and then you check if ret is string is empty, but if vector is empty and you try to retrive 0 element is crashed.

How it was:

std::string GPU::getVendor() {
  std::vector<const wchar_t*> vendor{};
  wmi::queryWMI("WIN32_VideoController", "AdapterCompatibility", vendor);
  auto ret = vendor[0];
  if (!ret) {
    return "<unknown>";
  }
  std::wstring tmp(ret);
  return {tmp.begin(), tmp.end()};
}

How it should be :

std::string GPU::getVendor() {
  std::vector<const wchar_t*> vendor{};
  wmi::queryWMI("WIN32_VideoController", "AdapterCompatibility", vendor);
  if(vendor.empty())
    return "<unknown>";

  auto ret = vendor[0];
  std::wstring tmp(ret);
  return {tmp.begin(), tmp.end()};
}

Please fix this with all this functions, even if library cannot retrive some hardware properties it at least should return unknown but not a crash.

lfreist commented 1 year ago

Hi, thanks for pointing this out. I'll fix it

KennyProgrammer commented 1 year ago

Thanks) Also you have the same issue in Win32 Motherboard class.

And another bug I've found that when on Windows multiple times use CoInitialize and CoUninitialize, it crashes or not retriving the next info at all (first info fine), so l've try to initialize it once and when all information I need I get then uninitialize it. Maybe it problem with my project because somewhere I already call this.

For test I create init.h and init.cpp files with init(), close() functions, then move initialization/uninitialization code there, and between them retrieve relevant information.