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(xsnap): free netstring in issueCommand(); handle malloc() failure #3101

Merged
merged 4 commits into from
May 17, 2021
Merged
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
15 changes: 10 additions & 5 deletions packages/xsnap/src/xsnap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1389,10 +1389,12 @@ static int fxReadNetString(FILE *inStream, char** dest, size_t* len)
} else {
*(buf + *len) = 0;
}
if (code != 0) {
if (code == 0) {
*dest = buf;
} else {
*dest = 0;
free(buf);
}
*dest = buf;
}
return code;
}
Expand Down Expand Up @@ -1473,12 +1475,14 @@ static void fx_issueCommand(xsMachine *the)

size_t length;
char* buf = NULL;
txSlot* arrayBuffer = &xsArg(0);
length = fxGetArrayBufferLength(the, arrayBuffer);
length = xsGetArrayBufferLength(xsArg(0));

buf = malloc(length);
if (!buf) {
fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
}

fxGetArrayBufferData(the, arrayBuffer, 0, buf, length);
xsGetArrayBufferData(xsArg(0), 0, buf, length);
int writeError = fxWriteNetString(toParent, "?", buf, length);

free(buf);
Expand All @@ -1495,6 +1499,7 @@ static void fx_issueCommand(xsMachine *the)
}

xsResult = xsArrayBuffer(buf, len);
free(buf);
}


Expand Down
22 changes: 21 additions & 1 deletion packages/xsnap/test/test-xs-perf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import test from 'ava';
import * as childProcess from 'child_process';
import * as os from 'os';
import { xsnap } from '../src/xsnap';
import { options } from './test-xsnap';

const decoder = new TextDecoder();

const xsnapOptions = {
name: 'xsnap test worker',
spawn: childProcess.spawn,
os: os.type(),
stderr: 'inherit',
stdout: 'inherit',
};

export function options() {
const messages = [];
async function handleCommand(message) {
messages.push(decoder.decode(message));
return new Uint8Array();
}
return { ...xsnapOptions, handleCommand, messages };
}

test('meter details', async t => {
const opts = options();
Expand Down