Skip to content

Commit 857281a

Browse files
committed
Fix potential memory leak if shrink-on-load re-open fails
1 parent b328108 commit 857281a

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎docs/src/content/docs/changelog/v0.35.6.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ https://github.com/lovell/sharp-libvips/releases/tag/v1.3.4
1111

1212
* Fix potential memory leak when extending animated images.
1313

14+
* Fix potential memory leak when shrink-on-load fails to re-open a file.
15+
1416
* Improve `pipelineColourspace` ICC profile colour management.
1517
[#4596](https://github.com/lovell/sharp/pull/4596)
1618
[@mertalev](https://github.com/mertalev)

‎src/pipeline.cc‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,12 @@ class PipelineWorker : public Napi::AsyncWorker {
270270
if (baton->input->buffer != nullptr) {
271271
// Reload JPEG buffer
272272
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
273-
image = VImage::jpegload_buffer(blob, option);
273+
try {
274+
image = VImage::jpegload_buffer(blob, option);
275+
} catch (...) {
276+
vips_area_unref(reinterpret_cast<VipsArea*>(blob));
277+
throw;
278+
}
274279
vips_area_unref(reinterpret_cast<VipsArea*>(blob));
275280
} else {
276281
// Reload JPEG file
@@ -282,7 +287,12 @@ class PipelineWorker : public Napi::AsyncWorker {
282287
if (baton->input->buffer != nullptr) {
283288
// Reload WebP buffer
284289
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
285-
image = VImage::webpload_buffer(blob, option);
290+
try {
291+
image = VImage::webpload_buffer(blob, option);
292+
} catch (...) {
293+
vips_area_unref(reinterpret_cast<VipsArea*>(blob));
294+
throw;
295+
}
286296
vips_area_unref(reinterpret_cast<VipsArea*>(blob));
287297
} else {
288298
// Reload WebP file
@@ -292,7 +302,12 @@ class PipelineWorker : public Napi::AsyncWorker {
292302
if (baton->input->buffer != nullptr) {
293303
// Reload SVG buffer
294304
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
295-
image = VImage::svgload_buffer(blob, option);
305+
try {
306+
image = VImage::svgload_buffer(blob, option);
307+
} catch (...) {
308+
vips_area_unref(reinterpret_cast<VipsArea*>(blob));
309+
throw;
310+
}
296311
vips_area_unref(reinterpret_cast<VipsArea*>(blob));
297312
} else {
298313
// Reload SVG file
@@ -306,7 +321,12 @@ class PipelineWorker : public Napi::AsyncWorker {
306321
if (baton->input->buffer != nullptr) {
307322
// Reload PDF buffer
308323
VipsBlob *blob = vips_blob_new(nullptr, baton->input->buffer, baton->input->bufferLength);
309-
image = VImage::pdfload_buffer(blob, option);
324+
try {
325+
image = VImage::pdfload_buffer(blob, option);
326+
} catch (...) {
327+
vips_area_unref(reinterpret_cast<VipsArea*>(blob));
328+
throw;
329+
}
310330
vips_area_unref(reinterpret_cast<VipsArea*>(blob));
311331
} else {
312332
// Reload PDF file

0 commit comments

Comments
 (0)