From d9b28c391bc7d94df2b95074b98ee43681c67b5a Mon Sep 17 00:00:00 2001 From: faulpeltz Date: Thu, 5 Sep 2024 17:06:29 +0200 Subject: [PATCH] fix: wrong placeholder replaced in windows binary (#86) --- lib/producer.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/producer.ts b/lib/producer.ts index 76759cfa..4f636ca3 100644 --- a/lib/producer.ts +++ b/lib/producer.ts @@ -37,14 +37,37 @@ 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 can 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 */) { + const nextPlaceholder = discoverPlaceholder( + binaryBuffer, + searchString, + padder, + position + placeholder.length, + ); + if (!('notFound' in nextPlaceholder)) { + return nextPlaceholder; + } + } + return { position, size: placeholder.length, padder }; }