From ced779b25966448feb9bd792efedd29dfa6ab9e0 Mon Sep 17 00:00:00 2001 From: Dustin Shahidehpour Date: Mon, 31 Oct 2016 12:41:09 -0700 Subject: [PATCH] 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 --- CSSLayout/CSSNodeList.c | 6 +++++- tests/CSSLayoutDefaultValuesTest.cpp | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CSSLayout/CSSNodeList.c b/CSSLayout/CSSNodeList.c index c4b8f3c1..d48cf4b3 100644 --- a/CSSLayout/CSSNodeList.c +++ b/CSSLayout/CSSNodeList.c @@ -92,5 +92,9 @@ CSSNodeRef CSSNodeListDelete(const CSSNodeListRef list, const CSSNodeRef node) { } CSSNodeRef CSSNodeListGet(const CSSNodeListRef list, const uint32_t index) { - return list->items[index]; + if (CSSNodeListCount(list) > 0) { + return list->items[index]; + } + + return NULL; } diff --git a/tests/CSSLayoutDefaultValuesTest.cpp b/tests/CSSLayoutDefaultValuesTest.cpp index be0dd8e1..7c647f61 100644 --- a/tests/CSSLayoutDefaultValuesTest.cpp +++ b/tests/CSSLayoutDefaultValuesTest.cpp @@ -13,6 +13,9 @@ TEST(CSSLayoutTest, assert_default_values) { const CSSNodeRef root = CSSNodeNew(); + ASSERT_EQ(0, CSSNodeChildCount(root)); + ASSERT_EQ(NULL, CSSNodeGetChild(root, 1)); + ASSERT_EQ(CSSDirectionInherit, CSSNodeStyleGetDirection(root)); ASSERT_EQ(CSSFlexDirectionColumn, CSSNodeStyleGetFlexDirection(root)); ASSERT_EQ(CSSJustifyFlexStart, CSSNodeStyleGetJustifyContent(root));