[Bug]: Unsafe force-unwraps (!!) causing potential NullPointerException crashes in upload and location flows
Summary
Several components in the app use Kotlin's non-null assertion operator (!!) on nullable values, potentially causing unexpected NullPointerException crashes during image processing, file uploads, chunked uploads, and location updates.
The issue affects four locations across ImageProcessingService, FileProcessor, LocationServiceManager, and UploadClient, where missing input streams, location data, or upload results are not handled safely.
These unsafe operations can also bypass existing fallback and error-handling mechanisms, affecting the reliability of upload and location workflows.
Affected Code
1. ImageProcessingService.kt (Line 161)
appContext.contentResolver.openInputStream(originalFilePath)!!
ContentResolver.openInputStream() can return null when the content provider cannot open the requested resource. Using !! on the returned value can cause an immediate NullPointerException instead of allowing the application to handle the unavailable stream gracefully.
2. FileProcessor.kt (Line 176)
contentResolver.openInputStream(Uri.fromFile(file))!!
If the input stream is null, the force-unwrap throws a NullPointerException. Since the surrounding code catches only IOException, the exception bypasses the existing error handling and prevents the intended fallback:
ImageCoordinates(file.absolutePath, null)
This means the file-path fallback may never execute when the input stream is unavailable.
3. LocationServiceManager.kt (Line 209)
lastLocationVar?.let { location.distanceTo(it) }!! >= 500
If lastLocationVar is null, the let expression returns null, and the subsequent !! throws a NullPointerException.
This can cause unexpected crashes during location updates when a previous location is unavailable.
4. UploadClient.kt (Lines 163, 166)
chunkInfo.get()!!.uploadResult!!.offset
chunkInfo.get()!!.uploadResult!!.filekey
These expressions contain multiple force-unwrap operations. Since ChunkInfo.uploadResult is explicitly nullable (UploadResult?), accessing its properties without validating its availability can result in a NullPointerException during chunked uploads.
Steps to Reproduce
- Open the Android app.
- Trigger image processing with an unavailable input stream.
- Trigger a location update when
lastLocationVaris null. - Initiate a chunked upload with a null
uploadResult. - Observe potential
NullPointerExceptioncrashes.
Expected Behavior
Nullable values should be handled safely without relying on force-unwraps (!!):
- ImageProcessingService: Null input streams are handled gracefully through RxJava error propagation.
- FileProcessor: Null input streams are handled without bypassing the existing fallback logic.
- LocationServiceManager: A null
lastLocationVaris handled without causing a crash. - UploadClient:
chunkInfoanduploadResultare validated before accessing their properties.
Actual Behavior
The app can crash with an uncaught NullPointerException when:
openInputStream()returns null during image duplicate detection (ImageProcessingService.kt:161).openInputStream()returns null while reading image coordinates, bypassing the existingIOExceptionfallback (FileProcessor.kt:176).lastLocationVaris null during a location update (LocationServiceManager.kt:209).chunkInfooruploadResultis null during a chunked upload (UploadClient.kt:163, 166).
These failures can interrupt image processing, uploads, and location updates.
Commons app version
6.9.0
Would you like to work on the issue?
Yes
I have already investigated the root cause, implemented the safe null-handling fixes, assign me
Assigned to you, @himanchalkaushale! Thanks!