Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsafe cast and detect resize overflow. #31106

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ using namespace facebook::react;
namespace facebook {
namespace react {

MapBuffer::MapBuffer(int initialSize) {
MapBuffer::MapBuffer(uint16_t initialSize) {
_dataSize = initialSize;
_data = new Byte[_dataSize];
// TODO: Should we clean up memory here?
}

void MapBuffer::makeSpace() {
int oldDataSize = _dataSize;
if (_dataSize >= std::numeric_limits<uint16_t>::max() / 2) {
LOG(ERROR)
<< "Error: trying to assign a value beyond the capacity of uint16_t"
<< static_cast<uint32_t>(_dataSize) * 2;
throw "Error: trying to assign a value beyond the capacity of uint16_t" +
std::to_string(static_cast<uint32_t>(_dataSize) * 2);
}
_dataSize *= 2;
uint8_t *_newdata = new Byte[_dataSize];
uint8_t *_oldData = _data;
Expand Down
4 changes: 2 additions & 2 deletions ReactCommon/react/renderer/mapbuffer/MapBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace facebook {
namespace react {

// 506 = 5 entries = 50*10 + 6 sizeof(header)
const int INITIAL_SIZE = 506;
constexpr uint16_t INITIAL_SIZE = 506;

/**
* MapBuffer is an optimized map format for transferring data like props between
Expand Down Expand Up @@ -46,7 +46,7 @@ class MapBuffer {
public:
MapBuffer() : MapBuffer(INITIAL_SIZE) {}

MapBuffer(int initialSize);
MapBuffer(uint16_t initialSize);

~MapBuffer();

Expand Down