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: nullptr check when closing windows #22948

Merged
merged 3 commits into from Apr 8, 2020
Merged

fix: nullptr check when closing windows #22948

merged 3 commits into from Apr 8, 2020

Conversation

codebytere
Copy link
Member

Description of Change

Closes #14161.

We weren't performing a nullptr check, so we would experience an EXC_BAD_ACCESS crash
if an object in the WindowVector is destroyed during cleanup. This fixes that by adding said nullptr check.

cc @zcbenz @deepak1556 @MarshallOfSound

Checklist

Release Notes

Notes: Fixed an occasional crash when closing all BrowserWindows.

@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Apr 3, 2020
Copy link
Member

@MarshallOfSound MarshallOfSound left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we explain how this helps? These aren't smart pointers, if this is an invalid access it's a UAF not a nullptr issue.

WindowVector === std::vector<NativeWindow*> so unless we're injecting / replacing nullptr in the vector this won't fix the issue afaik

@deepak1556
Copy link
Member

changing to std::vector<base::WeakPtr<NativeWindow>> will help rather than maintaining a raw map, the ideal solution is for the map to be maintained by AddWindow , RemoveWindow , but I don't know when is case this happens. Open to solutions.

@MarshallOfSound
Copy link
Member

If we make this a vector of weak pointers this solution makes sense, without it I don't think this fix actually helps.

@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Apr 4, 2020
Copy link
Member

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both DestroyAllWindows and CloseAllWindows work on a copied array of windows, and the source array is being updated when a window is created/destroyed, so its values can not be nullptr.

It seems the app destroyed other windows when a window is being closed, and the copied windows array still included freed pointers and crashed when calling window->IsClosed(). So checking null would not fix the crash as it is a wild pointer instead of null pointer.

// app is doing something like this:
childWindow.once('closed', () => {
  parentWindow.destroy()
})

An ideal solution would be forbidding users to delete NativeWindow and have WindowList manage the deletion, but that would be a fairly large refactoring. I think a small workaround would be something like:

void WindowList::DestroyAllWindows() {
  std::vector<base::WeakPtr<NativeWindow>> windows = ConvertToWeakPtrVector(GetInstance()->windows_);

  for (const auto& window : windows) {
    if (window)
      window->CloseImmediately();
  }
}

@deepak1556
Copy link
Member

deepak1556 commented Apr 7, 2020

@zcbenz any reason not maintain the WindowList vector contents as weakptr rather than converting at the call site ?

@zcbenz
Copy link
Member

zcbenz commented Apr 7, 2020

I think the internal records should still be a vector of raw ptrs, the purpose of WindowList is to provide a precise list of existing windows and it is doing the work correctly. Changing the internal vector to store weak ptrs would be a weird design, because WindowList already correctly manages the window list so there is no way for its elements to be null.

There is crash because CloseAllWindows and DestroyAllWindows are deleting windows while iterating them, so I think we should only fix these methods.

shell/browser/window_list.cc Outdated Show resolved Hide resolved
shell/browser/window_list.cc Show resolved Hide resolved
shell/browser/window_list.cc Outdated Show resolved Hide resolved
@codebytere codebytere merged commit 54f8c4e into master Apr 8, 2020
@release-clerk
Copy link

release-clerk bot commented Apr 8, 2020

Release Notes Persisted

Fixed an occasional crash when closing all BrowserWindows.

@trop
Copy link
Contributor

trop bot commented Apr 8, 2020

I have automatically backported this PR to "8-x-y", please check out #23022

@trop
Copy link
Contributor

trop bot commented Apr 8, 2020

I have automatically backported this PR to "9-x-y", please check out #23023

@trop
Copy link
Contributor

trop bot commented Apr 8, 2020

I have automatically backported this PR to "7-2-x", please check out #23024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EXC_BAD_ACCESS: atom::WindowList::CloseAllWindows() [window_list.cc : 88 + 0x3]
4 participants