Skip to content

Potential divide-by-zero in aspect ratio calculations #13

Description

@BadAtCaptchas

Description

Aspect ratio is calculated as width / height without checking if height is zero. This could occur with corrupted images or during loading before dimensions are set.

Affected Files

components/canvas-editor.tsx, components/glb-preview.tsx

Current Behavior

// Multiple occurrences without zero-check:
const aspectRatio = croppedImg.width / croppedImg.height    // Lines ~653, ~1026, ~1225
const aspectRatio = layer.width / layer.height              // Lines ~1605, ~1804
const aspectRatio = croppedThumbnail.width / croppedThumbnail.height  // Line ~2868
const aspectRatio = rendererCanvas.width / rendererCanvas.height      // Line 293 in glb-preview

Impact

  • If height is 0, aspectRatio becomes Infinity
  • Subsequent calculations using aspectRatio will produce invalid dimensions
  • Could cause NaN/Infinity to propagate through layer positioning

Suggested Fix

Add defensive checks:

const aspectRatio = height > 0 ? width / height : 1
// Or throw an error for invalid dimensions:
if (height <= 0 || width <= 0) {
  console.error('Invalid dimensions:', { width, height })
  return
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions