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

PLEASE FIX CRASH BUG IN FTReaderAPI, in method com.ftsafe.comm.bt4.BT4.ft_find() #30

Closed
boby1975 opened this issue Dec 16, 2021 · 1 comment

Comments

@boby1975
Copy link

boby1975 commented Dec 16, 2021

Crash occurs in method "com.ftsafe.comm.bt4.BT4.ft_find()" when for example the method "FT_SCardListReaders()" of PCSC API is called. It is not always but very often.

How to reproduce:

1) open project FEITIAN_MOBILE_READERS-master1.0.9.2\Android_SDK\new_android_sdk\PCSC_API\Demo code\pcscdemo
2) unhide (android:visibility="visible") button id="@+id/btn_list_readers" in "activity_main.xml" layout
3) add code below in "MainActivity", public void onClick,case R.id.btn_list_readers:

byte[] readerNames = new byte[1024];
ret = PCSCNative.SCardListReaders(readerNames);
showMessage("PCSCNative.SCardListReaders ret : 0x" + Integer.toHexString(ret));
String[] readers = new String(readerNames).split("\0");
if (readers.length > 0) showMessage("PCSCNative.SCardListReaders readers[0]: " + readers[0]);

4) build and run app, turn on BLE reader
5) press button "ESTABLISH", then press button "GET READER NAME" several times or "ESTABLISH" / "GET READER NAME", very often will be crash.

info from logcat when crash occurs:

2021-12-16 14:38:58.501 19328-19328/com.ftsafe.pcscdemo A/ftsafe.pcscdem: java_vm_ext.cc:578] JNI DETECTED ERROR IN APPLICATION: JNI GetByteArrayElements called with pending exception java.util.ConcurrentModificationException: 
java_vm_ext.cc:578]   at java.lang.Object java.util.ArrayList$Itr.next() (ArrayList.java:860)
java_vm_ext.cc:578]   at void com.ftsafe.comm.bt4.BT4.ft_find() (BT4.java:131)
java_vm_ext.cc:578]   at int com.ftsafe.comm.CommBase.listReaders(byte[], int[]) (CommBase.java:310)
java_vm_ext.cc:578]   at int com.ftsafe.pcscdemo.PCSCNative.SCardListReaders(byte[]) (PCSCNative.java:-2)
java_vm_ext.cc:578]   at void com.ftsafe.pcscdemo.MainActivity.onClick(android.view.View) (MainActivity.java:200)

Why this is happen:

THIS IS BECAUSE DURING ITERATING THROUGH ARRAY LIST arrayForBlueToothDevice in ft_find() THIS LIST CAN BE MODIFIED (ADD NEW ELEMENT) AT ANY TIME BY newLeScanCallback!!!
System will return Bluetooth LE scan results batched on bluetooth controller. Returns immediately, batch scan results data will be delivered through the callback at any time. Therefore situations may arise with a high probability when at the same time there is a passage through the list arrayForBlueToothDevice in ft_find() and its modification in newLeScanCallback which causes an uncaught ConcurrentModificationException and app crashes in such cases.

How to fix:

solution#1
Surround block "while (var3.hasNext())" in "com.ftsafe.comm.bt4.BT4.ft_find()" by try/catch like this

        if (this.mBlueToothAdapter.isDiscovering() && this.scanning) {
            Iterator var3 = this.arrayForBlueToothDevice.iterator();
            try {
                while (var3.hasNext()) {
                    BluetoothDevice device = (BluetoothDevice) var3.next();
                    this.mHandlerSendMessage(129, device);
                }
            } catch (java.util.ConcurrentModificationException e) {
                Log.e("ft_find", "THIS IS BECAUSE ARRAYLIST arrayForBlueToothDevice WAS MODIFIED IN newLeScanCallback DURING ITERATING THROUGH THIS ARRAYLIST!!!");
                e.printStackTrace();
            }
        }

It was tested. It works. Instead of crashes we catch ConcurrentModificationException in such situation.
info from logcat:

2021-12-16 14:20:17.918 17363-17363/com.ftsafe.pcscdemo E/ft_find: THIS IS BECAUSE ARRAYLIST arrayForBlueToothDevice WAS MODIFIED IN newLeScanCallback DURING ITERATING THROUGH THIS ARRAYLIST!!!
2021-12-16 14:20:17.919 17363-17363/com.ftsafe.pcscdemo W/System.err: java.util.ConcurrentModificationException
2021-12-16 14:20:17.921 17363-17363/com.ftsafe.pcscdemo W/System.err:     at java.util.ArrayList$Itr.next(ArrayList.java:860)
2021-12-16 14:20:17.921 17363-17363/com.ftsafe.pcscdemo W/System.err:     at com.ftsafe.comm.bt4.BT4.ft_find(BT4.java:150)
2021-12-16 14:20:17.922 17363-17363/com.ftsafe.pcscdemo W/System.err:     at com.ftsafe.comm.CommBase.listReaders(CommBase.java:310)
2021-12-16 14:20:17.922 17363-17363/com.ftsafe.pcscdemo W/System.err:     at com.ftsafe.pcscdemo.PCSCNative.SCardListReaders(Native Method)
2021-12-16 14:20:17.922 17363-17363/com.ftsafe.pcscdemo W/System.err:     at com.ftsafe.pcscdemo.MainActivity.onClick(MainActivity.java:200)

solution#2
Use CopyOnWriteArrayList for arrayForBlueToothDevice instead of ArrayList. Not tested yet.

PS
I guess this is an old and critical bug and should be fixed ASAP.

@boby1975 boby1975 changed the title PLEASE FIX CRASH BUG IN FTReaderAPI_xxx.jar PLEASE FIX CRASH BUG IN FTReaderAPI Dec 16, 2021
@boby1975
Copy link
Author

Bug fixed in FTReaderAPI_1.0.9.3.jar.
Thnks.

@boby1975 boby1975 changed the title PLEASE FIX CRASH BUG IN FTReaderAPI PLEASE FIX CRASH BUG IN FTReaderAPI, in method com.ftsafe.comm.bt4.BT4.ft_find() Dec 19, 2021
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