NULL dereference crash sometimes under [super initWithCoder:] in WebView

https://bugs.webkit.org/show_bug.cgi?id=184851
rdar://problem/39611236

Reviewed by Tim Horton.

Source/WebKit:

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _initializeWithConfiguration:]):
(-[WKWebView effectiveAppearanceDidChange]):
Added a null check and call the code later in initialization.

Source/WebKitLegacy/mac:

* WebView/WebView.mm:
(-[WebView _commonInitializationWithFrameName:groupName:]):
(-[WebView effectiveAppearanceDidChange]):
Added a null check and call the code later in initialization.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@230884 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebKit/ChangeLog b/Source/WebKit/ChangeLog
index c97d576..31f8700 100644
--- a/Source/WebKit/ChangeLog
+++ b/Source/WebKit/ChangeLog
@@ -1,3 +1,17 @@
+2018-04-20  Timothy Hatcher  <timothy@apple.com>
+
+        NULL dereference crash sometimes under [super initWithCoder:] in WebView
+
+        https://bugs.webkit.org/show_bug.cgi?id=184851
+        rdar://problem/39611236
+
+        Reviewed by Tim Horton.
+
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView _initializeWithConfiguration:]):
+        (-[WKWebView effectiveAppearanceDidChange]):
+        Added a null check and call the code later in initialization.
+
 2018-04-20  Tim Horton  <timothy_horton@apple.com>
 
         Adjust geolocation feature flag
diff --git a/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm b/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
index 20ebbd1..6e8f072 100644
--- a/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
+++ b/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
@@ -696,6 +696,7 @@
 
     _impl->setAutomaticallyAdjustsContentInsets(true);
     _impl->setRequiresUserActionForEditingControlsManager([configuration _requiresUserActionForEditingControlsManager]);
+    _impl->setDefaultAppearance([self _defaultAppearance]);
 #endif
 
 #if ENABLE(ACCESSIBILITY_EVENTS)
@@ -6260,6 +6261,11 @@
 
 - (void)effectiveAppearanceDidChange
 {
+    // This can be called during [super initWithCoder:] and [super initWithFrame:].
+    // That is before _impl is ready to be used, so check. <rdar://problem/39611236>
+    if (!_impl)
+        return;
+
     _impl->setDefaultAppearance([self _defaultAppearance]);
 }
 
diff --git a/Source/WebKitLegacy/mac/ChangeLog b/Source/WebKitLegacy/mac/ChangeLog
index e688547..20282c75 100644
--- a/Source/WebKitLegacy/mac/ChangeLog
+++ b/Source/WebKitLegacy/mac/ChangeLog
@@ -1,3 +1,17 @@
+2018-04-20  Timothy Hatcher  <timothy@apple.com>
+
+        NULL dereference crash sometimes under [super initWithCoder:] in WebView
+
+        https://bugs.webkit.org/show_bug.cgi?id=184851
+        rdar://problem/39611236
+
+        Reviewed by Tim Horton.
+
+        * WebView/WebView.mm:
+        (-[WebView _commonInitializationWithFrameName:groupName:]):
+        (-[WebView effectiveAppearanceDidChange]):
+        Added a null check and call the code later in initialization.
+
 2018-04-20  Tim Horton  <timothy_horton@apple.com>
 
         Adjust geolocation feature flag
diff --git a/Source/WebKitLegacy/mac/WebView/WebView.mm b/Source/WebKitLegacy/mac/WebView/WebView.mm
index a354bb6..43487d8 100644
--- a/Source/WebKitLegacy/mac/WebView/WebView.mm
+++ b/Source/WebKitLegacy/mac/WebView/WebView.mm
@@ -1541,6 +1541,7 @@
 
 #if !PLATFORM(IOS)
     [self _registerDraggedTypes];
+    [self _updateDefaultAppearance];
 #endif
 
     [self _setIsVisible:[self _isViewVisible]];
@@ -5298,9 +5299,11 @@
 
 - (void)effectiveAppearanceDidChange
 {
-    if (!_private->page)
+    // This can be called during [super initWithCoder:] and [super initWithFrame:].
+    // That is before _private is ready to be used, so check. <rdar://problem/39611236>
+    if (!_private || !_private->page)
         return;
-    
+
     [self _updateDefaultAppearance];
 }