@@ -115,29 +115,31 @@ bool jpg2bmp(const uint8_t *src, size_t src_len, uint8_t ** out, size_t * out_le
115
115
.advanced .working_buffer = work ,
116
116
.advanced .working_buffer_size = sizeof (work ),
117
117
};
118
-
118
+
119
+ bool ret = false;
120
+ uint8_t * output = NULL ;
119
121
esp_jpeg_image_output_t output_img = {};
120
122
if (esp_jpeg_get_image_info (& jpeg_cfg , & output_img ) != ESP_OK ) {
121
123
ESP_LOGE (TAG , "Failed to get image info" );
122
- return false ;
124
+ goto fail ;
123
125
}
124
-
126
+
125
127
// @todo here we allocate memory and we assume that the user will free it
126
128
// this is not the best way to do it, but we need to keep the API
127
129
// compatible with the previous version
128
130
const size_t output_size = output_img .output_len + BMP_HEADER_LEN ;
129
- uint8_t * output = _malloc (output_size );
131
+ output = _malloc (output_size );
130
132
if (!output ) {
131
133
ESP_LOGE (TAG , "Failed to allocate output buffer" );
132
- return false ;
134
+ goto fail ;
133
135
}
134
136
135
137
// Start writing decoded data after the BMP header
136
138
jpeg_cfg .outbuf = output + BMP_HEADER_LEN ;
137
139
jpeg_cfg .outbuf_size = output_img .output_len ;
138
140
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 ;
141
143
}
142
144
143
145
output [0 ] = 'B' ;
@@ -160,8 +162,13 @@ bool jpg2bmp(const uint8_t *src, size_t src_len, uint8_t ** out, size_t * out_le
160
162
161
163
* out = output ;
162
164
* out_len = output_size ;
165
+ ret = true;
163
166
164
- return true;
167
+ fail :
168
+ if (!ret && output ) {
169
+ free (output );
170
+ }
171
+ return ret ;
165
172
}
166
173
167
174
bool fmt2rgb888 (const uint8_t * src_buf , size_t src_len , pixformat_t format , uint8_t * rgb_buf )
0 commit comments