Skip to content

Commit 7948343

Browse files
rshestfacebook-github-bot
authored andcommitted
Fix race condition in ReadableNativeMap
Summary: [Changelog][Internal] Guard call to the C++ ReadableNAtiveMap.importValues with a lock. Note that all the occurrences in this class (together with importTypes) already were protected by a lock, except of this one, which with the very high chance caused crashes in T145271136. My corresponding comment from the task, for justification: > If callstack to be trusted, the crash happens on the C++ side, in ReadableNativeMap::importValues(). It throws ArrayIndexOutOfBoundsException, which, looking at the code, seems to be only possible due to a corrupted data or race conditions. > Now, looking at the Java side of ReadableNativeMap, and the particular call site... it's very dodgy, since all other occurrences of calling to native importTypes/importValues are guarded by locks, but the one crashing isn't. NOTE: A couple of `importKeys()` instances appears to suffer from the same problem as well. Reviewed By: javache Differential Revision: D43398416 fbshipit-source-id: 5ea3301857517d99a9007ddf2739e49ca8136743
1 parent ed8a3e0 commit 7948343

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,15 @@ public int getInt(@NonNull String name) {
188188
@Override
189189
public @NonNull Iterator<Map.Entry<String, Object>> getEntryIterator() {
190190
if (mKeys == null) {
191-
mKeys = Assertions.assertNotNull(importKeys());
191+
synchronized (this) {
192+
mKeys = Assertions.assertNotNull(importKeys());
193+
}
192194
}
193195
final String[] iteratorKeys = mKeys;
194-
final Object[] iteratorValues = Assertions.assertNotNull(importValues());
196+
Object[] iteratorValues;
197+
synchronized (this) {
198+
iteratorValues = Assertions.assertNotNull(importValues());
199+
}
195200
return new Iterator<Map.Entry<String, Object>>() {
196201
int currentIndex = 0;
197202

@@ -227,7 +232,9 @@ public Object setValue(Object value) {
227232
@Override
228233
public @NonNull ReadableMapKeySetIterator keySetIterator() {
229234
if (mKeys == null) {
230-
mKeys = Assertions.assertNotNull(importKeys());
235+
synchronized (this) {
236+
mKeys = Assertions.assertNotNull(importKeys());
237+
}
231238
}
232239
final String[] iteratorKeys = mKeys;
233240
return new ReadableMapKeySetIterator() {

0 commit comments

Comments
 (0)