Skip to content

Commit

Permalink
Fix assertion error with large canvases and toBuffer('raw')
Browse files Browse the repository at this point in the history
Also allows larger buffers to be returned on 64-bit platforms. Nan only allows 0x3FFFFFFF; Node/v8 allows 0x7FFFFFFF on 64-bit.

Fixes #1158
  • Loading branch information
zbjornson committed May 31, 2019
1 parent 9d71cfd commit c1cc378
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,11 +8,13 @@ project adheres to [Semantic Versioning](http://semver.org/).
(Unreleased)
==================
### Changed
* Allow larger buffers to be returned from `toBuffer('raw')`.
### Added
### Fixed
* Fix crash when changing canvas width/height while `fillStyle` or `strokeStyle`
was set to a `CanvasPattern` or `CanvasGradient` (#1357).
* Fix crash when changing width/height of SVG canvases (#1380).
* Fix crash when using `toBuffer('raw')` with large canvases (#1158).

2.5.0
==================
Expand Down
8 changes: 7 additions & 1 deletion src/Canvas.cc
Expand Up @@ -20,6 +20,7 @@
#include <unordered_set>
#include "Util.h"
#include <vector>
#include "node_buffer.h"

#ifdef HAVE_JPEG
#include "JPEGStream.h"
Expand Down Expand Up @@ -413,8 +414,13 @@ NAN_METHOD(Canvas::ToBuffer) {
if (info[0]->StrictEquals(Nan::New<String>("raw").ToLocalChecked())) {
cairo_surface_t *surface = canvas->surface();
cairo_surface_flush(surface);
if (canvas->nBytes() > node::Buffer::kMaxLength) {
Nan::ThrowError("Data exceeds maximum buffer length.");
return;
}
const unsigned char *data = cairo_image_surface_get_data(surface);
Local<Object> buf = Nan::CopyBuffer(reinterpret_cast<const char*>(data), canvas->nBytes()).ToLocalChecked();
Isolate* iso = Nan::GetCurrentContext()->GetIsolate();
Local<Object> buf = node::Buffer::Copy(iso, reinterpret_cast<const char*>(data), canvas->nBytes()).ToLocalChecked();
info.GetReturnValue().Set(buf);
return;
}
Expand Down

0 comments on commit c1cc378

Please sign in to comment.