apps-android-commons icon indicating copy to clipboard operation
apps-android-commons copied to clipboard

[Bug]: Unsafe force-unwraps (!!) causing potential NullPointerException crashes in upload and location flows

Open himanchalkaushale opened this issue 4 weeks ago • 2 comments

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

  1. Open the Android app.
  2. Trigger image processing with an unavailable input stream.
  3. Trigger a location update when lastLocationVar is null.
  4. Initiate a chunked upload with a null uploadResult.
  5. Observe potential NullPointerException crashes.

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 lastLocationVar is handled without causing a crash.
  • UploadClient: chunkInfo and uploadResult are validated before accessing their properties.

Actual Behavior

The app can crash with an uncaught NullPointerException when:

  1. openInputStream() returns null during image duplicate detection (ImageProcessingService.kt:161).
  2. openInputStream() returns null while reading image coordinates, bypassing the existing IOException fallback (FileProcessor.kt:176).
  3. lastLocationVar is null during a location update (LocationServiceManager.kt:209).
  4. chunkInfo or uploadResult is 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

himanchalkaushale avatar Nov 06 '26 20:11 himanchalkaushale

I have already investigated the root cause, implemented the safe null-handling fixes, assign me

himanchalkaushale avatar Nov 06 '26 20:11 himanchalkaushale

Assigned to you, @himanchalkaushale! Thanks!

nicolas-raoul avatar Nov 06 '26 21:11 nicolas-raoul