Skip to content

Commit 929908f

Browse files
vonovakfacebook-github-bot
authored andcommitted
make sure to check array bounds in VirtualizedSectionList (#23710)
Summary: SectionList accesses items outside of the array bounds. This was discovered when using mobx, which warns you: `[mobx.array] Attempt to read an array index (${index}) that is out of bounds`. This is because `section.data[itemIndex + 1]` goes beyond array length. This PR adds an array length check and simplifies the code a bit to avoid repetitive `this.props.` Pull Request resolved: #23710 Differential Revision: D14298557 Pulled By: cpojer fbshipit-source-id: fee3422ad5b053d91a097c5842f46e78a149c3d5
1 parent 7025cce commit 929908f

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

Libraries/Lists/VirtualizedSectionList.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ class VirtualizedSectionList<SectionT: SectionBase> extends React.PureComponent<
221221
trailingSection?: ?SectionT,
222222
} {
223223
let itemIndex = index;
224-
const defaultKeyExtractor = this.props.keyExtractor;
225-
for (let ii = 0; ii < this.props.sections.length; ii++) {
226-
const section = this.props.sections[ii];
224+
const {sections} = this.props;
225+
for (let ii = 0; ii < sections.length; ii++) {
226+
const section = sections[ii];
227227
const key = section.key || String(ii);
228228
itemIndex -= 1; // The section adds an item for the header
229229
if (itemIndex >= section.data.length + 1) {
@@ -234,26 +234,29 @@ class VirtualizedSectionList<SectionT: SectionBase> extends React.PureComponent<
234234
key: key + ':header',
235235
index: null,
236236
header: true,
237-
trailingSection: this.props.sections[ii + 1],
237+
trailingSection: sections[ii + 1],
238238
};
239239
} else if (itemIndex === section.data.length) {
240240
return {
241241
section,
242242
key: key + ':footer',
243243
index: null,
244244
header: false,
245-
trailingSection: this.props.sections[ii + 1],
245+
trailingSection: sections[ii + 1],
246246
};
247247
} else {
248-
const keyExtractor = section.keyExtractor || defaultKeyExtractor;
248+
const keyExtractor = section.keyExtractor || this.props.keyExtractor;
249249
return {
250250
section,
251251
key: key + ':' + keyExtractor(section.data[itemIndex], itemIndex),
252252
index: itemIndex,
253253
leadingItem: section.data[itemIndex - 1],
254-
leadingSection: this.props.sections[ii - 1],
255-
trailingItem: section.data[itemIndex + 1],
256-
trailingSection: this.props.sections[ii + 1],
254+
leadingSection: sections[ii - 1],
255+
trailingItem:
256+
section.data.length > itemIndex + 1
257+
? section.data[itemIndex + 1]
258+
: undefined,
259+
trailingSection: sections[ii + 1],
257260
};
258261
}
259262
}

0 commit comments

Comments
 (0)