Skip to content

Commit

Permalink
fix: producer must not replace placeholder in source string
Browse files Browse the repository at this point in the history
  • Loading branch information
faulpeltz committed Sep 4, 2024
1 parent 30af1d8 commit 9944d4a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,34 @@ function discoverPlaceholder(
binaryBuffer: Buffer,
searchString: string,
padder: string,
searchOffset: number = 0,
): Placeholder | NotFound {
const placeholder = Buffer.from(searchString);
const position = binaryBuffer.indexOf(placeholder);
const position = binaryBuffer.indexOf(placeholder, searchOffset);

if (position === -1) {
return { notFound: true };
}

/**
* the PAYLOAD/PRELUDE placeholders occur twice in the binaries:
* - in source text as a string literal
* - in bytecode as a raw string
* the ordering depends on the platform - we need to make sure that
* the bytecode string is replaced, not the source literal.
*
* this rejects the source code literal if it occurs first in the binary
* also see: https://github.com/yao-pkg/pkg/pull/86
*/
if (binaryBuffer[position - 1] === 39 /* ascii for ' APOSTROPHE */) {
return discoverPlaceholder(
binaryBuffer,
searchString,
padder,
position + placeholder.length,
);
}

return { position, size: placeholder.length, padder };
}

Expand Down

0 comments on commit 9944d4a

Please sign in to comment.