Skip to content

Commit

Permalink
fix: make sure getBytes copies the buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez committed Sep 20, 2023
1 parent cab26c2 commit 4fd8864
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export abstract class ExtismPluginBase {
if (value === null) {
return;
}
plugin.vars[key] = new Uint8Array(value);
plugin.vars[key] = value;
},
extism_http_request(requestOffset: bigint, bodyOffset: bigint): bigint {
if (!plugin.supportsHttpRequests()) {
Expand Down Expand Up @@ -697,7 +697,7 @@ class Allocator {
}

/**
* Gets bytes from a specific memory offset. This returns a view of the underlying memory.
* Gets bytes from a specific memory offset.
* @param {bigint} offset - Memory offset.
* @returns {Uint8Array | null} Byte array or null if offset is zero.
*/
Expand All @@ -708,7 +708,10 @@ class Allocator {

const length = this.getLength(offset);

return new Uint8Array(this.getMemory().buffer, Number(offset), Number(length));
const buffer = new Uint8Array(this.getMemory().buffer, Number(offset), Number(length));

// Copy the buffer because `this.getMemory().buffer` returns a write-through view
return new Uint8Array(buffer);
}

/**
Expand All @@ -732,13 +735,8 @@ class Allocator {
*/
allocBytes(data: Uint8Array): bigint {
const offs = this.alloc(BigInt(data.length));
const bytes = this.getBytes(offs);
if (bytes === null) {
this.free(offs);
return BigInt(0);
}

bytes.set(data);
const buffer = new Uint8Array(this.getMemory().buffer, Number(offs), data.length);
buffer.set(data);
return offs;
}

Expand Down

0 comments on commit 4fd8864

Please sign in to comment.