From 5a34a2aefd11b59a42f0e55439686c07053d1bb6 Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Fri, 6 Dec 2024 16:26:36 +0300 Subject: [PATCH 1/3] GsfIdProvider: fix resource leak (strict mode violation) Previously, `cursor.getString(1)` could return `null`, which then led to `NullPointerException` and the cursor not being closed. The new code always returns the same value as the old one + ensures that the cursor is closed properly. --- .../device_id_providers/GsfIdProvider.kt | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/fingerprint/src/main/java/com/fingerprintjs/android/fingerprint/device_id_providers/GsfIdProvider.kt b/fingerprint/src/main/java/com/fingerprintjs/android/fingerprint/device_id_providers/GsfIdProvider.kt index 7f0e63d..60af9e3 100644 --- a/fingerprint/src/main/java/com/fingerprintjs/android/fingerprint/device_id_providers/GsfIdProvider.kt +++ b/fingerprint/src/main/java/com/fingerprintjs/android/fingerprint/device_id_providers/GsfIdProvider.kt @@ -22,26 +22,11 @@ public class GsfIdProvider( val URI = Uri.parse(URI_GSF_CONTENT_PROVIDER) val params = arrayOf(ID_KEY) return try { - val cursor = contentResolver!! - .query(URI, null, null, params, null) - - if (cursor == null) { - return null - } - - if (!cursor.moveToFirst() || cursor.columnCount < 2) { - cursor.close() - return null - } - try { - val result: String? = java.lang.Long.toHexString(cursor.getString(1).toLong()) - cursor.close() - result - } catch (e: NumberFormatException) { - cursor.close() - null + contentResolver!!.query(URI, null, null, params, null)!!.use { cursor -> + check(cursor.moveToFirst() && cursor.columnCount >= 2) + java.lang.Long.toHexString(cursor.getString(1).toLong()) } - } catch (e: Exception) { + } catch (_: Exception) { null } } From 2273a12b2414f33eac367960c60be2b44d2dc887 Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Fri, 6 Dec 2024 17:20:38 +0300 Subject: [PATCH 2/3] CpuInfoProvider.cpuinfo(): fix resource leak (strict mode violation) This commit also addresses the issue https://github.com/fingerprintjs/fingerprintjs-android/issues/118 --- .../fingerprint/info_providers/CpuInfoProvider.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fingerprint/src/main/java/com/fingerprintjs/android/fingerprint/info_providers/CpuInfoProvider.kt b/fingerprint/src/main/java/com/fingerprintjs/android/fingerprint/info_providers/CpuInfoProvider.kt index abde100..9518d77 100644 --- a/fingerprint/src/main/java/com/fingerprintjs/android/fingerprint/info_providers/CpuInfoProvider.kt +++ b/fingerprint/src/main/java/com/fingerprintjs/android/fingerprint/info_providers/CpuInfoProvider.kt @@ -59,11 +59,12 @@ internal class CpuInfoProviderImpl : private fun getCpuInfo(): Map { val map: MutableMap = HashMap() - val s = Scanner(File(CPU_INFO_PATH)) - while (s.hasNextLine()) { - val cpuInfoValues = s.nextLine()!!.split(KEY_VALUE_DELIMITER) - if (cpuInfoValues.size > 1) map[cpuInfoValues[0].trim { it <= ' ' }] = - cpuInfoValues[1].trim { it <= ' ' } + Scanner(File(CPU_INFO_PATH)).use { s -> + while (s.hasNextLine()) { + val cpuInfoValues = s.nextLine()!!.split(KEY_VALUE_DELIMITER) + if (cpuInfoValues.size > 1) map[cpuInfoValues[0].trim { it <= ' ' }] = + cpuInfoValues[1].trim { it <= ' ' } + } } return map From 76e3c21a2c48d45772b19f8e72a9c80d79173dcd Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Fri, 6 Dec 2024 16:34:31 +0300 Subject: [PATCH 3/3] Add strict mode for debug builds Tested on a few emulators - no crashes so far. --- .../fingerprintjs/android/playground/App.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/fingerprintjs/android/playground/App.kt b/app/src/main/java/com/fingerprintjs/android/playground/App.kt index b876903..4dfd95e 100644 --- a/app/src/main/java/com/fingerprintjs/android/playground/App.kt +++ b/app/src/main/java/com/fingerprintjs/android/playground/App.kt @@ -1,6 +1,7 @@ package com.fingerprintjs.android.playground import android.app.Application +import android.os.StrictMode import com.fingerprintjs.android.playground.di.AppComponent import com.fingerprintjs.android.playground.di.DaggerAppComponent @@ -10,10 +11,26 @@ class App : Application() { private set override fun onCreate() { + setupStrictMode() super.onCreate() appComponent = DaggerAppComponent .builder() .app(this) .build() } -} \ No newline at end of file + + private fun setupStrictMode() { + if (BuildConfig.DEBUG) { + StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder() + .detectAll() + .penaltyLog() + .penaltyDeath() + .build()) + StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder() + .detectAll() + .penaltyLog() + .penaltyDeath() + .build()) + } + } +}