From b43a7be86771395815cace0c67bd385e926b8af0 Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Fri, 1 Mar 2019 12:12:08 +0100 Subject: [PATCH 1/2] make sure to check array bounds --- Libraries/Lists/VirtualizedSectionList.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Libraries/Lists/VirtualizedSectionList.js b/Libraries/Lists/VirtualizedSectionList.js index e314871c4ee13a..6f11c06ef4ff3f 100644 --- a/Libraries/Lists/VirtualizedSectionList.js +++ b/Libraries/Lists/VirtualizedSectionList.js @@ -221,9 +221,9 @@ class VirtualizedSectionList extends React.PureComponent< trailingSection?: ?SectionT, } { let itemIndex = index; - const defaultKeyExtractor = this.props.keyExtractor; - for (let ii = 0; ii < this.props.sections.length; ii++) { - const section = this.props.sections[ii]; + const {sections} = this.props + for (let ii = 0; ii < sections.length; ii++) { + const section = sections[ii]; const key = section.key || String(ii); itemIndex -= 1; // The section adds an item for the header if (itemIndex >= section.data.length + 1) { @@ -234,7 +234,7 @@ class VirtualizedSectionList extends React.PureComponent< key: key + ':header', index: null, header: true, - trailingSection: this.props.sections[ii + 1], + trailingSection: sections[ii + 1], }; } else if (itemIndex === section.data.length) { return { @@ -242,18 +242,18 @@ class VirtualizedSectionList extends React.PureComponent< key: key + ':footer', index: null, header: false, - trailingSection: this.props.sections[ii + 1], + trailingSection: sections[ii + 1], }; } else { - const keyExtractor = section.keyExtractor || defaultKeyExtractor; + const keyExtractor = section.keyExtractor || this.props.keyExtractor; return { section, key: key + ':' + keyExtractor(section.data[itemIndex], itemIndex), index: itemIndex, leadingItem: section.data[itemIndex - 1], - leadingSection: this.props.sections[ii - 1], - trailingItem: section.data[itemIndex + 1], - trailingSection: this.props.sections[ii + 1], + leadingSection: sections[ii - 1], + trailingItem: section.data.length > itemIndex + 1 ? section.data[itemIndex + 1] : undefined, + trailingSection: sections[ii + 1], }; } } From dcf8519490a81f51ed174be21f2251dc8c816d52 Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Fri, 1 Mar 2019 12:20:50 +0100 Subject: [PATCH 2/2] fix lint --- Libraries/Lists/VirtualizedSectionList.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Libraries/Lists/VirtualizedSectionList.js b/Libraries/Lists/VirtualizedSectionList.js index 6f11c06ef4ff3f..7cb2af4dff0439 100644 --- a/Libraries/Lists/VirtualizedSectionList.js +++ b/Libraries/Lists/VirtualizedSectionList.js @@ -221,7 +221,7 @@ class VirtualizedSectionList extends React.PureComponent< trailingSection?: ?SectionT, } { let itemIndex = index; - const {sections} = this.props + const {sections} = this.props; for (let ii = 0; ii < sections.length; ii++) { const section = sections[ii]; const key = section.key || String(ii); @@ -252,7 +252,10 @@ class VirtualizedSectionList extends React.PureComponent< index: itemIndex, leadingItem: section.data[itemIndex - 1], leadingSection: sections[ii - 1], - trailingItem: section.data.length > itemIndex + 1 ? section.data[itemIndex + 1] : undefined, + trailingItem: + section.data.length > itemIndex + 1 + ? section.data[itemIndex + 1] + : undefined, trailingSection: sections[ii + 1], }; }