Skip to content

Commit

Permalink
Fix +1 problem in LocalBundleReader (#10656)
Browse files Browse the repository at this point in the history
* Fix +1 problem in LocalBundleReader

* Change files
  • Loading branch information
aeulitz committed Sep 30, 2022
1 parent 9b5caf4 commit 663554a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Fix +1 problem in LocalBundleReader",
"packageName": "react-native-windows",
"email": "aeulitz@microsoft.com",
"dependentChangeType": "none"
}
5 changes: 3 additions & 2 deletions vnext/Microsoft.ReactNative/Utils/LocalBundleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ std::future<std::string> LocalBundleReader::LoadBundleAsync(const std::string &b
auto fileBuffer{co_await winrt::Windows::Storage::FileIO::ReadBufferAsync(file)};
auto dataReader{winrt::Windows::Storage::Streams::DataReader::FromBuffer(fileBuffer)};

std::string script(fileBuffer.Length() + 1, '\0');
// No need to use length + 1, STL guarantees that string storage is null-terminated.
std::string script(fileBuffer.Length(), '\0');

// Construct the array_view to slice into the first fileBuffer.Length bytes.
// DataReader.ReadBytes will read as many bytes as are present in the
// array_view. The backing string has fileBuffer.Length() + 1 bytes, without
// an explicit end it will read 1 byte to many and throw.
dataReader.ReadBytes(winrt::array_view<uint8_t>{
reinterpret_cast<uint8_t *>(&script[0]), reinterpret_cast<uint8_t *>(&script[script.length() - 1])});
reinterpret_cast<uint8_t *>(&script[0]), reinterpret_cast<uint8_t *>(&script[script.length()])});
dataReader.Close();

co_return script;
Expand Down

0 comments on commit 663554a

Please sign in to comment.