Skip to content

Commit ced779b

Browse files
d16rFacebook Github Bot
authored andcommitted
Prevent crash when accessing child count, but child list is NULL.
Summary: Previously, we would preallocate Node's with a child list of 4. We recently removed that logic (see diff below), and as a result, if you tried to access a Node's list of children before it had been allocated, you would crash. I added a simple check to protect from crashes, the operation of the check is O(1) so we shouldn't see a perf hit. Reviewed By: emilsjolander Differential Revision: D4104093 fbshipit-source-id: cd7b09818759aa76415b97e241f1a6746a2bc50c
1 parent a65e693 commit ced779b

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

CSSLayout/CSSNodeList.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,9 @@ CSSNodeRef CSSNodeListDelete(const CSSNodeListRef list, const CSSNodeRef node) {
9292
}
9393

9494
CSSNodeRef CSSNodeListGet(const CSSNodeListRef list, const uint32_t index) {
95-
return list->items[index];
95+
if (CSSNodeListCount(list) > 0) {
96+
return list->items[index];
97+
}
98+
99+
return NULL;
96100
}

tests/CSSLayoutDefaultValuesTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
TEST(CSSLayoutTest, assert_default_values) {
1414
const CSSNodeRef root = CSSNodeNew();
1515

16+
ASSERT_EQ(0, CSSNodeChildCount(root));
17+
ASSERT_EQ(NULL, CSSNodeGetChild(root, 1));
18+
1619
ASSERT_EQ(CSSDirectionInherit, CSSNodeStyleGetDirection(root));
1720
ASSERT_EQ(CSSFlexDirectionColumn, CSSNodeStyleGetFlexDirection(root));
1821
ASSERT_EQ(CSSJustifyFlexStart, CSSNodeStyleGetJustifyContent(root));

0 commit comments

Comments
 (0)