Skip to content

Commit

Permalink
Merge pull request #3101 from Agoric/2469-xsnap-fixes
Browse files Browse the repository at this point in the history
fix(xsnap): free netstring in issueCommand(); handle malloc() failure
  • Loading branch information
dckc authored May 17, 2021
2 parents 1995436 + 127e58a commit 8a03440
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
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

0 comments on commit 8a03440

Please sign in to comment.