{"record":{"id":"50faa17f5b768747","repo":"bvaughn/react-virtualized","slug":"invalid-offset-offset-specified","errorCode":null,"errorMessage":"Invalid offset ${offset} specified","messagePattern":"Invalid offset (.+?) specified","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"source/Grid/utils/CellSizeAndPositionManager.js","lineNumber":293,"sourceCode":"      interval *= 2;\n    }\n\n    return this._binarySearch(\n      Math.min(index, this._cellCount - 1),\n      Math.floor(index / 2),\n      offset,\n    );\n  }\n\n  /**\n   * Searches for the cell (index) nearest the specified offset.\n   *\n   * If no exact match is found the next lowest cell index will be returned.\n   * This allows partially visible cells (with offsets just before/above the fold) to be visible.\n   */\n  _findNearestCell(offset: number): number {\n    if (isNaN(offset)) {\n      throw Error(`Invalid offset ${offset} specified`);\n    }\n\n    // Our search algorithms find the nearest match at or below the specified offset.\n    // So make sure the offset is at least 0 or no match will be found.\n    offset = Math.max(0, offset);\n\n    const lastMeasuredCellSizeAndPosition = this.getSizeAndPositionOfLastMeasuredCell();\n    const lastMeasuredIndex = Math.max(0, this._lastMeasuredIndex);\n\n    if (lastMeasuredCellSizeAndPosition.offset >= offset) {\n      // If we've already measured cells within this range just use a binary search as it's faster.\n      return this._binarySearch(lastMeasuredIndex, 0, offset);\n    } else {\n      // If we haven't yet measured this high, fallback to an exponential search with an inner binary search.\n      // The exponential search avoids pre-computing sizes for the full set of cells as a binary search would.\n      // The overall complexity for this approach is O(log n).\n      return this._exponentialSearch(lastMeasuredIndex, offset);\n    }","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/bvaughn/react-virtualized/blob/c737715486f724586aee8870ebea1e9efb7b0bfe/source/Grid/utils/CellSizeAndPositionManager.js#L275-L311","documentation":"react-virtualized's CellSizeAndPositionManager._findNearestCell() performs a binary search for the cell nearest a given pixel offset. Because a NaN offset can never match in the search, the method explicitly rejects it with this throw before clamping the offset to 0. It is an internal assertion guarding against bad scroll/size math upstream.","triggerScenarios":"Calling start(offset) (via scrollToPosition or the Grid's scrollTo/scrollToCell flow) with an offset computed as NaN — typically when a cell size getter or estimated cell size yields NaN, or scrollToPosition(NaN)/scrollLeft/scrollTop set to NaN is passed to Grid.","commonSituations":"A custom cellSize/cellRenderer metadata function returning NaN (e.g. dividing by a width of 0), container width/height measured as NaN before layout, passing an unset variable as scrollToPosition, or numbers parsed from undefined in custom scrolling logic.","solutions":["Ensure your cellSizeGetter/estimatedCellSize and container width/height never evaluate to NaN (guard divisions by zero and missing props).","Before calling scrollToPosition or setting scrollLeft/scrollTop, coerce with Number.isFinite(offset) && offset >= 0.","If measuring the container, only render/measurement-start after the DOM node has a real size (e.g. react-virtualized-auto-sizer or onResize guard).","Wrap the Grid render/scroll call in a check that clamps: offset = Math.max(0, Math.floor(offset || 0))."],"exampleFix":"// before\ngrid.scrollToPosition(getScrollOffset()); // getScrollOffset() may return NaN\n\n// after\nconst offset = getScrollOffset();\nif (Number.isFinite(offset)) {\n  grid.scrollToPosition(Math.max(0, offset));\n}","handlingStrategy":"validation","validationCode":"function isSafeOffset(offset) {\n  return typeof offset === 'number' && Number.isFinite(offset) && offset >= 0;\n}\n// before calling scrollToPosition / setting scrollLeft/scrollTop:\nif (!isSafeOffset(rawOffset)) return; // or clamp: Math.max(0, Math.floor(rawOffset || 0))","typeGuard":"function isFiniteNumber(v) {\n  return typeof v === 'number' && Number.isFinite(v);\n}","tryCatchPattern":"try {\n  grid.scrollToPosition(offset);\n} catch (e) {\n  if (String(e.message).startsWith('Invalid offset')) {\n    grid.scrollToPosition(0); // safe fallback\n  } else {\n    throw e;\n  }\n}","preventionTips":["Validate every number passed to scrollToPosition, scrollToCell, scrollLeft/scrollTop with Number.isFinite before use","Only render the grid after container measurements exist (use react-virtualized-auto-sizer) so width/height are never NaN","Guard any arithmetic in cellSizeGetter against zero/undefined operands","Never pass user- or DOM-sourced values straight into scroll APIs without coercion via Math.max(0, Math.floor(x || 0))"],"tags":["nan","invalid-parameter","virtualization","scroll-offset"],"backgroundTag":"invalid-nan-argument","analyzedSha":"c737715486f724586aee8870ebea1e9efb7b0bfe","analyzedAt":"2026-08-30T00:33:38.249Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}