Skip to content

Fix crash when returnMapZoom returns NaN or Infinity#295

Open
hamzawaleed01 wants to merge 1 commit into
tomekvenits:masterfrom
hamzawaleed01:nan-zoom-guard
Open

Fix crash when returnMapZoom returns NaN or Infinity#295
hamzawaleed01 wants to merge 1 commit into
tomekvenits:masterfrom
hamzawaleed01:nan-zoom-guard

Conversation

@hamzawaleed01

@hamzawaleed01 hamzawaleed01 commented Apr 10, 2026

Copy link
Copy Markdown

Summary

Fixes #294

superCluster.getClusters(bBox, zoom) crashes when returnMapZoom() returns NaN or Infinity. This happens because the native map can emit regions with NaN/undefined values during initialization or rapid zoom/pan, and @mapbox/geo-viewport's Math.max/Math.min clamping does not catch NaN.

Related Issues

This fix likely resolves or mitigates several open crash reports that share the same root cause — invalid region values propagating into supercluster:

All of these trace back to the same missing validation: returnMapZoom() can return NaN/Infinity, and the library passes it directly to superCluster.getClusters() without checking.

Changes

Added a guard in ClusteredMapView.js to check that zoom is finite before calling getClusters():

  • Initial clustering (line ~102): returns empty markers array if zoom is invalid
  • _onRegionChangeComplete (line ~137): early-returns and still fires the onRegionChangeComplete callback

Why this is the right fix

  • The root issue is in @mapbox/geo-viewport not clamping NaN, but that's an upstream dependency we can't control here
  • Guarding at the getClusters call site is minimal, non-breaking, and handles all edge cases
  • No behavior change for valid zoom values — the guard only activates when zoom is genuinely invalid

The native map can emit regions with NaN or undefined values for
latitude, longitude, latitudeDelta, or longitudeDelta during
initialization or rapid zoom/pan transitions.

When this happens, calculateBBox() produces [NaN, NaN, NaN, NaN],
which flows into GeoViewport.viewport(). The Math.max/Math.min
clamping in geo-viewport does not catch NaN (Math.min(20, NaN) = NaN),
so superCluster.getClusters(bBox, NaN) throws an exception.

This commit adds a guard to check that zoom is finite before calling
getClusters, returning an empty markers array (or early-returning from
the region change handler) when zoom is invalid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash: superCluster.getClusters receives NaN zoom from returnMapZoom

1 participant