From a2e10e61413a0d158174a7a869c16aa13e5d3575 Mon Sep 17 00:00:00 2001 From: Fred Cox Date: Thu, 18 Jul 2024 12:47:28 +0100 Subject: [PATCH] Throw surface errors in toBuffer --- CHANGELOG.md | 1 + src/Canvas.cc | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ca6def96..e253d1e15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ This release notably changes to using N-API. 🎉 * Changed PNG consts to static properties of Canvas class ### Added * Added string tags to support class detection +* Throw Cairo errors in canvas.toBuffer() ### Fixed * Fix a case of use-after-free. (#2229) * Fix usage of garbage value by filling the allocated memory entirely with zeros if it's not modified. (#2229) diff --git a/src/Canvas.cc b/src/Canvas.cc index ee79915be..6ba312008 100644 --- a/src/Canvas.cc +++ b/src/Canvas.cc @@ -382,7 +382,15 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) { closure = static_cast(backend())->closure(); } - cairo_surface_finish(surface()); + cairo_surface_t *surf = surface(); + cairo_surface_finish(surf); + + cairo_status_t status = cairo_surface_status(surf); + if (status != CAIRO_STATUS_SUCCESS) { + Napi::Error::New(env, cairo_status_to_string(status)).ThrowAsJavaScriptException(); + return env.Undefined(); + } + return Napi::Buffer::Copy(env, &closure->vec[0], closure->vec.size()); }