Back to Fresco

Missing Zero Dimension Guard in Animation/Rendering Code

.llms/rules/ACR_zero_dimension_guard.md

3.7.02.1 KB
Original Source

Missing Zero Dimension Guard in Animation/Rendering Code

Severity: CRITICAL — D92552358 fixed crash MID 7da93ace143f44f0a0c328eaca7d3029

What to Look For

  • Methods that use width or height variables for frame/bitmap operations
  • getFrame(), loadFrame(), loadNextFrames() or similar animation methods
  • Bitmap creation or scaling operations using dimensions
  • Any code that divides by or indexes with dimension values

When to Flag

  • width or height used without preceding > 0 or != 0 check
  • Frame loading initiated without validating drawable dimensions
  • Bitmap operations that could fail with zero dimensions
  • Pattern: getFrame(frameNumber, width, height) without guards

Do NOT Flag

  • Code already inside if (width > 0 && height > 0) blocks
  • Test code with intentional zero dimensions
  • Code that explicitly handles zero dimensions gracefully
  • Methods that document zero dimensions as valid input

Examples

kotlin
// BAD — no dimension guard, crashes before view layout complete
fun getFrame(frameNumber: Int, width: Int, height: Int): FrameResult {
    if (cachedFrameIndex == null) {
        return loadFrame(frameNumber, width, height)  // ❌ Crashes if width=0
    }
}

// GOOD — guard against zero dimensions
fun getFrame(frameNumber: Int, width: Int, height: Int): FrameResult {
    if (cachedFrameIndex == null || width == 0 || height == 0) {
        return findNearestToRender()  // ✅ Graceful fallback
    }
    return loadFrame(frameNumber, width, height)
}

Recommendation

Always validate width > 0 && height > 0 before:

  1. Loading animation frames
  2. Creating bitmaps
  3. Performing scaling calculations
  4. Any operation that could be called before view layout completes

Evidence

  • D92552358: BufferFrameLoader.getFrame() crashed when called with zero dimensions before view layout
  • D92552368: loadNextFrames() wasted resources loading frames that couldn't be displayed
  • Crash MID: 7da93ace143f44f0a0c328eaca7d3029

Auto-generated by 10x-auto-code-reviewer plugin from analysis of fresco diffs (2025-12-16 to 2026-03-16). Source patterns: D92552358, D92552368