From 9587b1a2cf009214aa3aa0ade6fcbd7ea15b1aeb Mon Sep 17 00:00:00 2001 From: Sophie Alpert Date: Thu, 23 Feb 2023 22:37:09 -0800 Subject: [PATCH] [Fizz Node] Fix null bytes written at text chunk boundaries We encode strings 2048 UTF-8 bytes at a time. If the string we are encoding crosses to the next chunk but the current chunk doesn't fit an integral number of characters, we need to make sure not to send the whole buffer, only the bytes that are actually meaningful. Fixes #24985. I was able to verify that this fixes the repro shared in the issue (be careful when testing because the null bytes do not show when printed to my terminal, at least). However, I don't see a clear way to add a test for this that will be resilient to small changes in how we encode the markup (since it depends on where specific multibyte characters fall against the 2048-byte boundaries). --- packages/react-server/src/ReactServerStreamConfigNode.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/react-server/src/ReactServerStreamConfigNode.js b/packages/react-server/src/ReactServerStreamConfigNode.js index 807dafe5edf6e..15d503553be11 100644 --- a/packages/react-server/src/ReactServerStreamConfigNode.js +++ b/packages/react-server/src/ReactServerStreamConfigNode.js @@ -75,12 +75,11 @@ function writeStringChunk(destination: Destination, stringChunk: string) { writtenBytes += written; if (read < stringChunk.length) { - writeToDestination(destination, (currentView: any)); + writeToDestination(destination, (currentView: any).subarray(0, writtenBytes)); currentView = new Uint8Array(VIEW_SIZE); writtenBytes = textEncoder.encodeInto( stringChunk.slice(read), - // $FlowFixMe[incompatible-call] found when upgrading Flow - currentView, + (currentView: any), ).written; }