Bug 62884 - sheetnum is not checked in setSheetHidden
Summary: sheetnum is not checked in setSheetHidden
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: unspecified
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-05 06:28 UTC by zhonghao
Modified: 2018-12-25 10:01 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description zhonghao 2018-11-05 06:28:44 UTC
I notice that the setSheetHidden method of the org.apache.poi.hssf.model.InternalWorkbook class does not check its sheetnum parameter:

public void setSheetHidden(int sheetnum, SheetVisibility visibility) {
        BoundSheetRecord bsr = getBoundSheetRec(sheetnum);
        bsr.setHidden(visibility == SheetVisibility.HIDDEN);
        bsr.setVeryHidden(visibility == SheetVisibility.VERY_HIDDEN);
}

The other method, setSheetName, does check the sheetnum parameter:


 public void setSheetName(int sheetnum, final String sheetname) {
        checkSheets(sheetnum);

        // YK: Mimic Excel and silently truncate sheet names longer than 31 characters
        String sn = (sheetname.length() > 31) ? sheetname.substring(0, 31) : sheetname;

        BoundSheetRecord sheet = boundsheets.get(sheetnum);
        sheet.setSheetname(sn);
    } 

 private void checkSheets(int sheetnum) {
        if ((boundsheets.size()) <= sheetnum) {   // if we're short one add another..
            if ((boundsheets.size() + 1) <= sheetnum) {
                throw new RuntimeException("Sheet number out of bounds!");
            }
            BoundSheetRecord bsr = createBoundSheet(sheetnum);

            records.add(records.getBspos()+1, bsr);
            records.setBspos( records.getBspos() + 1 );
            boundsheets.add(bsr);
            getOrCreateLinkTable().checkExternSheet(sheetnum);
            fixTabIdRecord();
        }
    }

Please add the check to the setSheetHidden method.
Comment 1 Dominik Stadler 2018-12-25 10:01:04 UTC
Applied via r1849717, however HSSFWorksheet applies more strict checks anyway, so the "add a record with +1" is never actually used in normal code usage.