Files
react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h
T
Igor Klemenski e69f1c9f50 Fix unsafe cast and detect resize overflow. (#31106)
Summary:
Removing unsafe cast from `int` to `uint16_t`.
Also, adding code to detect multiplication overflow during buffer resize.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[General] [Fix] - Fix unsafe cast and detect overflow in MapBuffer.

Pull Request resolved: https://github.com/facebook/react-native/pull/31106

Test Plan: Code compiles in Visual Studio 2019 without the unsafe cast warning (or error depending on the configuration).

Reviewed By: mdvacca

Differential Revision: D26865138

Pulled By: rozele

fbshipit-source-id: 4692a38b05fc873e31fbbe94d70803244e82de5d
2021-03-09 08:47:31 -08:00

83 lines
2.0 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/renderer/mapbuffer/Primitives.h>
namespace facebook {
namespace react {
// 506 = 5 entries = 50*10 + 6 sizeof(header)
constexpr uint16_t INITIAL_SIZE = 506;
/**
* MapBuffer is an optimized map format for transferring data like props between
* C++ and other platforms The implementation of this map is optimized to:
* - be compact to optimize space when sparse (sparse is the common case).
* - be accessible through JNI with zero/minimal copying via ByteBuffer.
* - be Have excellent C++ single-write and many-read performance by maximizing
* CPU cache performance through compactness, data locality, and fixed offsets
* where possible.
* - be optimized for iteration and intersection against other maps, but with
* reasonably good random access as well.
* - Work recursively for nested maps/arrays.
* - Supports dynamic types that map to JSON.
* - Don't require mutability - single-write on creation.
* - have minimal APK size and build time impact.
*/
class MapBuffer {
private:
Header _header = {ALIGNMENT, 0, 0};
void makeSpace();
void putBytes(Key key, uint8_t *value, int valueSize);
// Buffer and its size
uint8_t *_data;
uint16_t _dataSize;
public:
MapBuffer() : MapBuffer(INITIAL_SIZE) {}
MapBuffer(uint16_t initialSize);
~MapBuffer();
void putInt(Key key, int value);
void putBool(Key key, bool value);
void putDouble(Key key, double value);
void putNull(Key key);
// TODO: create a MapBufferBuilder instead or add checks to verify
// if it's ok to read and write the Map
void finish();
int getInt(Key key);
bool getBool(Key key);
double getDouble(Key key);
uint16_t getBufferSize();
// TODO: review parameters of copy method
void copy(uint8_t *output);
bool isNull(Key key);
uint16_t getSize();
};
} // namespace react
} // namespace facebook