Skip to content

Commit c1cc378

Browse files
committed
Fix assertion error with large canvases and toBuffer('raw')
Also allows larger buffers to be returned on 64-bit platforms. Nan only allows 0x3FFFFFFF; Node/v8 allows 0x7FFFFFFF on 64-bit. Fixes #1158
1 parent 9d71cfd commit c1cc378

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ project adheres to [Semantic Versioning](http://semver.org/).
88
(Unreleased)
99
==================
1010
### Changed
11+
* Allow larger buffers to be returned from `toBuffer('raw')`.
1112
### Added
1213
### Fixed
1314
* Fix crash when changing canvas width/height while `fillStyle` or `strokeStyle`
1415
was set to a `CanvasPattern` or `CanvasGradient` (#1357).
1516
* Fix crash when changing width/height of SVG canvases (#1380).
17+
* Fix crash when using `toBuffer('raw')` with large canvases (#1158).
1618

1719
2.5.0
1820
==================

src/Canvas.cc

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <unordered_set>
2121
#include "Util.h"
2222
#include <vector>
23+
#include "node_buffer.h"
2324

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

0 commit comments

Comments
 (0)