Description: Check if we have failed due to insufficient memory and skip if so. Don't use MALLOC_PERTURB since it seems that this forces <size of file> memory to be available which OOMs on many systems.
Author: Iain Lane <laney@debian.org>
Bug-Upstream: https://bugzilla.gnome.org/show_bug.cgi?id=754387

Index: b/tests/cve-2015-4491.c
===================================================================
--- a/tests/cve-2015-4491.c
+++ b/tests/cve-2015-4491.c
@@ -19,6 +19,9 @@
  */
 
 #include <gdk-pixbuf.h>
+#include <malloc.h>
+
+#include "test-common.h"
 
 static void
 test_original (void)
@@ -29,7 +32,8 @@
 
   buf = gdk_pixbuf_new_from_resource_at_scale ("/test/resource/cve-2015-4491.bmp", size, size, FALSE, &err);
 
-  g_assert_no_error (err);
+  if (!assert_error_not_insufficient_memory (err))
+      return;
 
   g_object_unref (buf);
 }
@@ -76,6 +80,8 @@
 int
 main (int argc, char *argv[])
 {
+  /* With perturbing, this test OOMs on weaker machines. */
+  mallopt (M_PERTURB, 0);
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/pixbuf/cve-2015-4491/original", test_original);
Index: b/tests/pixbuf-scale.c
===================================================================
--- a/tests/pixbuf-scale.c
+++ b/tests/pixbuf-scale.c
@@ -83,7 +83,9 @@
 
   path = g_test_get_filename (G_TEST_DIST, filename, NULL);
   ref = gdk_pixbuf_new_from_file (path, &error);
-  g_assert_no_error (error);
+
+  if (!assert_error_not_insufficient_memory (error))
+      return;
 
   width = gdk_pixbuf_get_width (ref);
   height = gdk_pixbuf_get_height (ref);
@@ -111,10 +113,18 @@
 
   path = g_test_get_filename (G_TEST_DIST, filename, NULL);
   ref = gdk_pixbuf_new_from_file (path, &error);
-  g_assert_no_error (error);
+
+  if (!assert_error_not_insufficient_memory (error))
+      return;
 
   pixbuf = gdk_pixbuf_add_alpha (ref, FALSE, 0, 0, 0);
-  g_assert (pixbuf != NULL);
+
+  if (pixbuf == NULL)
+  {
+      g_test_skip ("Couldn't add alpha to the image - your system probably lacks sufficient memory.");
+      return;
+  }
+
   g_object_unref (pixbuf);
 
   pixbuf = gdk_pixbuf_add_alpha (ref, TRUE, 0, 0, 255);
@@ -141,10 +151,18 @@
 
   path = g_test_get_filename (G_TEST_DIST, filename, NULL);
   ref = gdk_pixbuf_new_from_file (path, &error);
-  g_assert_no_error (error);
+
+  if (!assert_error_not_insufficient_memory (error))
+      return;
 
   pixbuf = gdk_pixbuf_rotate_simple (ref, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
-  g_assert (pixbuf != NULL);
+
+  if (pixbuf == NULL)
+  {
+      g_test_skip ("Couldn't rotae the image - your system probably lacks sufficient memory.");
+      return;
+  }
+
   g_object_unref (pixbuf);
 
   g_object_unref (ref);
Index: b/tests/test-common.c
===================================================================
--- a/tests/test-common.c
+++ b/tests/test-common.c
@@ -65,6 +65,19 @@
 }
 
 gboolean
+assert_error_not_insufficient_memory (GError *err)
+{
+  if (err && g_error_matches (err, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY))
+  {
+      g_test_skip ("Skipping - your system has insufficient memory to load the test image.");
+      return FALSE;
+  }
+
+  g_assert_no_error (err);
+  return TRUE;
+}
+
+gboolean
 pixdata_equal (GdkPixbuf *p1, GdkPixbuf *p2, GError **error)
 {
   if (gdk_pixbuf_get_colorspace (p1) != gdk_pixbuf_get_colorspace (p2)) {
Index: b/tests/test-common.h
===================================================================
--- a/tests/test-common.h
+++ b/tests/test-common.h
@@ -28,6 +28,7 @@
 G_BEGIN_DECLS
 
 gboolean format_supported (const gchar *filename);
+gboolean assert_error_not_insufficient_memory (GError *err);
 gboolean pixdata_equal (GdkPixbuf *p1, GdkPixbuf *p2, GError **error);
 
 G_END_DECLS
Index: b/tests/Makefile.am
===================================================================
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -60,6 +60,8 @@
 
 cve_2015_4491_SOURCES =			\
 	cve-2015-4491.c			\
+	test-common.c			\
+	test-common.h			\
 	resources.h			\
 	resources.c			\
 	$(NULL)
