Bug 45645

Summary: autoSizeColumn() works incorrectly for large widths (cf. bug #43090)
Product: POI Reporter: Norbert Schenk <norbert.schenk>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 3.0-dev   
Target Milestone: ---   
Hardware: PC   
OS: Windows Server 2003   

Description Norbert Schenk 2008-08-18 06:55:26 UTC
Preliminary note: This bug is in 3.1-FINAL, but the latest version the Combo Box did offer was 3.0-dev

In HSSFSheet.autoSizeColumns(), the following code:

if (width != -1) {
    if (width > Short.MAX_VALUE) { //width can be bigger that Short.MAX_VALUE!
        width = Short.MAX_VALUE;
    }
    sheet.setColumnWidth(column, (short) (width * 256));
}

should be replaced by:

if (width != -1) {
    width *= 256;
    if (width > Short.MAX_VALUE) { //width can be bigger that Short.MAX_VALUE!
        width = Short.MAX_VALUE;
    }
    sheet.setColumnWidth(column, (short) (width));
}

which is probably what was intended, since only then the cast to short will always have an argument that is within range.

With the original version, large widths produce settings which depend on the least significant bits and thus tend to be rather random.

The topic has already be addressed in bug #43090, but the resolution which made it into the final version does not solve the problem.
Comment 1 Yegor Kozlov 2008-08-18 11:34:26 UTC
Thanks for pointing it out. Of course, multiplication should be done before checking Short.MAX_VALUE.
Fixed in r686844.

Yegor