Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BERTLVPackager throws a java.lang.ArithmeticException: divide by zero #315

Closed
christopher-grech opened this issue Jun 12, 2020 · 11 comments
Closed

Comments

@christopher-grech
Copy link

Whenever a tag is specified with a length of zero, rather than the tag getting ignored, the implementation is throwing a divide by zero exception.

Looking through the code, I believe that changing lines 259 - 272 from:

if (length > 0) {
    System.arraycopy(b, consumed, value, 0, value.length);
}

int uninterpretLength = getUninterpretLength(length, valueInterpreter);
byte[] rawValueBytes =
        valueInterpreter.uninterpret(value, 0, uninterpretLength);

tlvSubFieldData = unpackValue(tag, rawValueBytes, subFieldNumber, length);


consumed = consumed + length;
ISOTaggedField tlv = new ISOTaggedField(tag, tlvSubFieldData);
m.set(tlv);

to:

if (length > 0) {
    System.arraycopy(b, consumed, value, 0, value.length);


    int uninterpretLength = getUninterpretLength(length, valueInterpreter);
    byte[] rawValueBytes =
            valueInterpreter.uninterpret(value, 0, uninterpretLength);

    tlvSubFieldData = unpackValue(tag, rawValueBytes, subFieldNumber, length);


    consumed = consumed + length;
    ISOTaggedField tlv = new ISOTaggedField(tag, tlvSubFieldData);
    m.set(tlv);
}

should solve the issue.

The problem here is that both getUninterpretLength() and unpackValue() will fail with this exception, however, there's no need to call them if the data length is indeed zero.

@christopher-grech christopher-grech changed the title java.lang.ArithmeticException: divide by zero in BERTLVPackager BERTLVPackager throws a java.lang.ArithmeticException: divide by zero Jun 12, 2020
@christopher-grech
Copy link
Author

We're currently using version 2.1.3, just in case you'll need that info.

@ar
Copy link
Member

ar commented Jun 12, 2020

Thanks for the bug report and suggested solution.

@christopher-grech
Copy link
Author

Happy to help.

@ar
Copy link
Member

ar commented Jun 12, 2020

The Exception is definitely an error, but I believe a zero length tag is still valid and need to be encoded anyway.

@christopher-grech
Copy link
Author

Fair enough. So will it be encoded with a null value then?

ar added a commit that referenced this issue Jun 12, 2020
@ar
Copy link
Member

ar commented Jun 12, 2020

I just added a test for a zero length tag, and it works OK. Perhaps you can run it there
(gradle test --tests=BERTLVPackagerTest --info) and eventually provide an example of a failing test that produces your exception so we can get our arms around the problem.

christopher-grech added a commit to christopher-grech/jPOS that referenced this issue Jun 12, 2020
@ar ar closed this as completed in 929ea8e Jun 12, 2020
@christopher-grech
Copy link
Author

Awesome! Is there a planned release date for this fix?

@ar
Copy link
Member

ar commented Jun 17, 2020

Q4/2020 is a reasonable bet, but you can use a pinned SNAPSHOT meanwhile.

@christopher-grech
Copy link
Author

Is that something you need to do, the pinned SNAPSHOT?

@ar
Copy link
Member

ar commented Jun 30, 2020

No, you just specify the full snapshot filename, including timestamp.
See http://jpos.org/maven/org/jpos/jpos/2.1.5-SNAPSHOT/

@christopher-grech
Copy link
Author

Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants