diff --git a/CHANGELOG.md b/CHANGELOG.md index a42a9275..16ad5e90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed +- Fixes hard crash to exception when creating a stream with null object and calling createWriter with it - Fixes missing buffer information for recrypt typescript definition ## [2.5.0] - 2022-06-23 diff --git a/src/ObjectByteWriterWithPosition.cpp b/src/ObjectByteWriterWithPosition.cpp index 231d7ca0..1e8978fe 100644 --- a/src/ObjectByteWriterWithPosition.cpp +++ b/src/ObjectByteWriterWithPosition.cpp @@ -52,12 +52,20 @@ IOBasicTypes::LongBufferSizeType ObjectByteWriterWithPosition::Write(const IOBas Local args[1]; args[0] = anArray; - - Local result = func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args).ToLocalChecked(); + MaybeLocal maybe; + TryCatch try_catch(Isolate::GetCurrent()); + + maybe = func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args); + Local result; + + if (!maybe.ToLocal(&result)) { + try_catch.ReThrow(); + return 0; + } if(result.IsEmpty()) { - THROW_EXCEPTION("wrong return value. it's empty. return the number of written characters"); - return 0; + THROW_EXCEPTION("wrong return value. it's empty. return the number of written characters"); + return 0; } else if(result->IsNumber()) { @@ -65,8 +73,8 @@ IOBasicTypes::LongBufferSizeType ObjectByteWriterWithPosition::Write(const IOBas } else { - THROW_EXCEPTION("wrong return value. write should return the number of written characters"); - return 0; + THROW_EXCEPTION("wrong return value. write should return the number of written characters"); + return 0; } } @@ -81,4 +89,4 @@ IOBasicTypes::LongFilePositionType ObjectByteWriterWithPosition::GetCurrentPosit Local func = Local::Cast(value); return TO_NUMBER(func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 0, NULL).ToLocalChecked())->Value(); -} \ No newline at end of file +} diff --git a/tests/BasicModificationWithStreams.js b/tests/BasicModificationWithStreams.js index 89719798..67a2c0c6 100644 --- a/tests/BasicModificationWithStreams.js +++ b/tests/BasicModificationWithStreams.js @@ -1,4 +1,5 @@ var muhammara = require('../muhammara'); +const chai = require('chai'); describe('BasicModificationWithStreams', function() { it('should complete without error', function() { @@ -14,9 +15,15 @@ describe('BasicModificationWithStreams', function() { color:0x00 }); pageModifier.endContext().writePage(); - pdfWriter.end(); outStream.close(); inStream.close(); }); + + it('null for stream should throw an error and not crash', function () { + var res = new muhammara.PDFStreamForResponse(null) + chai.expect( + muhammara.createWriter.bind(undefined, res) + ).to.throw('Cannot read properties of null') + }) });