Fix assert when getting empty string at end of storage

Summary:
When the empty string is the last entry in the string table, our code would illegally use std::vector::operator[] past the last char, causing assert failures in debug builds.

Fix this by using pointer arithmetic instead.

Reviewed By: jpporto

Differential Revision: D36551789

fbshipit-source-id: 41380716a2e4a5148236518b560183ce6c5d972e
This commit is contained in:
Daniel Andersson 2022-05-23 12:04:14 -07:00 коммит произвёл Facebook GitHub Bot
Родитель d6a8daec04
Коммит a746083e0f
2 изменённых файлов: 16 добавлений и 1 удалений

Просмотреть файл

@ -758,8 +758,12 @@ uint32_t ConsecutiveStringStorage::getEntryHash(size_t i) const {
ensureStorageValid();
auto &entry = strTable_[i];
const unsigned char *data = &storage_[entry.getOffset()];
uint32_t length = entry.getLength();
assert(
entry.getOffset() + (entry.isUTF16() ? length * 2 : length) <=
storage_.size() &&
"entry past end");
const unsigned char *data = storage_.data() + entry.getOffset();
if (entry.isUTF16()) {
const char16_t *u16data = reinterpret_cast<const char16_t *>(data);
return hermes::hashString(ArrayRef<char16_t>{u16data, length});

Просмотреть файл

@ -0,0 +1,11 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// RUN: %hermes -O -target=HBC %s
// RUN: %hermes -O -target=HBC -lazy %s
// RUN: %hermes -O -target=HBC -emit-binary -out %t.hbc %s && %hermes %t.hbc
''['']