Skip to content

Commit

Permalink
PSD: Fix memory leak on error
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed Oct 31, 2023
1 parent ce1b138 commit a0a24df
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/sail-codecs/psd/psd.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct psd_state {
enum SailPsdCompression compression;
unsigned bytes_per_channel;
unsigned char *scan_buffer;
struct sail_palette *palette;
};

static sail_status_t alloc_psd_state(struct psd_state **psd_state) {
Expand All @@ -67,6 +68,7 @@ static sail_status_t alloc_psd_state(struct psd_state **psd_state) {
(*psd_state)->compression = SAIL_PSD_COMPRESSION_NONE;
(*psd_state)->bytes_per_channel = 0;
(*psd_state)->scan_buffer = NULL;
(*psd_state)->palette = NULL;

return SAIL_OK;
}
Expand All @@ -82,6 +84,8 @@ static void destroy_psd_state(struct psd_state *psd_state) {
sail_destroy_load_options(psd_state->load_options);
sail_destroy_save_options(psd_state->save_options);

sail_destroy_palette(psd_state->palette);

sail_free(psd_state);
}

Expand Down Expand Up @@ -155,27 +159,25 @@ SAIL_EXPORT sail_status_t sail_codec_load_seek_next_frame_v8_psd(void *state, st
SAIL_TRY(psd_private_get_big_endian_uint32_t(psd_state->io, &data_size));

/* Palette. */
struct sail_palette *palette = NULL;

if (data_size > 0) {
SAIL_LOG_TRACE("PSD: Palette data size: %u", data_size);
SAIL_TRY(sail_alloc_palette_for_data(SAIL_PIXEL_FORMAT_BPP24_RGB, 256, &palette));
SAIL_TRY(sail_alloc_palette_for_data(SAIL_PIXEL_FORMAT_BPP24_RGB, 256, &psd_state->palette));

/* Merge RR GG BB... to RGB RGB... */
unsigned char buf[256*3];
SAIL_TRY(psd_state->io->strict_read(psd_state->io->stream, buf, sizeof(buf)));

unsigned char *palette_data = palette->data;
unsigned char *palette_data = psd_state->palette->data;

for (unsigned i = 0; i < 256; i++) {
for (unsigned channel = 0; channel < 3; channel++) {
*palette_data++ = buf[256 * channel + i];
}
}
} else if (mode == SAIL_PSD_MODE_BITMAP) {
SAIL_TRY(sail_alloc_palette_for_data(SAIL_PIXEL_FORMAT_BPP24_RGB, 2, &palette));
SAIL_TRY(sail_alloc_palette_for_data(SAIL_PIXEL_FORMAT_BPP24_RGB, 2, &psd_state->palette));

unsigned char *palette_data = palette->data;
unsigned char *palette_data = psd_state->palette->data;

*palette_data++ = 255;
*palette_data++ = 255;
Expand Down Expand Up @@ -238,9 +240,12 @@ SAIL_EXPORT sail_status_t sail_codec_load_seek_next_frame_v8_psd(void *state, st
image_local->width = width;
image_local->height = height;
image_local->pixel_format = pixel_format;
image_local->palette = palette;
image_local->palette = psd_state->palette;
image_local->bytes_per_line = sail_bytes_per_line(image_local->width, image_local->pixel_format);

/* Palette has been moved. */
psd_state->palette = NULL;

*image = image_local;

return SAIL_OK;
Expand Down

0 comments on commit a0a24df

Please sign in to comment.