>From 54c134b2e0c8ab9ad3df41a806fd6f3497b4d6eb Mon Sep 17 00:00:00 2001
From: Nikolay Sivov <nsivov@codeweavers.com>
Date: Sat, 22 Oct 2011 21:22:05 +0400
Subject: [PATCH 2/3] Don't try to use null interface pointer

---
 dlls/msxml3/node.c         |    7 +++++--
 dlls/msxml3/tests/domdoc.c |   20 ++++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/dlls/msxml3/node.c b/dlls/msxml3/node.c
index 681cd44..7219bd3 100644
--- a/dlls/msxml3/node.c
+++ b/dlls/msxml3/node.c
@@ -311,8 +311,11 @@ HRESULT node_insert_before(xmlnode *This, IXMLDOMNode *new_child, const VARIANT
 
     case VT_UNKNOWN:
     case VT_DISPATCH:
-        hr = IUnknown_QueryInterface(V_UNKNOWN(ref_child), &IID_IXMLDOMNode, (void**)&before);
-        if(FAILED(hr)) return hr;
+        if (V_UNKNOWN(ref_child))
+        {
+            hr = IUnknown_QueryInterface(V_UNKNOWN(ref_child), &IID_IXMLDOMNode, (void**)&before);
+            if(FAILED(hr)) return hr;
+        }
         break;
 
     default:
diff --git a/dlls/msxml3/tests/domdoc.c b/dlls/msxml3/tests/domdoc.c
index a758880..c12aa5c 100644
--- a/dlls/msxml3/tests/domdoc.c
+++ b/dlls/msxml3/tests/domdoc.c
@@ -9341,12 +9341,32 @@ static void test_insertBefore(void)
     hr = IXMLDOMDocument_createElement(doc, _bstr_("elem3"), &elem3);
     ok(hr == S_OK, "got 0x%08x\n", hr);
 
+    hr = IXMLDOMDocument_createElement(doc, _bstr_("elem3"), &elem3);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXMLDOMDocument_createElement(doc, _bstr_("elem4"), &elem4);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
     EXPECT_NO_CHILDREN(elem1);
     EXPECT_NO_CHILDREN(elem2);
     EXPECT_NO_CHILDREN(elem3);
 
     todo_wine EXPECT_REF(elem2, 2);
 
+    V_VT(&v) = VT_DISPATCH;
+    V_DISPATCH(&v) = NULL;
+    node = NULL;
+    hr = IXMLDOMElement_insertBefore(elem1, (IXMLDOMNode*)elem4, v, &node);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+    ok(node == (void*)elem4, "got %p\n", node);
+
+    EXPECT_CHILDREN(elem1);
+    hr = IXMLDOMElement_removeChild(elem1, (IXMLDOMNode*)elem4, NULL);
+    EXPECT_HR(hr, S_OK);
+    IXMLDOMElement_Release(elem4);
+
+    EXPECT_NO_CHILDREN(elem1);
+
     V_VT(&v) = VT_NULL;
     node = NULL;
     hr = IXMLDOMElement_insertBefore(elem1, (IXMLDOMNode*)elem2, v, &node);
-- 
1.5.6.5


