8000 Fix HEIF image handling with uninitialized library by YakoYakoYokuYoku · Pull Request #895 · libgd/libgd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix HEIF image handling with uninitialized library #895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/gd.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ typedef const char *gdHeifChroma;
#define GD_HEIF_CHROMA_422 "422"
#define GD_HEIF_CHROMA_444 "444"

BGD_DECLARE(int) gdHeifInit();

BGD_DECLARE(void) gdHeifDeinit();

/* define struct with name and func ptr and add it to gdImageStruct gdInterpolationMethod interpolation; */

/* Interpolation function ptr */
Expand Down
49 changes: 49 additions & 0 deletions src/gd_heif.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,52 @@
#define GD_HEIF_ALLOC_STEP (4*1024)
#define GD_HEIF_HEADER 12

static BGD_THREAD_LOCAL int heifInit = 0;

typedef enum gd_heif_brand {
GD_HEIF_BRAND_AVIF = 1,
GD_HEIF_BRAND_MIF1 = 2,
GD_HEIF_BRAND_HEIC = 4,
GD_HEIF_BRAND_HEIX = 8,
} gd_heif_brand;

/*
Function: gdHeifInit

Initialize libheif, no need to call manually because other HEIF
functions do so.

Returns:

On success 0 is returned, otherwise 1 o 10000 n error.
*/
BGD_DECLARE(int) gdHeifInit()
{
if (heifInit)
return 0;
struct heif_error err = heif_init(NULL);
if (err.code != heif_error_Ok)
return 1;
heifInit = 1;
return 0;
}

/*
Function: gdHeifInit

Deinitialize libheif, call this whenever is not longer needed to
handle HEIF images.
*/
BGD_DECLARE(void) gdHeifDeinit()
{
if (!heifInit) {
gd_error("gd-heif invalid library deinitialization");
return;
}
heif_deinit();
heifInit = 0;
}

/*
Function: gdImageCreateFromHeif

Expand Down Expand Up @@ -135,6 +174,11 @@ static gdImagePtr _gdImageCreateFromHeifCtx(gdIOCtx *infile, gd_heif_brand expec
}
gdSeek(infile, 0);

if (gdHeifInit()) {
gd_error("gd-heif failure to initialize library");
return GD_FALSE;
}

while (n == GD_HEIF_ALLOC_STEP) {
temp = gdRealloc(filedata, size + GD_HEIF_ALLOC_STEP);
if (temp) {
Expand Down Expand Up @@ -301,6 +345,11 @@ static int _gdImageHeifCtx(gdImagePtr im, gdIOCtx *outfile, int quality, gdHeifC
return GD_FALSE;
}

if (gdHeifInit()) {
gd_error("Failure to initialize heif library");
return GD_FALSE;
}

heif_ctx = heif_context_alloc();
if (heif_ctx == NULL) {
gd_error("gd-heif could not allocate context\n");
Expand Down
18 changes: 18 additions & 0 deletions src/gdhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ extern "C" {
#include <stdlib.h>
#endif /* _WIN32_WCE */

#if defined(_WIN32) || defined(CYGWIN) || defined(_WIN32_WCE)
# ifdef __GNUC__
# if (__STDC_VERSION__ >= 201112L)
# define BGD_THREAD_LOCAL _Thread_local
# else
# define BGD_THREAD_LOCAL __thread
# endif
# else
# define BGD_THREAD_LOCAL __declspec(thread)
# endif
#else
# if (__STDC_VERSION__ >= 201112L)
# define BGD_THREAD_LOCAL _Thread_local
# else
# define BGD_THREAD_LOCAL __thread
# endif
#endif

/* TBB: strtok_r is not universal; provide an implementation of it. */

char *gd_strtok_r(char *s, const char *sep, char **state);
Expand Down
3 changes: 1 addition & 2 deletions tests/heif/heif_read.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* Simple test case that confirms the failure of using `gdImageCreateFromHeif`
* with a NULL pointer.
* Simple test case that checks reading images using `gdImageCreateFromHeif`.
*/


Expand Down
Loading
0