warn and return NULL instead of a meaningless struct for invalid

2004-02-15  Michael Natterer  <mitch@gimp.org>

	* libgimp/gimpdrawable.c (gimp_drawable_get): warn and return NULL
	instead of a meaningless struct for invalid drawable_IDs. Will
	make buggy plug-ins crash earlier and more reliably.

	Replaced all if(drawable) by g_return_if_fail(drawable != NULL)

	* libgimp/gimptile.c (gimp_tile_ref): replaced if(tile)
	by g_return_if_fail(tile != NULL)

	(gimp_tile_unref): g_return_if_fail(tile->ref_count > 0)
This commit is contained in:
Michael Natterer 2004-02-15 22:18:17 +00:00 committed by Michael Natterer
parent 8ebce54f6b
commit acbc6ead6a
3 changed files with 116 additions and 90 deletions

View file

@ -1,3 +1,16 @@
2004-02-15 Michael Natterer <mitch@gimp.org>
* libgimp/gimpdrawable.c (gimp_drawable_get): warn and return NULL
instead of a meaningless struct for invalid drawable_IDs. Will
make buggy plug-ins crash earlier and more reliably.
Replaced all if(drawable) by g_return_if_fail(drawable != NULL)
* libgimp/gimptile.c (gimp_tile_ref): replaced if(tile)
by g_return_if_fail(tile != NULL)
(gimp_tile_unref): g_return_if_fail(tile->ref_count > 0)
2004-02-15 Sven Neumann <sven@gimp.org>
* plug-ins/common/gif.c: only call gimp_ui_init() when being run

View file

@ -32,16 +32,24 @@ GimpDrawable *
gimp_drawable_get (gint32 drawable_ID)
{
GimpDrawable *drawable;
gint width;
gint height;
gint bpp;
width = gimp_drawable_width (drawable_ID);
height = gimp_drawable_height (drawable_ID);
bpp = gimp_drawable_bpp (drawable_ID);
g_return_val_if_fail (width > 0 && height > 0 && bpp > 0, NULL);
drawable = g_new0 (GimpDrawable, 1);
drawable = g_new (GimpDrawable, 1);
drawable->drawable_id = drawable_ID;
drawable->width = gimp_drawable_width (drawable_ID);
drawable->height = gimp_drawable_height (drawable_ID);
drawable->bpp = gimp_drawable_bpp (drawable_ID);
drawable->ntile_rows = (drawable->height + TILE_HEIGHT - 1) / TILE_HEIGHT;
drawable->ntile_cols = (drawable->width + TILE_WIDTH - 1) / TILE_WIDTH;
drawable->tiles = NULL;
drawable->shadow_tiles = NULL;
drawable->width = width;
drawable->height = height;
drawable->bpp = bpp;
drawable->ntile_rows = (height + TILE_HEIGHT - 1) / TILE_HEIGHT;
drawable->ntile_cols = (width + TILE_WIDTH - 1) / TILE_WIDTH;
return drawable;
}
@ -49,17 +57,16 @@ gimp_drawable_get (gint32 drawable_ID)
void
gimp_drawable_detach (GimpDrawable *drawable)
{
if (drawable)
{
gimp_drawable_flush (drawable);
g_return_if_fail (drawable != NULL);
if (drawable->tiles)
g_free (drawable->tiles);
if (drawable->shadow_tiles)
g_free (drawable->shadow_tiles);
gimp_drawable_flush (drawable);
g_free (drawable);
}
if (drawable->tiles)
g_free (drawable->tiles);
if (drawable->shadow_tiles)
g_free (drawable->shadow_tiles);
g_free (drawable);
}
void
@ -69,27 +76,26 @@ gimp_drawable_flush (GimpDrawable *drawable)
gint n_tiles;
gint i;
if (drawable)
g_return_if_fail (drawable != NULL);
if (drawable->tiles)
{
if (drawable->tiles)
{
tiles = drawable->tiles;
n_tiles = drawable->ntile_rows * drawable->ntile_cols;
tiles = drawable->tiles;
n_tiles = drawable->ntile_rows * drawable->ntile_cols;
for (i = 0; i < n_tiles; i++)
if ((tiles[i].ref_count > 0) && tiles[i].dirty)
gimp_tile_flush (&tiles[i]);
}
for (i = 0; i < n_tiles; i++)
if ((tiles[i].ref_count > 0) && tiles[i].dirty)
gimp_tile_flush (&tiles[i]);
}
if (drawable->shadow_tiles)
{
tiles = drawable->shadow_tiles;
n_tiles = drawable->ntile_rows * drawable->ntile_cols;
if (drawable->shadow_tiles)
{
tiles = drawable->shadow_tiles;
n_tiles = drawable->ntile_rows * drawable->ntile_cols;
for (i = 0; i < n_tiles; i++)
if ((tiles[i].ref_count > 0) && tiles[i].dirty)
gimp_tile_flush (&tiles[i]);
}
for (i = 0; i < n_tiles; i++)
if ((tiles[i].ref_count > 0) && tiles[i].dirty)
gimp_tile_flush (&tiles[i]);
}
}
@ -106,57 +112,54 @@ gimp_drawable_get_tile (GimpDrawable *drawable,
gint tile_num;
gint i, j, k;
if (drawable)
g_return_val_if_fail (drawable != NULL, NULL);
if (shadow)
tiles = drawable->shadow_tiles;
else
tiles = drawable->tiles;
if (! tiles)
{
n_tiles = drawable->ntile_rows * drawable->ntile_cols;
tiles = g_new (GimpTile, n_tiles);
right_tile = drawable->width - ((drawable->ntile_cols - 1) * TILE_WIDTH);
bottom_tile = drawable->height - ((drawable->ntile_rows - 1) * TILE_HEIGHT);
for (i = 0, k = 0; i < drawable->ntile_rows; i++)
{
for (j = 0; j < drawable->ntile_cols; j++, k++)
{
tiles[k].bpp = drawable->bpp;
tiles[k].tile_num = k;
tiles[k].ref_count = 0;
tiles[k].dirty = FALSE;
tiles[k].shadow = shadow;
tiles[k].data = NULL;
tiles[k].drawable = drawable;
if (j == (drawable->ntile_cols - 1))
tiles[k].ewidth = right_tile;
else
tiles[k].ewidth = TILE_WIDTH;
if (i == (drawable->ntile_rows - 1))
tiles[k].eheight = bottom_tile;
else
tiles[k].eheight = TILE_HEIGHT;
}
}
if (shadow)
tiles = drawable->shadow_tiles;
drawable->shadow_tiles = tiles;
else
tiles = drawable->tiles;
if (!tiles)
{
n_tiles = drawable->ntile_rows * drawable->ntile_cols;
tiles = g_new (GimpTile, n_tiles);
right_tile = drawable->width - ((drawable->ntile_cols - 1) * TILE_WIDTH);
bottom_tile = drawable->height - ((drawable->ntile_rows - 1) * TILE_HEIGHT);
for (i = 0, k = 0; i < drawable->ntile_rows; i++)
{
for (j = 0; j < drawable->ntile_cols; j++, k++)
{
tiles[k].bpp = drawable->bpp;
tiles[k].tile_num = k;
tiles[k].ref_count = 0;
tiles[k].dirty = FALSE;
tiles[k].shadow = shadow;
tiles[k].data = NULL;
tiles[k].drawable = drawable;
if (j == (drawable->ntile_cols - 1))
tiles[k].ewidth = right_tile;
else
tiles[k].ewidth = TILE_WIDTH;
if (i == (drawable->ntile_rows - 1))
tiles[k].eheight = bottom_tile;
else
tiles[k].eheight = TILE_HEIGHT;
}
}
if (shadow)
drawable->shadow_tiles = tiles;
else
drawable->tiles = tiles;
}
tile_num = row * drawable->ntile_cols + col;
return &tiles[tile_num];
drawable->tiles = tiles;
}
return NULL;
tile_num = row * drawable->ntile_cols + col;
return &tiles[tile_num];
}
GimpTile *
@ -168,6 +171,8 @@ gimp_drawable_get_tile2 (GimpDrawable *drawable,
gint row;
gint col;
g_return_val_if_fail (drawable != NULL, NULL);
col = x / TILE_WIDTH;
row = y / TILE_HEIGHT;

View file

@ -38,6 +38,7 @@
*/
#define FREE_QUANTUM 0.1
void gimp_read_expect_msg (WireMessage *msg,
gint type);
@ -47,6 +48,8 @@ static void gimp_tile_cache_insert (GimpTile *tile);
static void gimp_tile_cache_flush (GimpTile *tile);
/* private variables */
static GHashTable * tile_hash_table = NULL;
static GList * tile_list_head = NULL;
static GList * tile_list_tail = NULL;
@ -55,21 +58,22 @@ static gulong cur_cache_size = 0;
static gulong max_cache_size = 0;
/* public functions */
void
gimp_tile_ref (GimpTile *tile)
{
if (tile)
g_return_if_fail (tile != NULL);
tile->ref_count++;
if (tile->ref_count == 1)
{
tile->ref_count++;
if (tile->ref_count == 1)
{
gimp_tile_get (tile);
tile->dirty = FALSE;
}
gimp_tile_cache_insert (tile);
gimp_tile_get (tile);
tile->dirty = FALSE;
}
gimp_tile_cache_insert (tile);
}
void
@ -90,6 +94,7 @@ gimp_tile_unref (GimpTile *tile,
gboolean dirty)
{
g_return_if_fail (tile != NULL);
g_return_if_fail (tile->ref_count > 0);
tile->ref_count--;
tile->dirty |= dirty;
@ -150,6 +155,9 @@ gimp_tile_cache_ntiles (gulong ntiles)
gimp_tile_height () * 4 + 1023) / 1024);
}
/* private functions */
static void
gimp_tile_get (GimpTile *tile)
{