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

Windows: Incorrect handling IEnumWbemClassObject::Next calls #90

Open
ruifig opened this issue Jun 25, 2024 · 0 comments
Open

Windows: Incorrect handling IEnumWbemClassObject::Next calls #90

ruifig opened this issue Jun 25, 2024 · 0 comments
Assignees
Labels
bug Something isn't working windows Issues related to windows

Comments

@ruifig
Copy link

ruifig commented Jun 25, 2024

I'm hitting crashes/exceptions due to what seems to be incorrect handling of IEnumWbemClassObject::Next

  ULONG u_return = 0;
  IWbemClassObject* obj = nullptr;
  int cpu_id = 0;
  while (wmi.enumerator) {
    wmi.enumerator->Next(WBEM_INFINITE, 1, &obj, &u_return);
    if (!u_return) {
      break;
    }
    ...

That pattern above seems to be used in several places, and I believe it has a few bugs

  • u_return can have 1 set from the previous loop iteration and not that last Next call
  • obj can be set from the previous loop iteration, instead of the current Next call, causing Object is not connected to server error when trying to use it (since that was already release)

I can't reproduce the issue on my machine, but several other developers are encountering this problem.
To be fair, I didn't read the entire documentation for Next (https://learn.microsoft.com/en-us/windows/win32/api/wbemcli/nf-wbemcli-ienumwbemclassobject-next) , but the easiest fix seems to be to simply move u_return and obj to inside the loop to avoid side effects from the previous iteration, and thus we can rely on that for the checks. E.g:

  int cpu_id = 0;
  while (wmi.enumerator) {
    ULONG u_return = 0;
    IWbemClassObject* obj = nullptr;
    wmi.enumerator->Next(WBEM_INFINITE, 1, &obj, &u_return);
    if (!u_return) {
      break;
    }
    ...
@lfreist lfreist added bug Something isn't working windows Issues related to windows labels Jul 3, 2024
@lfreist lfreist self-assigned this Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working windows Issues related to windows
Projects
None yet
Development

No branches or pull requests

2 participants