You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2007/08/10 18:21:47 UTC

DO NOT REPLY [Bug 43090] New: - autoSizeColumn can calculate negative sizes for the column width due to a cast from integer to short

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=43090>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=43090

           Summary: autoSizeColumn can calculate negative sizes for the
                    column width due to a cast from integer to short
           Product: POI
           Version: 3.0
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: normal
          Priority: P2
         Component: HSSF
        AssignedTo: dev@poi.apache.org
        ReportedBy: jan.dostert@sap.com


Hi, 

autoSizeColumn can calculate negative sizes for the column width due to a cast
from integer to short.

If a column is long (e.g. 3000 characters), the calculated length of
autoSizeColumn does not fit into a short any more.

HSSFSheet.autoSizeColumn(short) line: 1476	

           if (width != -1) {
                sheet.setColumnWidth(column, (short) (width * 256));
            }

e.g. width = 1668.10  

width * 256 = 1668.10 * 256 = 427033,6

which is greater than 32767 which a short can hold. Due to the cast to short
only the lower two bytes are taken into account which results in -31718.

Thus, sheet.setColumnWidth is called with an argument of -31718 and sets the
width to a negative value.

Maybe a check like 

if (width > Short.MAX_VALUE) {
     width = Short.MAX_VALUE;
}

would already help.

Attached a small test case which reproduces the problem.

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class AutoSizeColumn {

    public static void main(String args[]) throws Exception {

        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet();
        
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell((short)0);

        int size = 3000;
        
        StringBuffer value = new StringBuffer(size);
        for (int i = 0; i < size; i++) {
            value.append(".");
        }
        
        cell.setCellValue(new HSSFRichTextString(value.toString()));
        
        sheet.autoSizeColumn((short)0);
        int width = sheet.getColumnWidth((short)0);
        
        if (width < 0) {
            System.out.println("width < 0");
        }
        
        workBook.write(new FileOutputStream("foo.xls"));
    }
}

Regards,
Jan

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


DO NOT REPLY [Bug 43090] - autoSizeColumn can calculate negative sizes for the column width due to a cast from integer to short

Posted by bu...@apache.org.
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=43090>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=43090


yegor@dinom.ru changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Additional Comments From yegor@dinom.ru  2007-08-10 10:35 -------
Good catch.
I applied the suggested fix.

Regards,
Yegor


-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org