Skip to content

Commit

Permalink
Merge pull request #51879 from Expensify/arosiclair-fix-parent-report…
Browse files Browse the repository at this point in the history
…-loop

(cherry picked from commit e7267ea)

(CP triggered by mountiny)
  • Loading branch information
dangrous authored and OSBotify committed Nov 1, 2024
1 parent 02b0cf1 commit 391be07
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ function getParentReport(report: OnyxEntry<Report>): OnyxEntry<Report> {
* Returns the root parentReport if the given report is nested.
* Uses recursion to iterate any depth of nested reports.
*/
function getRootParentReport(report: OnyxEntry<Report>): OnyxEntry<Report> {
function getRootParentReport(report: OnyxEntry<Report>, visitedReportIDs: Set<string> = new Set<string>()): OnyxEntry<Report> {
if (!report) {
return undefined;
}
Expand All @@ -766,10 +766,18 @@ function getRootParentReport(report: OnyxEntry<Report>): OnyxEntry<Report> {
return report;
}

// Detect and prevent an infinite loop caused by a cycle in the ancestry. This should normally
// never happen
if (visitedReportIDs.has(report.reportID)) {
Log.alert('Report ancestry cycle detected.', {reportID: report.reportID, ancestry: Array.from(visitedReportIDs)});
return undefined;
}
visitedReportIDs.add(report.reportID);

const parentReport = getReportOrDraftReport(report?.parentReportID);

// Runs recursion to iterate a parent report
return getRootParentReport(!isEmptyObject(parentReport) ? parentReport : undefined);
return getRootParentReport(!isEmptyObject(parentReport) ? parentReport : undefined, visitedReportIDs);
}

/**
Expand Down

0 comments on commit 391be07

Please sign in to comment.