Skip to content

Commit

Permalink
return nullptr for a few API functions in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 22, 2024
1 parent 3e82934 commit f66ebcc
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
6 changes: 3 additions & 3 deletions examples/heif_dec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ int decode_single_image(heif_image_handle* handle,
assert(nDepthImages == 1);
(void) nDepthImages;

struct heif_image_handle* depth_handle;
struct heif_image_handle* depth_handle = nullptr;
err = heif_image_handle_get_depth_image_handle(handle, depth_id, &depth_handle);
if (err.code) {
std::cerr << "Could not read depth channel\n";
Expand Down Expand Up @@ -339,7 +339,7 @@ int decode_single_image(heif_image_handle* handle,

for (heif_item_id auxId : auxIDs) {

struct heif_image_handle* aux_handle;
struct heif_image_handle* aux_handle = nullptr;
err = heif_image_handle_get_auxiliary_image_handle(handle, auxId, &aux_handle);
if (err.code) {
std::cerr << "Could not read auxiliary image\n";
Expand All @@ -348,7 +348,7 @@ int decode_single_image(heif_image_handle* handle,

int aux_bit_depth = heif_image_handle_get_luma_bits_per_pixel(aux_handle);

struct heif_image* aux_image;
struct heif_image* aux_image = nullptr;
err = heif_decode_image(aux_handle,
&aux_image,
encoder->colorspace(false),
Expand Down
7 changes: 5 additions & 2 deletions examples/heif_enc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ heif_error create_output_nclx_profile_and_configure_encoder(heif_encoder* encode
}
}
else {
heif_color_profile_nclx* input_nclx;
heif_color_profile_nclx* input_nclx = nullptr;

heif_error error = heif_image_get_nclx_color_profile(input_image.get(), &input_nclx);
if (error.code == heif_error_Color_profile_does_not_exist) {
Expand All @@ -588,8 +588,11 @@ heif_error create_output_nclx_profile_and_configure_encoder(heif_encoder* encode
nclx->full_range_flag = input_nclx->full_range_flag;

heif_nclx_color_profile_free(input_nclx);
input_nclx = nullptr;
}

assert(!input_nclx);

// TODO: this assumes that the encoder plugin has a 'chroma' parameter. Currently, they do, but there should be a better way to set this.
switch (heif_image_get_chroma_format(input_image.get())) {
case heif_chroma_420:
Expand Down Expand Up @@ -731,7 +734,7 @@ heif_image_handle* encode_tiled(heif_context* ctx, heif_encoder* encoder, heif_e
const input_tiles_generator& tile_generator,
const heif_image_tiling& tiling)
{
heif_image_handle* tiled_image;
heif_image_handle* tiled_image = nullptr;


// --- create the main grid image
Expand Down
1 change: 1 addition & 0 deletions heifio/encoder_y4m.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ bool Y4MEncoder::Encode(const struct heif_image_handle* handle,
int ch = heif_image_get_height(image, heif_channel_Cb);

if (yw < 0 || cw < 0) {
fclose(fp);
return false;
}

Expand Down
27 changes: 27 additions & 0 deletions libheif/api/libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,8 @@ struct heif_error heif_image_handle_get_auxiliary_type(const struct heif_image_h
heif_suberror_Null_pointer_argument).error_struct(handle->image.get());
}

*out_type = nullptr;

auto auxType = handle->image->get_aux_type();

char* buf = (char*) malloc(auxType.length() + 1);
Expand Down Expand Up @@ -865,6 +867,8 @@ struct heif_error heif_image_handle_get_auxiliary_image_handle(const struct heif
heif_suberror_Null_pointer_argument).error_struct(main_image_handle->image.get());
}

*out_auxiliary_handle = nullptr;

auto auxImages = main_image_handle->image->get_aux_images();
for (const auto& aux : auxImages) {
if (aux->get_id() == auxiliary_id) {
Expand Down Expand Up @@ -1285,6 +1289,12 @@ struct heif_error heif_image_handle_get_depth_image_handle(const struct heif_ima
heif_item_id depth_id,
struct heif_image_handle** out_depth_handle)
{
if (out_depth_handle == nullptr) {
return {heif_error_Usage_error,
heif_suberror_Null_pointer_argument,
"NULL out_depth_handle passed to heif_image_handle_get_depth_image_handle()"};
}

auto depth_image = handle->image->get_depth_channel();

if (depth_image->get_id() != depth_id) {
Expand Down Expand Up @@ -1411,6 +1421,14 @@ struct heif_error heif_decode_image(const struct heif_image_handle* in_handle,
heif_chroma chroma,
const struct heif_decoding_options* input_options)
{
if (out_img == nullptr) {
return {heif_error_Usage_error,
heif_suberror_Null_pointer_argument,
"NULL out_img passed to heif_decode_image()"};
}

*out_img = nullptr;

heif_item_id id = in_handle->image->get_id();

heif_decoding_options dec_options = normalize_options(input_options);
Expand Down Expand Up @@ -2867,6 +2885,7 @@ struct heif_error heif_context_get_encoder_for_format(struct heif_context* conte
return (*encoder)->alloc();
}
else {
*encoder = nullptr;
Error err(heif_error_Unsupported_filetype, // TODO: is this the right error code?
heif_suberror_Unspecified);
return err.error_struct(context ? context->context.get() : nullptr);
Expand Down Expand Up @@ -3371,6 +3390,10 @@ struct heif_error heif_context_encode_image(struct heif_context* ctx,
heif_suberror_Null_pointer_argument).error_struct(ctx->context.get());
}

if (out_image_handle) {
*out_image_handle = nullptr;
}

heif_encoding_options options;
heif_color_profile_nclx nclx;
set_default_options(options);
Expand Down Expand Up @@ -3586,6 +3609,10 @@ struct heif_error heif_context_add_tiled_image(struct heif_context* ctx,
const struct heif_encoder* encoder,
struct heif_image_handle** out_grid_image_handle)
{
if (out_grid_image_handle) {
*out_grid_image_handle = nullptr;
}

Result<std::shared_ptr<ImageItem_Tiled>> gridImageResult;
gridImageResult = ImageItem_Tiled::add_new_tiled_item(ctx->context.get(), parameters, encoder);

Expand Down
1 change: 1 addition & 0 deletions libheif/image-items/tiled.cc
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ Error ImageItem_Tiled::add_image_tile(uint32_t tile_x, uint32_t tile_y,

Result<std::shared_ptr<HeifPixelImage>> colorConversionResult = item->convert_colorspace_for_encoding(image, encoder, *options);
if (colorConversionResult.error) {
heif_encoding_options_free(options);
return colorConversionResult.error;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct heif_image* createImage_RRGGBB_BE() {
struct heif_error encode_image(struct heif_image* img) {
struct heif_context* ctx = heif_context_alloc();

struct heif_encoder* enc;
struct heif_encoder* enc = nullptr;
struct heif_error err { heif_error_Ok };

err = heif_context_get_encoder_for_format(ctx,
Expand Down
3 changes: 3 additions & 0 deletions tests/uncompressed_decode_mono.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ TEST_CASE("check image size mono") {

void check_image_content_mono(struct heif_context *&context) {
heif_image_handle *handle = get_primary_image_handle(context);
REQUIRE(handle != nullptr);
heif_image *img = get_primary_image_mono(handle);

int stride;
Expand Down Expand Up @@ -183,6 +184,8 @@ void check_image_content_mono(struct heif_context *&context) {
REQUIRE(((int)(img_plane[stride * row + 28])) == 0);
REQUIRE(((int)(img_plane[stride * row + 29])) == 0);
}

heif_image_handle_release(handle);
}

TEST_CASE("check image content mono") {
Expand Down

0 comments on commit f66ebcc

Please sign in to comment.