Clean up failures in DRI2CreateBuffer

Change-Id: If47c76bccac5ec9de9ca41886230de6f3e9549d7
diff --git a/src/omap_dri2.c b/src/omap_dri2.c
index 237efac..ebcaebe 100644
--- a/src/omap_dri2.c
+++ b/src/omap_dri2.c
@@ -144,7 +144,7 @@
 	ScreenPtr pScreen = pDraw->pScreen;
 	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
 	OMAPDRI2BufferPtr buf = calloc(1, sizeof(*buf));
-	PixmapPtr pPixmap;
+	PixmapPtr pPixmap = NULL;
 	struct omap_bo *bo;
 	int ret;
 
@@ -152,6 +152,7 @@
 			pDraw, attachment, format);
 
 	if (!buf) {
+		ERROR_MSG("Couldn't allocate internal buffer structure");
 		return NULL;
 	}
 
@@ -188,16 +189,14 @@
 	{
 		assert(attachment != DRI2BufferFrontLeft);
 		ERROR_MSG("Failed to create back buffer for window");
-		free(buf);
-		return NULL;
+		goto fail;
 	}
 
 	bo = OMAPPixmapBo(pPixmap);
 	if (!bo)
 	{
 		ERROR_MSG("Attempting to DRI2 wrap a pixmap with no DRM buffer object backing");
-		free(buf);
-		return NULL;
+		goto fail;
 	}
 
 	DRIBUF(buf)->attachment = attachment;
@@ -212,8 +211,7 @@
 	ret = omap_bo_get_name(bo, &DRIBUF(buf)->name);
 	if (ret) {
 		ERROR_MSG("could not get buffer name: %d", ret);
-		/* TODO: MIDEGL-1462: cleanup */
-		return NULL;
+		goto fail;
 	}
 
 	/* Q: how to know across OMAP generations what formats that the display
@@ -234,6 +232,22 @@
 	OMAPRegisterExternalAccess(pPixmap);
 
 	return DRIBUF(buf);
+
+fail:
+	if (pPixmap != NULL)
+	{
+		if (attachment != DRI2BufferFrontLeft)
+		{
+			pScreen->DestroyPixmap(pPixmap);
+		}
+		else
+		{
+			pPixmap->refcnt--;
+		}
+	}
+	free(buf);
+
+	return NULL;
 }
 
 /**