From: Nick Burch Date: Sun, 21 Sep 2008 17:38:39 +0000 (+0000) Subject: Fix bug #45777 - Throw an exception if HSSF Footer or Header is attemped to be set... X-Git-Tag: REL_3_2_FINAL~38 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=91ee384223f0589c8bd7c3d6097f6acd0c8ea21d;p=poi.git Fix bug #45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@697559 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 4bcbce240c..eb5ae733f3 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out 45844 - Addtional diagnostics for HSLF SlideShowRecordDumper 45829 - HSSFPicture.getImageDimension() failed when DPI of image is zero 45815 - Bit mask values in StyleTextPropAtom were not preserved across read-write diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index d78b3fe7b3..1718ff8411 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out 45844 - Addtional diagnostics for HSLF SlideShowRecordDumper 45829 - HSSFPicture.getImageDimension() failed when DPI of image is zero 45815 - Bit mask values in StyleTextPropAtom were not preserved across read-write diff --git a/src/java/org/apache/poi/hssf/record/FooterRecord.java b/src/java/org/apache/poi/hssf/record/FooterRecord.java index 6a1aa8624b..6ccc082801 100644 --- a/src/java/org/apache/poi/hssf/record/FooterRecord.java +++ b/src/java/org/apache/poi/hssf/record/FooterRecord.java @@ -118,6 +118,18 @@ public class FooterRecord field_4_footer = footer; field_3_unicode_flag = (byte) (StringUtil.hasMultibyte(field_4_footer) ? 1 : 0); + // Check it'll fit into the space in the record + + if(field_4_footer == null) return; + if(field_3_unicode_flag == 1) { + if(field_4_footer.length() > 127) { + throw new IllegalArgumentException("Footer string too long (limit is 127 for unicode strings)"); + } + } else { + if(field_4_footer.length() > 255) { + throw new IllegalArgumentException("Footer string too long (limit is 255 for non-unicode strings)"); + } + } } /** diff --git a/src/java/org/apache/poi/hssf/record/HeaderRecord.java b/src/java/org/apache/poi/hssf/record/HeaderRecord.java index cd58437028..e8190364a8 100644 --- a/src/java/org/apache/poi/hssf/record/HeaderRecord.java +++ b/src/java/org/apache/poi/hssf/record/HeaderRecord.java @@ -117,6 +117,18 @@ public class HeaderRecord field_4_header = header; field_3_unicode_flag = (byte) (StringUtil.hasMultibyte(field_4_header) ? 1 : 0); + + // Check it'll fit into the space in the record + if(field_4_header == null) return; + if(field_3_unicode_flag == 1) { + if(field_4_header.length() > 127) { + throw new IllegalArgumentException("Header string too long (limit is 127 for unicode strings)"); + } + } else { + if(field_4_header.length() > 255) { + throw new IllegalArgumentException("Header string too long (limit is 255 for non-unicode strings)"); + } + } } /** diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java index 10c779ffab..6d895b9e49 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java @@ -70,6 +70,11 @@ public class HSSFFooter extends HeaderFooter { right = newRight; createFooterString(); } + + protected String getRawFooter() { + return footerRecord.getFooter(); + } + /** * Creates the complete footer string based on the left, center, and middle diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java b/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java index 7b932f6821..82748b8bc8 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java @@ -77,6 +77,10 @@ public class HSSFHeader extends HeaderFooter { right = newRight; createHeaderString(); } + + protected String getRawHeader() { + return headerRecord.getHeader(); + } /** * Creates the complete header string based on the left, center, and middle diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index a4f9bbf18d..4a43006997 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -1414,4 +1414,64 @@ public final class TestBugs extends TestCase { assertFalse(nwb.isSheetHidden(2)); assertTrue(nwb.isSheetVeryHidden(2)); } + + /** + * header / footer text too long + */ + public void test45777() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet(); + + String s248 = ""; + for(int i=0; i<248; i++) { + s248 += "x"; + } + String s249 = s248 + "1"; + String s250 = s248 + "12"; + String s251 = s248 + "123"; + assertEquals(248, s248.length()); + assertEquals(249, s249.length()); + assertEquals(250, s250.length()); + assertEquals(251, s251.length()); + + + // Try on headers + s.getHeader().setCenter(s248); + assertEquals(254, s.getHeader().getRawHeader().length()); + writeOutAndReadBack(wb); + + s.getHeader().setCenter(s249); + assertEquals(255, s.getHeader().getRawHeader().length()); + writeOutAndReadBack(wb); + + try { + s.getHeader().setCenter(s250); // 256 + fail(); + } catch(IllegalArgumentException e) {} + + try { + s.getHeader().setCenter(s251); // 257 + fail(); + } catch(IllegalArgumentException e) {} + + + // Now try on footers + s.getFooter().setCenter(s248); + assertEquals(254, s.getFooter().getRawFooter().length()); + writeOutAndReadBack(wb); + + s.getFooter().setCenter(s249); + assertEquals(255, s.getFooter().getRawFooter().length()); + writeOutAndReadBack(wb); + + try { + s.getFooter().setCenter(s250); // 256 + fail(); + } catch(IllegalArgumentException e) {} + + try { + s.getFooter().setCenter(s251); // 257 + fail(); + } catch(IllegalArgumentException e) {} + } }