Bug 62884

Summary: sheetnum is not checked in setSheetHidden
Product: POI Reporter: zhonghao
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: All   

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.