[gegl] linear_new_from_data: Fail if given an invalid rowstride



commit 50c60c7544c36da7405c999c893433962546e7ff
Author: Daniel Sabo <DanielSabo gmail com>
Date:   Mon Mar 3 06:11:24 2014 -0800

    linear_new_from_data: Fail if given an invalid rowstride
    
    Previously this would result in a silently corrupted buffer.

 gegl/buffer/gegl-buffer-linear.c |   62 +++++++++++++++++++------------------
 1 files changed, 32 insertions(+), 30 deletions(-)
---
diff --git a/gegl/buffer/gegl-buffer-linear.c b/gegl/buffer/gegl-buffer-linear.c
index 7d20563..de870b1 100644
--- a/gegl/buffer/gegl-buffer-linear.c
+++ b/gegl/buffer/gegl-buffer-linear.c
@@ -64,20 +64,24 @@ gegl_buffer_linear_new_from_data (const gpointer       data,
                                   gpointer             destroy_fn_data)
 {
   GeglBuffer *buffer;
+  GeglTile   *tile;
+  gint        bpp;
 
-  if (extent==NULL)
-    {
-      g_error ("got a NULL extent");
-    }
+  g_return_val_if_fail (extent, NULL);
+  g_return_val_if_fail (format, NULL);
 
-  g_assert (format);
+  bpp = babl_format_get_bytes_per_pixel (format);
 
-  if (rowstride <= 0) /* handle both 0 and negative coordinates as a request
-                       * for a rowstride, negative rowstrides are not supported.
-                       */
-    rowstride = extent->width;
+  if (rowstride == 0)
+    {
+      rowstride = extent->width;
+    }
   else
-    rowstride = rowstride / babl_format_get_bytes_per_pixel (format);
+    {
+      g_return_val_if_fail (rowstride > 0, NULL);
+      g_return_val_if_fail (rowstride % bpp == 0, NULL);
+      rowstride = rowstride / bpp;
+    }
 
   buffer = g_object_new (GEGL_TYPE_BUFFER,
                          "x",          extent->x,
@@ -94,26 +98,24 @@ gegl_buffer_linear_new_from_data (const gpointer       data,
 
   g_object_set_data (G_OBJECT (buffer), "is-linear", (void*)0xf00);
 
-  {
-    GeglTile *tile = gegl_tile_new_bare ();
-
-    tile->tile_storage = buffer->tile_storage;
-    tile->x = 0;
-    tile->y = 0;
-    tile->z = 0;
-    tile->next_shared = tile;
-    tile->prev_shared = tile;
-    tile->rev = tile->stored_rev + 1;
-    gegl_tile_set_data_full (tile,
-                             (gpointer) data,
-                             babl_format_get_bytes_per_pixel (format) * rowstride * extent->height,
-                             destroy_fn,
-                             destroy_fn_data);
-
-    if (buffer->tile_storage->cache)
-      gegl_tile_handler_cache_insert (buffer->tile_storage->cache, tile, 0, 0, 0);
-    gegl_tile_unref (tile);
-  }
+  tile = gegl_tile_new_bare ();
+
+  tile->tile_storage = buffer->tile_storage;
+  tile->x = 0;
+  tile->y = 0;
+  tile->z = 0;
+  tile->next_shared = tile;
+  tile->prev_shared = tile;
+  tile->rev = tile->stored_rev + 1;
+  gegl_tile_set_data_full (tile,
+                           (gpointer) data,
+                           bpp * rowstride * extent->height,
+                           destroy_fn,
+                           destroy_fn_data);
+
+  if (buffer->tile_storage->cache)
+    gegl_tile_handler_cache_insert (buffer->tile_storage->cache, tile, 0, 0, 0);
+  gegl_tile_unref (tile);
 
   return buffer;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]