From 778ed11f87770fbff7b6114e522d15456a42482f Mon Sep 17 00:00:00 2001 From: sanasol Date: Tue, 27 Jan 2026 05:24:43 +0100 Subject: [PATCH] fix: Revert null-padding - was corrupting adjacent data The null-fill before overwrite was likely corrupting data after the string, causing crashes. Reverted to develop behavior: only overwrite the bytes we need to change. Also re-enabled sentry patching. Co-Authored-By: Claude Opus 4.5 --- backend/utils/clientPatcher.js | 44 ++++++++++++++-------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/backend/utils/clientPatcher.js b/backend/utils/clientPatcher.js index 538aa62..e750d9b 100644 --- a/backend/utils/clientPatcher.js +++ b/backend/utils/clientPatcher.js @@ -141,8 +141,8 @@ class ClientPatcher { } /** - * Replace bytes in buffer with null-padding for shorter replacements - * When new pattern is shorter than old, pads with 0x00 to prevent leftover bytes + * Replace bytes in buffer - only overwrites the length of new bytes + * Does NOT null-pad to avoid corrupting adjacent data */ replaceBytes(buffer, oldBytes, newBytes) { let count = 0; @@ -156,10 +156,7 @@ class ClientPatcher { const positions = this.findAllOccurrences(result, oldBytes); for (const pos of positions) { - // Zero-fill the old region first if new is shorter (prevents leftover bytes) - if (newBytes.length < oldBytes.length) { - result.fill(0x00, pos, pos + oldBytes.length); - } + // Only overwrite the length of the new bytes - don't null-fill! newBytes.copy(result, pos); count++; } @@ -214,11 +211,7 @@ class ClientPatcher { const lastCharFirstByte = result[lastCharPos]; if (lastCharFirstByte === oldLastCharByte) { - // Zero-fill first if new is shorter - if (newUtf16NoLast.length < oldUtf16NoLast.length) { - result.fill(0x00, pos, pos + oldUtf16NoLast.length); - } - + // Only overwrite, don't null-fill newUtf16NoLast.copy(result, pos); result[lastCharPos] = newLastCharByte; count++; @@ -238,21 +231,20 @@ class ClientPatcher { console.log(` Patching strategy: ${strategy.description}`); - // 1. Patch telemetry/sentry URL - DISABLED: May cause crash on glibc 2.41+ - // The sentry string is near executable code and patching it may corrupt memory layout - // const oldSentry = 'https://ca900df42fcf57d4dd8401a86ddd7da2@sentry.hytale.com/2'; - // const newSentry = `${protocol}t@${domain}/2`; - // const sentryResult = this.replaceBytes( - // result, - // this.stringToLengthPrefixed(oldSentry), - // this.stringToLengthPrefixed(newSentry) - // ); - // result = sentryResult.buffer; - // if (sentryResult.count > 0) { - // console.log(` Patched ${sentryResult.count} sentry URL(s)`); - // totalCount += sentryResult.count; - // } - console.log(` Skipping sentry patch (stability fix)`); + // 1. Patch telemetry/sentry URL + const oldSentry = 'https://ca900df42fcf57d4dd8401a86ddd7da2@sentry.hytale.com/2'; + const newSentry = `${protocol}t@${domain}/2`; + + const sentryResult = this.replaceBytes( + result, + this.stringToLengthPrefixed(oldSentry), + this.stringToLengthPrefixed(newSentry) + ); + result = sentryResult.buffer; + if (sentryResult.count > 0) { + console.log(` Patched ${sentryResult.count} sentry URL(s)`); + totalCount += sentryResult.count; + } // 2. Patch main domain (hytale.com -> mainDomain) const domainResult = this.replaceBytes(