d3dx9: Return D3DERR_INVALIDCALL in D3DXLoadSurfaceFromMemory if pDestRect is invalid.
diff --git a/dlls/d3dx9_36/surface.c b/dlls/d3dx9_36/surface.c
index c39ec57..4bd7450 100644
--- a/dlls/d3dx9_36/surface.c
+++ b/dlls/d3dx9_36/surface.c
@@ -583,7 +583,8 @@
  *   Success: D3D_OK, if we successfully load the pixel data into our surface or
  *                    if pSrcMemory is NULL but the other parameters are valid
  *   Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
- *                                if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN)
+ *                                if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
+ *                                if DestRect is invalid
  *            D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
  *            E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
  *
@@ -631,8 +632,12 @@
         destsize.x = surfdesc.Width;
         destsize.y = surfdesc.Height;
     } else {
+        if(pDestRect->left > pDestRect->right || pDestRect->right > surfdesc.Width) return D3DERR_INVALIDCALL;
+        if(pDestRect->top > pDestRect->bottom || pDestRect->bottom > surfdesc.Height) return D3DERR_INVALIDCALL;
+        if(pDestRect->left < 0 || pDestRect->top < 0) return D3DERR_INVALIDCALL;
         destsize.x = pDestRect->right - pDestRect->left;
         destsize.y = pDestRect->bottom - pDestRect->top;
+        if(destsize.x == 0 || destsize.y == 0) return D3D_OK;
     }
 
     hr = IDirect3DSurface9_LockRect(pDestSurface, &lockrect, pDestRect, 0);
diff --git a/dlls/d3dx9_36/tests/surface.c b/dlls/d3dx9_36/tests/surface.c
index 43ec02b..83895c0 100644
--- a/dlls/d3dx9_36/tests/surface.c
+++ b/dlls/d3dx9_36/tests/surface.c
@@ -217,7 +217,7 @@
     HRESULT hr;
     BOOL testdummy_ok, testbitmap_ok;
     IDirect3DSurface9 *surf, *newsurf;
-    RECT rect;
+    RECT rect, destrect;
     D3DLOCKED_RECT lockrect;
     const WORD pixdata_a8r3g3b2[] = { 0x57df, 0x98fc, 0xacdd, 0xc891 };
     const WORD pixdata_a1r5g5b5[] = { 0x46b5, 0x99c8, 0x06a2, 0x9431 };
@@ -330,6 +330,26 @@
     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_UNKNOWN, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
     ok(hr == E_FAIL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, E_FAIL);
 
+    SetRect(&destrect, -1, -1, 1, 1); /* destination rect is partially outside texture boundaries */
+    hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
+    ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
+
+    SetRect(&destrect, 255, 255, 257, 257); /* destination rect is partially outside texture boundaries */
+    hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
+    ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
+
+    SetRect(&destrect, 1, 1, 0, 0); /* left > right, top > bottom */
+    hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
+    ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
+
+    SetRect(&destrect, 0, 0, 0, 0); /* left = right, top = bottom */
+    hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
+    ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
+
+    SetRect(&destrect, 257, 257, 257, 257); /* left = right, top = bottom, but invalid values */
+    hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
+    ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
+
 
     /* D3DXLoadSurfaceFromSurface */
     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &newsurf, NULL);