Bug 59665: Using HSSFWorkbook#setSheetOrder to move sheets to the end corrupts bspos value in WorkbookRecordList

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753038 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-07-17 09:27:07 +00:00
parent c44094c04e
commit 16b0c380c9
2 changed files with 16 additions and 2 deletions

View File

@ -655,17 +655,18 @@ public final class InternalWorkbook {
* @param sheetname the name of the sheet to reorder * @param sheetname the name of the sheet to reorder
* @param pos the position that we want to insert the sheet into (0 based) * @param pos the position that we want to insert the sheet into (0 based)
*/ */
public void setSheetOrder(String sheetname, int pos ) { public void setSheetOrder(String sheetname, int pos ) {
int sheetNumber = getSheetIndex(sheetname); int sheetNumber = getSheetIndex(sheetname);
//remove the sheet that needs to be reordered and place it in the spot we want //remove the sheet that needs to be reordered and place it in the spot we want
boundsheets.add(pos, boundsheets.remove(sheetNumber)); boundsheets.add(pos, boundsheets.remove(sheetNumber));
// also adjust order of Records, calculate the position of the Boundsheets via getBspos()... // also adjust order of Records, calculate the position of the Boundsheets via getBspos()...
int pos0 = records.getBspos() - (boundsheets.size() - 1); int initialBspos = records.getBspos();
int pos0 = initialBspos - (boundsheets.size() - 1);
Record removed = records.get(pos0 + sheetNumber); Record removed = records.get(pos0 + sheetNumber);
records.remove(pos0 + sheetNumber); records.remove(pos0 + sheetNumber);
records.add(pos0 + pos, removed); records.add(pos0 + pos, removed);
records.setBspos(initialBspos);
} }
/** /**

View File

@ -1197,4 +1197,17 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
wb = new HSSFWorkbook(new FileInputStream(file)); wb = new HSSFWorkbook(new FileInputStream(file));
assertCloseDoesNotModifyFile(filename, wb); assertCloseDoesNotModifyFile(filename, wb);
} }
@Test
public void setSheetOrderToEnd() throws Exception {
final HSSFWorkbook workbook = new HSSFWorkbook();
workbook.createSheet("A");
try {
for (int i = 0; i < 2 * workbook.getInternalWorkbook().getRecords().size(); i++) {
workbook.setSheetOrder("A", 0);
}
} catch (Exception e) {
throw new Exception("Moving a sheet to the end should not throw an exception, but threw ", e);
}
}
} }