This looks like a candidate for the more efficient switch on string. Gary
On Sun, Jun 12, 2016 at 7:47 AM, <brit...@apache.org> wrote: > Author: britter > Date: Sun Jun 12 14:47:11 2016 > New Revision: 1748015 > > URL: http://svn.apache.org/viewvc?rev=1748015&view=rev > Log: > IMAGING-178: PnmImageParser does not check the validity of input PAM > header. Thanks to emopers. This also fixes #20 from github. > > Modified: > commons/proper/imaging/trunk/src/changes/changes.xml > > commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java > > commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/pnm/PnmImageParserTest.java > > Modified: commons/proper/imaging/trunk/src/changes/changes.xml > URL: > http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/changes/changes.xml?rev=1748015&r1=1748014&r2=1748015&view=diff > > ============================================================================== > --- commons/proper/imaging/trunk/src/changes/changes.xml (original) > +++ commons/proper/imaging/trunk/src/changes/changes.xml Sun Jun 12 > 14:47:11 2016 > @@ -46,6 +46,9 @@ The <action> type attribute can be add,u > <body> > > <release version="1.0" date="TBA" description="First major release"> > + <action issue="IMAGING-178" dev="britter" type="fix" > due-to="emopers"> > + PnmImageParser does not check the validity of input PAM header. > + </action> > <action issue="IMAGING-184" dev="ggregory" type="update"> > Update platform from Java 5 to 7 > </action> > > Modified: > commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java > URL: > http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java?rev=1748015&r1=1748014&r2=1748015&view=diff > > ============================================================================== > --- > commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java > (original) > +++ > commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/pnm/PnmImageParser.java > Sun Jun 12 14:47:11 2016 > @@ -157,18 +157,28 @@ public class PnmImageParser extends Imag > final String type = tokenizer.nextToken(); > if ("WIDTH".equals(type)) { > seenWidth = true; > + if(!tokenizer.hasMoreTokens()) > + throw new ImageReadException("PAM header has no > WIDTH value"); > width = Integer.parseInt(tokenizer.nextToken()); > } else if ("HEIGHT".equals(type)) { > seenHeight = true; > + if(!tokenizer.hasMoreTokens()) > + throw new ImageReadException("PAM header has no > HEIGHT value"); > height = Integer.parseInt(tokenizer.nextToken()); > } else if ("DEPTH".equals(type)) { > seenDepth = true; > + if(!tokenizer.hasMoreTokens()) > + throw new ImageReadException("PAM header has no > DEPTH value"); > depth = Integer.parseInt(tokenizer.nextToken()); > } else if ("MAXVAL".equals(type)) { > seenMaxVal = true; > + if(!tokenizer.hasMoreTokens()) > + throw new ImageReadException("PAM header has no > MAXVAL value"); > maxVal = Integer.parseInt(tokenizer.nextToken()); > } else if ("TUPLTYPE".equals(type)) { > seenTupleType = true; > + if(!tokenizer.hasMoreTokens()) > + throw new ImageReadException("PAM header has no > TUPLTYPE value"); > tupleType.append(tokenizer.nextToken()); > } else if ("ENDHDR".equals(type)) { > break; > > Modified: > commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/pnm/PnmImageParserTest.java > URL: > http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/pnm/PnmImageParserTest.java?rev=1748015&r1=1748014&r2=1748015&view=diff > > ============================================================================== > --- > commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/pnm/PnmImageParserTest.java > (original) > +++ > commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/pnm/PnmImageParserTest.java > Sun Jun 12 14:47:11 2016 > @@ -46,4 +46,12 @@ public class PnmImageParserTest { > PnmImageParser underTest = new PnmImageParser(); > underTest.getImageInfo(bytes, params); > } > + > + @Test(expected = ImageReadException.class) > + public void testGetImageInfo_missingWidthValue() throws > ImageReadException, IOException { > + byte[] bytes = "P7\nWIDTH \n".getBytes(US_ASCII); > + Map<String, Object> params = Collections.emptyMap(); > + PnmImageParser underTest = new PnmImageParser(); > + underTest.getImageInfo(bytes, params); > + } > } > > > -- E-Mail: garydgreg...@gmail.com | ggreg...@apache.org Java Persistence with Hibernate, Second Edition <http://www.manning.com/bauer3/> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/> Spring Batch in Action <http://www.manning.com/templier/> Blog: http://garygregory.wordpress.com Home: http://garygregory.com/ Tweet! http://twitter.com/GaryGregory