Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 properties of null')
})
});