Bug 55380 - Adding conditional formatting gets stuck in endless loop
Summary: Adding conditional formatting gets stuck in endless loop
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.9-FINAL
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-08-07 18:29 UTC by Yauheni Shahun
Modified: 2013-08-12 19:24 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yauheni Shahun 2013-08-07 18:29:59 UTC
Adding conditional formatting with certain set of ranges gets stuck in endless loop.

Test code to reproduce:

Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
CellRangeAddress[] ranges = new CellRangeAddress[] {
    CellRangeAddress.valueOf("C9:D30"), CellRangeAddress.valueOf("C7:C31")
};
ConditionalFormattingRule rule = sheet.getSheetConditionalFormatting().createConditionalFormattingRule("$A$1>0");
sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule);

Debugging shows that code gets stuck in org.apache.poi.hssf.record.cf.CellRangeUtil.mergeCellRanges(List) method on repeated merging/unmerging regions.
Comment 1 Nick Burch 2013-08-08 23:01:28 UTC
Does this happen for XSSF too? (That'll help us narrow down if it's common code that's broken, or HSSF specific code that's the problem)
Comment 2 Yauheni Shahun 2013-08-08 23:56:15 UTC
I verified that this happens for XSSF too because the same CellRangeUtil class is used.
Comment 3 Dominik Stadler 2013-08-09 12:27:55 UTC
Stacktrace when it hangs: 

	CellRangeUtil.mergeCellRanges(List) line: 118	
	CellRangeUtil.mergeCellRanges(CellRangeAddress[]) line: 101	
	CFHeaderRecord.<init>(CellRangeAddress[], int) line: 45	
	CFRecordsAggregate.<init>(CellRangeAddress[], CFRuleRecord[]) line: 72	
	HSSFSheetConditionalFormatting.addConditionalFormatting(CellRangeAddress[], HSSFConditionalFormattingRule[]) line: 155	
	HSSFSheetConditionalFormatting.addConditionalFormatting(CellRangeAddress[], HSSFConditionalFormattingRule) line: 172	
	HSSFSheetConditionalFormatting.addConditionalFormatting(CellRangeAddress[], ConditionalFormattingRule) line: 182	
	TestHSSFConditionalFormatting(BaseTestConditionalFormatting).testBug55380() line: 698
Comment 4 Dominik Stadler 2013-08-09 13:11:03 UTC
A simplified reproducer is as follows:

    public void testMergeCellRanges55380() {
        CellRangeAddress cr1 = CellRangeAddress.valueOf("C9:D30");
        CellRangeAddress cr2 = CellRangeAddress.valueOf("C7:C31");
        CellRangeAddress[] cr3 = CellRangeUtil.mergeCellRanges(new CellRangeAddress[]{cr1, cr2});   // endless loop...
        assertEquals(2, cr3.length);
        assertEquals("C9:D30", cr3[0].formatAsString());
        assertEquals("C7:C31", cr3[1].formatAsString());
    }
Comment 5 Dominik Stadler 2013-08-09 15:12:05 UTC
It happens with Overlapping Regions (i.e. not enclosing/inside and not no_intersection), the handling of this case is quite complex and seems to be buggy, I would propose to simply remove this for now as it only would be able to merge some rare cases and obviously does not do that well anyway right now. 

Any objections to the removal of this code-pieces, i.e. resolveRangeOverlap() and related methods?
Comment 6 Dominik Stadler 2013-08-12 19:24:50 UTC
For now I have removed merging of overlapping regions to avoid the endless loops in the implementation of the mergeCellRanges(). None of the unit tests stepped into the method, so the code was untested and probably never fully worked at all or was broken sometimes back by other changes.