From 2884864e2e4af856880e5121650e92876938466f Mon Sep 17 00:00:00 2001 From: kari-ts Date: Tue, 19 Nov 2024 11:33:52 -0800 Subject: [PATCH] android: handle null query results in ShareActivity If contentResolver.query returns null, or the URI is invalid, skip processing and log instead of crashing. Also, use 'use' for the cursor instead of 'let' to automatically close the cursor after processing. Fixes tailscale/corp#24293 Signed-off-by: kari-ts --- .../java/com/tailscale/ipn/ShareActivity.kt | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/android/src/main/java/com/tailscale/ipn/ShareActivity.kt b/android/src/main/java/com/tailscale/ipn/ShareActivity.kt index 44a583770a..3e121f13e4 100644 --- a/android/src/main/java/com/tailscale/ipn/ShareActivity.kt +++ b/android/src/main/java/com/tailscale/ipn/ShareActivity.kt @@ -92,33 +92,25 @@ class ShareActivity : ComponentActivity() { } } - val pendingFiles: List = - uris?.filterNotNull()?.mapNotNull { - contentResolver?.query(it, null, null, null, null)?.let { c -> - val nameCol = c.getColumnIndex(OpenableColumns.DISPLAY_NAME) - val sizeCol = c.getColumnIndex(OpenableColumns.SIZE) - c.moveToFirst() - val name: String = - c.getString(nameCol) - ?: run { - // For some reason, some content resolvers don't return a name. - // Try to build a name from a random integer plus file extension - // (if type can be determined), else just a random integer. - val rand = Random.nextLong() - contentResolver.getType(it)?.let { mimeType -> - MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType)?.let { - extension -> - "$rand.$extension" - } ?: "$rand" - } ?: "$rand" + val pendingFiles: List = + uris?.filterNotNull()?.mapNotNull { uri -> + contentResolver?.query(uri, null, null, null, null)?.use { cursor -> + val nameCol = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) + val sizeCol = cursor.getColumnIndex(OpenableColumns.SIZE) + + if (cursor.moveToFirst()) { + val name: String = cursor.getString(nameCol) + ?: generateFallbackName(uri) + val size: Long = cursor.getLong(sizeCol) + Ipn.OutgoingFile(Name = name, DeclaredSize = size).apply { + this.uri = uri } - val size = c.getLong(sizeCol) - c.close() - val file = Ipn.OutgoingFile(Name = name, DeclaredSize = size) - file.uri = it - file - } - } ?: emptyList() + } else { + TSLog.e(TAG, "Cursor is empty for URI: $uri") + null + } + } + } ?: emptyList() if (pendingFiles.isEmpty()) { TSLog.e(TAG, "Share failure - no files extracted from intent") @@ -126,4 +118,11 @@ class ShareActivity : ComponentActivity() { requestedTransfers.set(pendingFiles) } + + private fun generateFallbackName(uri: Uri): String { + val randomId = Random.nextLong() + val mimeType = contentResolver?.getType(uri) + val extension = mimeType?.let { MimeTypeMap.getSingleton().getExtensionFromMimeType(it) } + return if (extension != null) "$randomId.$extension" else randomId.toString() +} }