Skip to content

Conversation

@cactuser-Lu
Copy link
Contributor

@cactuser-Lu cactuser-Lu commented Nov 3, 2025

fix: prevent array bounds error in getColumnWidth calculation

当colSpan过大时,getColumnWidth 中数组越界,将产生样式错乱

c71ef100eac0c9f759dcaaf3c195e584 4c1debeb6132812c2175aa1e534534e5

Summary by CodeRabbit

发布说明

  • 优化改进
    • 改进了列宽度计算的内部逻辑,增强了边界验证和数据安全性。
    • 优化了多列跨度场景下的计算准确性。

@vercel
Copy link

vercel bot commented Nov 3, 2025

Someone is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Nov 3, 2025

概览

VirtualCell.tsx 中的 getColumnWidth 函数添加了显式返回类型 number。内部计算逻辑得到增强,现在包括边界范围检查、使用起始/结束偏移量及备用值、通过 Math.max 确保返回非负宽度值,增强了对跨多列情况的安全性。

变更

文件组 / 文件 变更总结
返回类型补全
src/VirtualTable/VirtualCell.tsx
为导出函数 getColumnWidth 添加显式返回类型 : number;优化内部计算逻辑以包含边界范围检查、偏移量备用处理和非负值保证

代码审查工作量评估

🎯 2 (简单) | ⏱️ ~8 分钟

需要关注的方面:

  • 验证新的边界范围检查逻辑是否正确处理所有边界情况(例如,colIndex + colSpan 超出 columnsOffset 数组范围时)
  • 确认 Math.max(endOffset - startOffset, 0) 的处理是否符合预期的宽度计算行为
  • 检查所有现有调用点是否与新的返回类型兼容

诗歌

🐰 小小列宽算得妙,边界检查显功劳,
显式类型明心意,Math.max 保安全,
虚拟表格更稳健!✨

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR 标题清晰准确地反映了变更的核心内容。该标题准确总结了主要改动:向 getColumnWidth 函数添加了边界检查(bounds check),以修复当 colSpan 过大时导致的数组越界错误。标题简洁明了,没有使用模糊术语或不必要的噪音,完全对应 PR 的实际目标和实现内容。
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @cactuser-Lu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses and fixes a critical array out-of-bounds error in the getColumnWidth utility function within VirtualCell.tsx. The issue manifested as visual style inconsistencies when colSpan values exceeded the available column offsets, leading to an incorrect calculation of column widths. The changes introduce defensive programming to ensure array accesses are always within valid boundaries, thereby stabilizing the table rendering and preventing visual glitches.

Highlights

  • Array Bounds Check: Implemented robust bounds checking within the getColumnWidth function to prevent columnsOffset array out-of-bounds access, which previously led to incorrect calculations.
  • Style Disorder Fix: Resolved a visual bug where an excessively large colSpan value caused style inconsistencies in the table rendering due to the aforementioned array index errors.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link

codecov bot commented Nov 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.10%. Comparing base (6a249a2) to head (2577064).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1399   +/-   ##
=======================================
  Coverage   96.09%   96.10%           
=======================================
  Files          57       57           
  Lines        3434     3438    +4     
  Branches      632      633    +1     
=======================================
+ Hits         3300     3304    +4     
  Misses        129      129           
  Partials        5        5           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request aims to fix an array out-of-bounds error in the getColumnWidth function when colSpan is large. The proposed change correctly handles the out-of-bounds access for large colSpan values. However, it introduces a new bug in calculating the width for columns that start from the first column (colIndex 0). The current implementation incorrectly calculates the start offset in this scenario, leading to an incorrect width calculation. I've provided a corrected implementation that is more robust and handles all edge cases correctly.

Comment on lines +37 to +42
const startIndex = Math.max(colIndex, 0);
const endIndex = Math.min(startIndex + mergedColSpan, columnsOffset.length - 1);

const startOffset = columnsOffset[startIndex] || 0;
const endOffset = columnsOffset[endIndex] || startOffset;
return Math.max(endOffset - startOffset, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

While this change fixes the out-of-bounds access when colSpan is large, it introduces a new issue. When calculating the width for a span starting at the first column (i.e., when colIndex is -1), the startIndex becomes 0, and startOffset is incorrectly calculated as columnsOffset[0] instead of 0. This results in an incorrect width calculation.

I suggest a more robust implementation that correctly handles all edge cases, including negative colIndex, large colSpan, and out-of-bounds colIndex values, while being more readable.

  const len = columnsOffset.length;
  const lastOffset = len > 0 ? columnsOffset[len - 1] : 0;

  const startIdx = colIndex;
  const endIdx = colIndex + mergedColSpan;

  const startOffset = startIdx < 0 ? 0 : (startIdx >= len ? lastOffset : columnsOffset[startIdx]);
  const endOffset = endIdx < 0 ? 0 : (endIdx >= len ? lastOffset : columnsOffset[endIdx]);

  return Math.max(endOffset - startOffset, 0);

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/VirtualTable/VirtualCell.tsx (1)

34-34: 建议添加 JSDoc 文档说明参数约束。

考虑为函数添加文档注释,说明参数的预期范围和边界情况的处理方式:

 /**
  * Return the width of the column by `colSpan`.
  * When `colSpan` is `0` will be trade as `1`.
+ * 
+ * @param colIndex - Starting column index (can be negative, will be clamped to 0)
+ * @param colSpan - Number of columns to span (0 will be treated as 1)
+ * @param columnsOffset - Array of cumulative column offsets
+ * @returns The calculated width (always non-negative)
  */
 export function getColumnWidth(colIndex: number, colSpan: number, columnsOffset: number[]): number {

这能帮助其他开发者理解函数如何处理边界情况,特别是负数 colIndex 的处理(参见第 78 行的调用场景)。

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6a249a2 and 2577064.

📒 Files selected for processing (1)
  • src/VirtualTable/VirtualCell.tsx (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test / react component workflow
🔇 Additional comments (1)
src/VirtualTable/VirtualCell.tsx (1)

34-43: 边界检查实现完整正确,有效防止数组越界。

验证结果确认:

  • 负数索引处理: 当 colIndex = 0 时,startColIndex = -1 通过 Math.max(colIndex, 0) 安全处理,防止数组访问错误。
  • 超大 colSpan 处理: Math.min(startIndex + mergedColSpan, columnsOffset.length - 1) 正确限制 endIndex 在有效范围内,返回从起始位置到最后可用列的总宽度。
  • 零值处理: colSpan || 1 正确处理零值,视为跨度 1。
  • 防御性编程: 通过 || 0|| startOffset 提供后备值,确保不会出现 undefined 计算。
  • 非负结果: Math.max(endOffset - startOffset, 0) 保证返回值非负。

实现代码现已通过验证,所有边界情况均被正确处理。

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.

1 participant