Skip to content

Commit 9944d4a

Browse files
committed
fix: producer must not replace placeholder in source string
1 parent 30af1d8 commit 9944d4a

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

lib/producer.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,34 @@ function discoverPlaceholder(
3737
binaryBuffer: Buffer,
3838
searchString: string,
3939
padder: string,
40+
searchOffset: number = 0,
4041
): Placeholder | NotFound {
4142
const placeholder = Buffer.from(searchString);
42-
const position = binaryBuffer.indexOf(placeholder);
43+
const position = binaryBuffer.indexOf(placeholder, searchOffset);
4344

4445
if (position === -1) {
4546
return { notFound: true };
4647
}
4748

49+
/**
50+
* the PAYLOAD/PRELUDE placeholders occur twice in the binaries:
51+
* - in source text as a string literal
52+
* - in bytecode as a raw string
53+
* the ordering depends on the platform - we need to make sure that
54+
* the bytecode string is replaced, not the source literal.
55+
*
56+
* this rejects the source code literal if it occurs first in the binary
57+
* also see: https://github.com/yao-pkg/pkg/pull/86
58+
*/
59+
if (binaryBuffer[position - 1] === 39 /* ascii for ' APOSTROPHE */) {
60+
return discoverPlaceholder(
61+
binaryBuffer,
62+
searchString,
63+
padder,
64+
position + placeholder.length,
65+
);
66+
}
67+
4868
return { position, size: placeholder.length, padder };
4969
}
5070

0 commit comments

Comments
 (0)