Skip to content

Commit

Permalink
vips/image: use g_object_unref instead of g_clear_object (as noted by…
Browse files Browse the repository at this point in the history
… libvips documentation) (#219), add test case for checking return errors of a known corrupted JPEG (#374)

Co-authored-by: Gerdriaan Mulder <[email protected]>
  • Loading branch information
mrngm and Gerdriaan Mulder authored Oct 22, 2023
1 parent 00aefc3 commit 3e45ca9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Binary file added resources/jpg-corruption.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions vips/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
int has_alpha_channel(VipsImage *image) { return vips_image_hasalpha(image); }

void clear_image(VipsImage **image) {
// https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html#g-clear-object
if (G_IS_OBJECT(*image)) g_clear_object(image);
// Reference-counting in libvips: https://www.libvips.org/API/current/using-from-c.html#using-C-ref
// https://docs.gtk.org/gobject/method.Object.unref.html
if (G_IS_OBJECT(*image)) g_object_unref(*image);
}
14 changes: 14 additions & 0 deletions vips/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,20 @@ func TestImageRef_JP2K(t *testing.T) {
assert.Equal(t, 1, metadata.Pages)
}

func TestImageRef_CorruptedJPEG(t *testing.T) {
Startup(nil)

raw, err := ioutil.ReadFile(resources + "jpg-corruption.jpg")
require.NoError(t, err)

img, err := NewImageFromBuffer(raw)
require.NoError(t, err)
require.NotNil(t, img)

_, _, err = img.ExportJpeg(nil)
assert.Error(t, err, "VipsJpeg: Corrupt JPEG data: bad Huffman code")
}

// TODO unit tests to cover:
// NewImageFromReader failing test
// NewImageFromFile failing test
Expand Down

0 comments on commit 3e45ca9

Please sign in to comment.