Modern Apps git

← Modern-Apps.git

photos: fix decoder zero-size and pixel-layer cast crashes

Commit
140a070c3e347ccd46f0ad0de6eae822c407fd11
Author
Vayun Mathur <[email protected]>
When
2026-11-19 02:32 UTC (17 days ago)
Parents
cf856cf
Three crash paths:

- PhotoEditViewModel.decode and WallpaperViewModel.load
  scaled extreme-aspect images (e.g. 10000x1) so the short
  side rounded to 0, and ImageDecoder.setTargetSize(w, 0)
  threw IllegalArgumentException; only IOException/
  SecurityException were caught, so it crashed the editor /
  wallpaper flow. Coerce both dimensions to at least 1,
  mirroring PhotoOcr and FaceWorker.
- PixelEditController checked the active layer as PixelLayer
  synchronously, then applied the transform async; the main-
  thread completion did (l as PixelLayer) on whatever layer
  had shifted into the captured index meanwhile (delete /
  replace during a slow healing/liquify), throwing
  ClassCastException. All four completions now re-check
  with as? and leave a replaced layer unchanged, mirroring
  AdjustmentEditController.

Verification: ./gradlew :photos:testDevUnitTest
(BUILD SUCCESSFUL), ./check photos office passwords
(exit 0).

Diff

photos/src/main/java/com/vayunmathur/photos/platform/PhotoEditViewModel.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/platform/PhotoEditViewModel.kt b/photos/src/main/java/com/vayunmathur/photos/platform/PhotoEditViewModel.ktindex d45ff5e5f5..140bca277a 100644--- a/photos/src/main/java/com/vayunmathur/photos/platform/PhotoEditViewModel.kt+++ b/photos/src/main/java/com/vayunmathur/photos/platform/PhotoEditViewModel.kt@@ -134,7 +134,7 @@ class PhotoEditViewModel(                     val target = PREVIEW_MAX_DIM                     if (w > target || h > target) {                         val scale = target.toFloat() / maxOf(w, h)-                        decoder.setTargetSize((w * scale).roundToInt(), (h * scale).roundToInt())+                        decoder.setTargetSize((w * scale).roundToInt().coerceAtLeast(1), (h * scale).roundToInt().coerceAtLeast(1))                     }                     decoder.allocator = android.graphics.ImageDecoder.ALLOCATOR_SOFTWARE                 }
photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.kt b/photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.ktindex 29e3c6fc12..5bba735f18 100644--- a/photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.kt+++ b/photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.kt@@ -38,7 +38,8 @@ class PixelEditController(private val vm: PhotoEditViewModel) {             withContext(Dispatchers.Main) {                 vm.updateDocument {                     it.updateLayer(index) { l ->-                        (l as PixelLayer).copy(bitmapRef = BitmapReference(finalBmp))+                        // Layer may have been replaced while the transform ran off-thread.+                        (l as? PixelLayer)?.copy(bitmapRef = BitmapReference(finalBmp)) ?: l                     }                 }             }@@ -58,7 +59,7 @@ class PixelEditController(private val vm: PhotoEditViewModel) {             )             withContext(Dispatchers.Main) {                 vm.updateDocument {-                    it.updateLayer(index) { l -> (l as PixelLayer).copy(bitmapRef = BitmapReference(filled)) }+                    it.updateLayer(index) { l -> (l as? PixelLayer)?.copy(bitmapRef = BitmapReference(filled)) ?: l }                 }             }         }@@ -93,7 +94,7 @@ class PixelEditController(private val vm: PhotoEditViewModel) {             val flipped = Bitmap.createBitmap(src, 0, 0, src.width, src.height, m, true)             withContext(Dispatchers.Main) {                 vm.updateDocument { doc ->-                    doc.updateLayer(index) { l -> (l as PixelLayer).copy(bitmapRef = BitmapReference(flipped)) }+                    doc.updateLayer(index) { l -> (l as? PixelLayer)?.copy(bitmapRef = BitmapReference(flipped)) ?: l }                 }             }         }@@ -120,7 +121,7 @@ class PixelEditController(private val vm: PhotoEditViewModel) {             withContext(Dispatchers.Main) {                 vm.updateDocument { doc ->                     doc.updateLayer(index) { l ->-                        (l as PixelLayer).copy(bitmapRef = BitmapReference(out))+                        (l as? PixelLayer)?.copy(bitmapRef = BitmapReference(out)) ?: l                     }                 }             }
photos/src/main/java/com/vayunmathur/photos/platform/WallpaperHelper.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/platform/WallpaperHelper.kt b/photos/src/main/java/com/vayunmathur/photos/platform/WallpaperHelper.ktindex 3f6ce15488..476d294612 100644--- a/photos/src/main/java/com/vayunmathur/photos/platform/WallpaperHelper.kt+++ b/photos/src/main/java/com/vayunmathur/photos/platform/WallpaperHelper.kt@@ -57,7 +57,7 @@ class WallpaperViewModel(application: Application) : AndroidViewModel(applicatio                     val maxEdge = 4096                     if (w > maxEdge || h > maxEdge) {                         val scale = maxEdge.toFloat() / max(w, h)-                        decoder.setTargetSize((w * scale).roundToInt(), (h * scale).roundToInt())+                        decoder.setTargetSize((w * scale).roundToInt().coerceAtLeast(1), (h * scale).roundToInt().coerceAtLeast(1))                     }                     decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE                 }