Skip to content

Commit

Permalink
Fix createWriter cpp / crash / npe when using null as stream object (#…
Browse files Browse the repository at this point in the history
…190)

Fix createWriter cpp / crash / npe when using null as stream object

Fixes #188
  • Loading branch information
julianhille authored Jun 29, 2022
1 parent a7576b9 commit 0a6427e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions src/ObjectByteWriterWithPosition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,29 @@ IOBasicTypes::LongBufferSizeType ObjectByteWriterWithPosition::Write(const IOBas

Local<Value> args[1];
args[0] = anArray;

Local<Value> result = func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args).ToLocalChecked();
MaybeLocal<Value> maybe;
TryCatch try_catch(Isolate::GetCurrent());

maybe = func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args);
Local <Value> 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())
{
return TO_UINT32(result)->Value();
}
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;
}
}

Expand All @@ -81,4 +89,4 @@ IOBasicTypes::LongFilePositionType ObjectByteWriterWithPosition::GetCurrentPosit
Local<Function> func = Local<Function>::Cast(value);

return TO_NUMBER(func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 0, NULL).ToLocalChecked())->Value();
}
}
9 changes: 8 additions & 1 deletion tests/BasicModificationWithStreams.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var muhammara = require('../muhammara');
const chai = require('chai');

describe('BasicModificationWithStreams', function() {
it('should complete without error', function() {
Expand All @@ -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 propert.*(write)?.* of null/)
})
});

0 comments on commit 0a6427e

Please sign in to comment.