afpdev / alpheusafpparser

Library & parser for IBM Advanced Function Presentation (AFP) document/print stream format
GNU General Public License v3.0
17 stars 12 forks source link

ArrayIndexOutOfBoundException when parsing GDD #18

Closed RaphC closed 6 years ago

RaphC commented 7 years ago

Hi,

I'm facing an issue about parsing GDD Structured Field

java.lang.ArrayIndexOutOfBoundsException: -2541 at com.mgz.util.UtilBinaryDecoding.parseShort(UtilBinaryDecoding.java:61) at com.mgz.afp.goca.GDD_GraphicsDataDescriptor.decodeAFP(GDD_GraphicsDataDescriptor.java:54) at com.mgz.afp.parser.AFPParser.parseNextSF(AFPParser.java:128)

The method decodeAfp of the class GDD_GraphicsDataDescriptor, makes a loop on structured data until length is reached. For an unknown reason at the end of the first loop, my param length is -2541 and program try to extract data from structured field with a negative position (my new pos value)

    int actualLength = length!=-1 ? length : sfData.length - offset;
    gddParameters = new ArrayList<GDD_Parameter>();

    int pos=0;
    while(pos<actualLength){
        int paramLength = UtilBinaryDecoding.parseShort(sfData, offset + pos, 2)+1;
        GDD_Parameter gddParameter = GDD_Parameter.buildGDDParameter(sfData,offset+pos,paramLength,config);
        gddParameters.add(gddParameter);
        pos+=paramLength;
    }

If I check the positive value, the error gone and the field seems correclty parsed. Here is my workaround

    while(0<= pos && pos<actualLength){
            .......
            }

I guess the error is located in int paramLength = UtilBinaryDecoding.parseShort(sfData, offset + pos, 2)+1; but I didn't investigate.

FWI, I only have one GDD parameter which is a WindowSpecification instance.

Regards,

RaphC commented 7 years ago

One more thing : The computation of paramLength is a bit strange. We use UtilBinaryDecoding.parseShort ,which return a short primitive, whereas the paramLength field is an integer primitive. Is the root cause can be a cast issue ? Does the method UtilBinaryDecoding.parseInt is more appropriate ?

Regards

afpdev commented 7 years ago

Hi, thank you for reporting this error and for the investigative work you put into it. I think you are right, something is wrong with the calculation of the parameter size which definitely shouldn't come out negative. I guess it's a overrun of the short primitive. I will look into it and hope that I can fix this bug quick so it will be included the next update on 2nd of July.