app, pdb: fix #8363 gimp_selection_float() crash

Calling gimp_selection_float from a Python plug-in could make it crash
with an error like Calling error for procedure 'gimp-selection-float':
Item x cannot be used because it is not a group item.

This is caused by an incorrect check for group layers.
gimp_pdb_item_is_group returns an error when the condition is False,
while we only want an error when a group layer is selected (True).
Thus we need to use gimp_pdb_item_is_not_group, which returns an error
when the item is a group, which is what we want.

These function names are a little confusing, we might need to think
about better naming sometime.

I added C/Python tests for this function, so that we can test whether
this works correctly.
This commit is contained in:
Jacob Boerema 2024-04-15 18:07:48 -04:00
parent 140a3c82d0
commit 7aa33bf771
5 changed files with 133 additions and 4 deletions

View File

@ -223,7 +223,7 @@ selection_float_invoker (GimpProcedure *procedure,
{
if (! gimp_pdb_item_is_attached (GIMP_ITEM (drawables[i]), NULL,
GIMP_PDB_ITEM_CONTENT, error) ||
gimp_pdb_item_is_group (GIMP_ITEM (drawables[i]), error) ||
! gimp_pdb_item_is_not_group (GIMP_ITEM (drawables[i]), error) ||
(image && image != gimp_item_get_image (GIMP_ITEM (drawables[i]))))
{
success = FALSE;

View File

@ -8,6 +8,7 @@ endif
tests = [
'palette',
'selection-float'
]
# Unit testing environment is based on gimp_run_env with additional environment

View File

@ -0,0 +1,75 @@
static GimpValueArray *
gimp_c_test_run (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
gint n_drawables,
GimpDrawable **drawables,
GimpProcedureConfig *config,
gpointer run_data)
{
GimpImage *img;
GimpDrawable *layer1;
GimpDrawable *layer2;
GimpDrawable *group1;
GimpLayer *float_layer;
/* Setup */
GIMP_TEST_START("gimp_image_new()")
img = gimp_image_new (32, 32, GIMP_RGB);
GIMP_TEST_END(GIMP_IS_IMAGE (img))
layer1 = GIMP_DRAWABLE (gimp_layer_new (img, "layer1", 20, 10,
GIMP_RGBA_IMAGE, 100.0, GIMP_LAYER_MODE_NORMAL));
layer2 = GIMP_DRAWABLE (gimp_layer_new (img, "layer2", 10, 20,
GIMP_RGBA_IMAGE, 100.0, GIMP_LAYER_MODE_NORMAL));
group1 = GIMP_DRAWABLE (gimp_layer_group_new (img));
GIMP_TEST_START("insert layer")
GIMP_TEST_END(gimp_image_insert_layer (img, GIMP_LAYER (layer1), NULL, 0))
GIMP_TEST_START("insert group layer")
GIMP_TEST_END(gimp_image_insert_layer (img, GIMP_LAYER (group1), NULL, -1))
GIMP_TEST_START("insert layer inside group")
GIMP_TEST_END(gimp_image_insert_layer (img, GIMP_LAYER (layer2),
GIMP_LAYER (group1), -1))
/* Floating selection tests */
/* 1. Fail with no selection */
GIMP_TEST_START("Gimp.Selection.float - no selection")
GIMP_TEST_END(gimp_selection_float (img, 1, &layer1, 10, 10) == NULL)
/* 2. Fail on a group layer */
GIMP_TEST_START("Gimp.Selection.float - group layer")
GIMP_TEST_END(gimp_selection_float (img, 1, &group1, 10, 10) == NULL)
/* 3. Succeed on a normal layer */
gimp_image_select_rectangle (img, GIMP_CHANNEL_OP_REPLACE, 5, 5, 20, 20);
GIMP_TEST_START("gimp_selection_float - normal layer")
float_layer = gimp_selection_float (img, 1, &layer1, 10, 10);
GIMP_TEST_END(GIMP_IS_LAYER (float_layer))
GIMP_TEST_START("gimp_floating_sel_remove")
GIMP_TEST_END(gimp_floating_sel_remove (float_layer) == TRUE);
/* 4. Succeed on a layer inside a group */
gimp_image_select_rectangle (img, GIMP_CHANNEL_OP_REPLACE, 5, 5, 20, 20);
GIMP_TEST_START("gimp_selection_float - layer inside group")
float_layer = gimp_selection_float (img, 1, &layer2, 10, 10);
GIMP_TEST_END(GIMP_IS_LAYER (float_layer))
GIMP_TEST_START("gimp_floating_sel_remove")
GIMP_TEST_END(gimp_floating_sel_remove (float_layer) == TRUE);
/* Teardown */
gimp_image_delete (img);
GIMP_TEST_RETURN
}

View File

@ -0,0 +1,53 @@
#!/usr/bin/env python3
image = Gimp.Image.new(32,32,Gimp.ImageBaseType.RGB)
gimp_assert('Gimp.Image.new', image is not None)
layer1 = Gimp.Layer.new(image, "layer1", 20, 10,
Gimp.ImageType.RGBA_IMAGE, 100.0,
Gimp.LayerMode.NORMAL)
image.insert_layer(layer1,None,0)
group1 = Gimp.Layer.group_new(image)
image.insert_layer(group1,None,-1)
layer2 = Gimp.Layer.new(image, "layer2", 10, 20,
Gimp.ImageType.RGBA_IMAGE, 100.0,
Gimp.LayerMode.NORMAL)
gimp_assert('insert layer inside group', image.insert_layer(layer2,group1,-1) == True)
# Make floating selection
# 1. Fail with no selection
gimp_assert('Gimp.Selection.float - no selection',
Gimp.Selection.float(image,[layer1],10,10) is None)
# 2. Fail on a group layer
gimp_assert('Gimp.Selection.float - group layer',
Gimp.Selection.float(image,[group1],10,10) is None)
# Create a selection
image.select_rectangle(Gimp.ChannelOps.REPLACE, 5, 5, 20, 20)
# 3. Succeed on a normal layer
gimp_assert('take selected layers: layer1',
image.take_selected_layers([layer1]) is True)
float1 = Gimp.Selection.float(image,[layer1],10,10)
gimp_assert('Gimp.Selection.float - normal layer',
float1 is not None)
gimp_assert('Remove float1 layer from image',
Gimp.floating_sel_remove(float1) is True)
# Create a selection
image.select_rectangle(Gimp.ChannelOps.REPLACE, 5, 5, 20, 20)
# 4. Succeed on a layer inside a group
gimp_assert('take selected layers: layer2',
image.take_selected_layers([layer2]) is True)
sel_drawables = image.get_selected_drawables()
gimp_assert('selected drawables', sel_drawables is not None)
float2 = Gimp.Selection.float(image,sel_drawables,10,10)
gimp_assert('Gimp.Selection.float - layer inside group', float2 is not None)
gimp_assert('Remove float2 layer from image',
Gimp.floating_sel_remove(float2) is True)

View File

@ -214,7 +214,7 @@ HELP
{
if (! gimp_pdb_item_is_attached (GIMP_ITEM (drawables[i]), NULL,
GIMP_PDB_ITEM_CONTENT, error) ||
gimp_pdb_item_is_group (GIMP_ITEM (drawables[i]), error) ||
! gimp_pdb_item_is_not_group (GIMP_ITEM (drawables[i]), error) ||
(image && image != gimp_item_get_image (GIMP_ITEM (drawables[i]))))
{
success = FALSE;