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
This commit is contained in:
Nick Burch 2008-09-21 17:38:39 +00:00
parent fe1d514b0b
commit cb4c63a2bc
7 changed files with 95 additions and 0 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.2-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out</action>
<action dev="POI-DEVELOPERS" type="add">45844 - Addtional diagnostics for HSLF SlideShowRecordDumper</action>
<action dev="POI-DEVELOPERS" type="fix">45829 - HSSFPicture.getImageDimension() failed when DPI of image is zero</action>
<action dev="POI-DEVELOPERS" type="fix">45815 - Bit mask values in StyleTextPropAtom were not preserved across read-write</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.2-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out</action>
<action dev="POI-DEVELOPERS" type="add">45844 - Addtional diagnostics for HSLF SlideShowRecordDumper</action>
<action dev="POI-DEVELOPERS" type="fix">45829 - HSSFPicture.getImageDimension() failed when DPI of image is zero</action>
<action dev="POI-DEVELOPERS" type="fix">45815 - Bit mask values in StyleTextPropAtom were not preserved across read-write</action>

View File

@ -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)");
}
}
}
/**

View File

@ -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)");
}
}
}
/**

View File

@ -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

View File

@ -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

View File

@ -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) {}
}
}