Skip to content

Commit 0c72514

Browse files
committed
jpg2bmp: unified rework error handling
Consolidated all error handling in jpg2bmp into a single fail path so memory is freed reliably
1 parent 225a4db commit 0c72514

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

conversions/to_bmp.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,31 @@ bool jpg2bmp(const uint8_t *src, size_t src_len, uint8_t ** out, size_t * out_le
115115
.advanced.working_buffer = work,
116116
.advanced.working_buffer_size = sizeof(work),
117117
};
118-
118+
119+
bool ret = false;
120+
uint8_t *output = NULL;
119121
esp_jpeg_image_output_t output_img = {};
120122
if (esp_jpeg_get_image_info(&jpeg_cfg, &output_img) != ESP_OK) {
121123
ESP_LOGE(TAG, "Failed to get image info");
122-
return false;
124+
goto fail;
123125
}
124-
126+
125127
// @todo here we allocate memory and we assume that the user will free it
126128
// this is not the best way to do it, but we need to keep the API
127129
// compatible with the previous version
128130
const size_t output_size = output_img.output_len + BMP_HEADER_LEN;
129-
uint8_t *output = _malloc(output_size);
131+
output = _malloc(output_size);
130132
if (!output) {
131133
ESP_LOGE(TAG, "Failed to allocate output buffer");
132-
return false;
134+
goto fail;
133135
}
134136

135137
// Start writing decoded data after the BMP header
136138
jpeg_cfg.outbuf = output + BMP_HEADER_LEN;
137139
jpeg_cfg.outbuf_size = output_img.output_len;
138140
if(esp_jpeg_decode(&jpeg_cfg, &output_img) != ESP_OK){
139-
free(output);
140-
return false;
141+
ESP_LOGE(TAG, "JPEG decode failed");
142+
goto fail;
141143
}
142144

143145
output[0] = 'B';
@@ -160,8 +162,13 @@ bool jpg2bmp(const uint8_t *src, size_t src_len, uint8_t ** out, size_t * out_le
160162

161163
*out = output;
162164
*out_len = output_size;
165+
ret = true;
163166

164-
return true;
167+
fail:
168+
if (!ret && output) {
169+
free(output);
170+
}
171+
return ret;
165172
}
166173

167174
bool fmt2rgb888(const uint8_t *src_buf, size_t src_len, pixformat_t format, uint8_t * rgb_buf)

0 commit comments

Comments
 (0)