Skip to content

Commit

Permalink
GsfIdProvider: fix resource leak (strict mode violation)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Sergey-Makarov committed Dec 9, 2024
1 parent 1a9f9bb commit 694a50b
Showing 1 changed file with 4 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 694a50b

Please sign in to comment.