[英]Catching memory exception during recursive programming in Java Card
Although recursive programming style isn't recommended in Java Card, I want to make a little test on the Fibonacci algorithm. 虽然Java Card不推荐使用递归编程风格,但我想对Fibonacci算法进行一些测试。 I wrote a function that compute the Fibonacci's suite for big integers (represented by byte arrays).
我编写了一个函数来计算Fibonacci的大整数套件(由字节数组表示)。
My code is the following: 我的代码如下:
public static byte[] fibonacci(byte[] n) {
if (isLEThan1(n)) {
return n;
}
else {
return add(fibonacci(subtract(n, new byte[]{0x01})),fibonacci(subtract(n,new byte[]{0x02})));
}
}
where boolean isLEThan(byte[])
returns true
if the integer represented by the byte array is less or equal than 1, false
if not. 其中
boolean isLEThan(byte[])
如果字节数组表示的整数小于或等于1则返回true
否则返回false
。
byte[] add(byte[], byte[])
and byte[] subtract(byte[], byte[])
implement addition and subtraction for big integers represented by byte arrays. byte[] add(byte[], byte[])
和byte[] subtract(byte[], byte[])
实现了由字节数组表示的大整数的加法和减法。 They return a new byte array containing the result of the operation. 它们返回一个包含操作结果的新字节数组。
I thought that by giving a large array to the function described above, I will get an exception such as SystemException.NO_RESOURCE
because of the number of array instantiated by subtract due to recursive calls. 我认为通过给上面描述的函数提供一个大数组,我将得到一个异常,例如
SystemException.NO_RESOURCE
因为由于递归调用而减去实例化的数组。
But I have to think that I don't catch the right exception because I get 6F00
as status word. 但我必须认为我没有抓住正确的例外,因为我将
6F00
作为状态字。
Here's the list of exceptions that I consider: 这是我考虑的例外列表:
try {
fibonacci(array);
} catch (ArithmeticException e) {
ISOException.throwIt((short) 0x0100);
} catch (ArrayStoreException e) {
ISOException.throwIt((short) 0x0200);
} catch (APDUException e) {
ISOException.throwIt(Util.makeShort((byte) 0x03,
(byte) e.getReason()));
} catch (CryptoException e) {
ISOException.throwIt(Util.makeShort((byte) 0x04,
(byte) e.getReason()));
} catch (ISOException e) {
ISOException.throwIt(Util.makeShort((byte) 0x05,
(byte) e.getReason()));
} catch (PINException e) {
ISOException.throwIt(Util.makeShort((byte) 0x06,
(byte) e.getReason()));
} catch (ServiceException e) {
ISOException.throwIt(Util.makeShort((byte) 0x07,
(byte) e.getReason()));
} catch (SystemException e) {
ISOException.throwIt(Util.makeShort((byte) 0x08,
(byte) e.getReason()));
} catch (TransactionException e) {
ISOException.throwIt(Util.makeShort((byte) 0x09,
(byte) e.getReason()));
} catch (ClassCastException e) {
ISOException.throwIt((short) 0x0A00);
} catch (IndexOutOfBoundsException e) {
ISOException.throwIt((short) 0x0B00);
} catch (NegativeArraySizeException e) {
ISOException.throwIt((short) 0x0C00);
} catch (NullPointerException e) {
ISOException.throwIt((short) 0x0D00);
} catch (SecurityException e) {
ISOException.throwIt((short) 0x0E00);
}
}
So, does somebody have an idea about the exception concerned in that case? 那么,在这种情况下,是否有人对有关的例外情况有所了解?
It's a SystemException
. 这是一个
SystemException
。 However, ISO/IEC 7816-4 doesn't allow you to use just any status word. 但是,ISO / IEC 7816-4不允许您仅使用任何状态字。 Instead use eg
(short) (0x6700 | 0x0080 | e.getReason())
as reason for your ISOException
. 而是使用eg
(short) (0x6700 | 0x0080 | e.getReason())
作为ISOException
原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.