Skip to content

Commit d9b28c3

Browse files
authored
fix: wrong placeholder replaced in windows binary (#86)
1 parent 30af1d8 commit d9b28c3

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

lib/producer.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,37 @@ 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 can 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+
const nextPlaceholder = discoverPlaceholder(
61+
binaryBuffer,
62+
searchString,
63+
padder,
64+
position + placeholder.length,
65+
);
66+
if (!('notFound' in nextPlaceholder)) {
67+
return nextPlaceholder;
68+
}
69+
}
70+
4871
return { position, size: placeholder.length, padder };
4972
}
5073

0 commit comments

Comments
 (0)