Skip to content

Fix division by zero in RenderTable intrinsic size methods#178217

Merged
auto-submit[bot] merged 4 commits intoflutter:masterfrom
777genius:fix/table-division-by-zero
Jan 7, 2026
Merged

Fix division by zero in RenderTable intrinsic size methods#178217
auto-submit[bot] merged 4 commits intoflutter:masterfrom
777genius:fix/table-division-by-zero

Conversation

@777genius
Copy link
Contributor

@777genius 777genius commented Nov 9, 2025

Description

This PR fixes a division by zero crash in RenderTable when intrinsic size methods are called on empty tables (0 rows × 0 columns) with non-zero constraints.

The Problem

When RenderTable has 0 columns and intrinsic size methods (computeMinIntrinsicHeight, computeMaxIntrinsicHeight, computeMinIntrinsicWidth, computeMaxIntrinsicWidth) are called with non-zero width constraints, the code calls _computeColumnWidths() without checking if the table is empty. This leads to division by zero at line 1140:

final double delta = (minWidthConstraint - tableWidth) / columns;
// When columns = 0, this crashes with division by zero

The Solution

Added early return checks if (rows * columns == 0) return 0.0; to all four intrinsic size methods, matching the pattern already used by other methods that call _computeColumnWidths():

  • computeDryBaseline (line 1242) - already has the check
  • computeDryLayout (line 1274) - already has the check
  • performLayout (line 1318) - already has the check
  • paint (line 1461) - already has the check

Now all four intrinsic methods are consistent with the rest of the codebase.

Testing

Added comprehensive test coverage (Empty table intrinsic dimensions should not crash) that:

  • Creates an empty table (0×0)
  • Calls all four intrinsic size methods with various constraints
  • Verifies they return 0.0 without crashing

Changes Made

packages/flutter/lib/src/rendering/table.dart:

  • Added empty table check to computeMinIntrinsicWidth (line 963-965)
  • Added empty table check to computeMaxIntrinsicWidth (line 978-980)
  • Added empty table check to computeMinIntrinsicHeight (line 995-997)
  • Added empty table check to computeMaxIntrinsicHeight (line 1016-1019)

packages/flutter/test/rendering/table_test.dart:

  • Added test case Empty table intrinsic dimensions should not crash
  • Tests all four intrinsic methods with both finite and infinite constraints

Related Issues

This fix addresses a potential crash when using RenderTable with intrinsic sizing widgets (like IntrinsicHeight, IntrinsicWidth) on empty tables.

Checklist

Before you create this PR, confirm all the requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth review process.

  • I have read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I have read the Tree Hygiene wiki page, which explains my responsibilities.
  • I have read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I have signed the CLA.
  • I have listed at least one issue that this PR fixes (defensive programming for edge case).
  • I have updated/added relevant documentation (doc comments with ///).
  • I have added new tests that verify the fix works.
  • All existing and new tests are passing (flutter analyze shows no issues).
  • I have followed the breaking change policy and added Data Driven Fixes where applicable (no breaking changes in this PR).

@github-actions github-actions bot added the framework flutter/packages/flutter repository. See also f: labels. label Nov 9, 2025
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 effectively resolves a division-by-zero crash in RenderTable's intrinsic sizing methods for empty tables. The addition of early return checks is a solid approach and aligns with existing patterns in the codebase. The accompanying tests are comprehensive and validate the fix. I have one suggestion to remove a minor code redundancy to improve maintainability.

@Piinks Piinks requested a review from hannah-hyj November 11, 2025 23:09
@flutter-dashboard
Copy link

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Copy link
Member

@hannah-hyj hannah-hyj left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fix and the detailed description!!

Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

LGTM 👍 . Thanks for fixing this!

@justinmc
Copy link
Contributor

justinmc commented Dec 3, 2025

Could you fix the analyzer failure so we can merge this @777genius ?

777genius and others added 3 commits January 6, 2026 18:16
When RenderTable has 0 columns and intrinsic size methods are called
with non-zero width constraints, the _computeColumnWidths() method
attempts to divide by zero at line 1140:

  final double delta = (minWidthConstraint - tableWidth) / columns;

This adds early return checks for empty tables (rows * columns == 0)
to four intrinsic size methods:
- computeMinIntrinsicWidth
- computeMaxIntrinsicWidth
- computeMinIntrinsicHeight
- computeMaxIntrinsicHeight

This matches the pattern already used by other methods that call
_computeColumnWidths(), such as computeDryLayout (line 1274),
performLayout (line 1318), and paint (line 1461).

All four methods now return 0.0 for empty tables, which is consistent
with the behavior of other dimension methods.

Added comprehensive test coverage to verify that empty tables handle
intrinsic size queries without crashing.
The check in computeMaxIntrinsicHeight was redundant since the method
delegates to getMinIntrinsicHeight(), which eventually calls
computeMinIntrinsicHeight() where the same check already exists.

This simplifies the code while maintaining the same functionality.

The checks in computeMinIntrinsicWidth, computeMaxIntrinsicWidth, and
computeMinIntrinsicHeight remain as they either prevent division by
zero or serve as performance optimizations for direct computations.
@Piinks Piinks force-pushed the fix/table-division-by-zero branch from 891bc27 to d2103e9 Compare January 7, 2026 00:20
@Piinks
Copy link
Contributor

Piinks commented Jan 7, 2026

Analysis and merge conflicts should be set now.

@Piinks Piinks added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 7, 2026
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 7, 2026
@auto-submit
Copy link
Contributor

auto-submit bot commented Jan 7, 2026

autosubmit label was removed for flutter/flutter/178217, because - The status or check suite Linux flutter_plugins has failed. Please fix the issues identified (or deflake) before re-applying this label.

@Piinks Piinks added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 7, 2026
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 7, 2026
@auto-submit
Copy link
Contributor

auto-submit bot commented Jan 7, 2026

autosubmit label was removed for flutter/flutter/178217, because - The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label.

@Piinks
Copy link
Contributor

Piinks commented Jan 7, 2026

Confirmed Google testing failure is unrelated to this change.

@Piinks Piinks added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 7, 2026
@auto-submit auto-submit bot added this pull request to the merge queue Jan 7, 2026
Merged via the queue into flutter:master with commit b2d1576 Jan 7, 2026
75 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 7, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 7, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 7, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 7, 2026
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Jan 7, 2026
Roll Flutter from 13b2b91 to 01d37bc (74 revisions)

flutter/flutter@13b2b91...01d37bc

2026-01-07 [email protected] Replace Hybrid Composition wiki page with dev-facing content (flutter/flutter#180642)
2026-01-07 [email protected] Improve code quality in `FlutterActivityTest.java` (flutter/flutter#180585)
2026-01-07 [email protected] Roll Dart SDK to 3.11.0-296.1.beta (flutter/flutter#180633)
2026-01-07 [email protected] Bump target Windows version to 10 (flutter/flutter#180624)
2026-01-07 [email protected] Run hook_user_defines and link_hook integration tests on CI (flutter/flutter#180622)
2026-01-07 [email protected] Fix division by zero in RenderTable intrinsic size methods (flutter/flutter#178217)
2026-01-07 [email protected] Remove more  `requires 24` anotations (flutter/flutter#180116)
2026-01-07 [email protected] Roll Skia from 3971dbb85d45 to c5359a4d720e (1 revision) (flutter/flutter#180631)
2026-01-07 [email protected] Fix TabBar.image does not render at initialIndex for the first time (flutter/flutter#179616)
2026-01-07 [email protected] Roll Skia from 1e7ad625f6f7 to 3971dbb85d45 (3 revisions) (flutter/flutter#180627)
2026-01-07 [email protected] Unpin DDS (flutter/flutter#180571)
2026-01-07 [email protected] Fix DropdownMenuEntry.style not resolved when entry is highlighted (flutter/flutter#178873)
2026-01-07 [email protected] Remove unnecessary `@RequiresApi24` annotations from FlutterFragment methods (flutter/flutter#180117)
2026-01-07 [email protected] Roll Skia from 7fc63228056b to 1e7ad625f6f7 (1 revision) (flutter/flutter#180616)
2026-01-07 [email protected] Roll Fuchsia Linux SDK from QfR2ZFZ5kGTD3raO3... to dTvN_JVSCfGFRasvH... (flutter/flutter#180612)
2026-01-07 [email protected] [ Widget Preview ] Add support for `dart:ffi` imports (flutter/flutter#180586)
2026-01-07 [email protected] Roll Skia from eec90000a899 to 7fc63228056b (2 revisions) (flutter/flutter#180608)
2026-01-07 [email protected] Add --web-define flag for template variable injection in Flutter web builds (flutter/flutter#175805)
2026-01-07 [email protected] docs(engine): update rbe notes (flutter/flutter#180599)
2026-01-06 [email protected] Roll Skia from b6249496c230 to eec90000a899 (3 revisions) (flutter/flutter#180602)
2026-01-06 [email protected] Forward proxy 404 responses to client (flutter/flutter#179858)
2026-01-06 [email protected] Roll Dart SDK from 40a8c6188f7f to d2e3ce177270 (1 revision) (flutter/flutter#180598)
2026-01-06 [email protected] Restore CLI precedence for web headers and HTTPS over web_dev_config.yaml (flutter/flutter#179639)
2026-01-06 [email protected] Roll Skia from f5d9da13d56d to b6249496c230 (1 revision) (flutter/flutter#180596)
2026-01-06 [email protected] Enable misc leak tracking (flutter/flutter#176992)
2026-01-06 [email protected] [hooks] Don't require NDK for Android targets (flutter/flutter#180594)
2026-01-06 [email protected] [skia] Disable legacy non-const SkData APIs (flutter/flutter#179684)
2026-01-06 [email protected] Fix/ios share context menu (flutter/flutter#176199)
2026-01-06 [email protected] Roll Skia from b970aeffa66f to f5d9da13d56d (5 revisions) (flutter/flutter#180588)
2026-01-06 [email protected] Manually bump dependencies (flutter/flutter#180509)
2026-01-06 [email protected] Roll Dart SDK from 8150be8a0e48 to 40a8c6188f7f (2 revisions) (flutter/flutter#180582)
2026-01-06 [email protected] Roll Packages from 12eb764 to d3f860d (2 revisions) (flutter/flutter#180581)
2026-01-06 [email protected] Roll Fuchsia Test Scripts from MHF-UAfO6sVKqSEYk... to nR2ESa1Gd8yPcWo06... (flutter/flutter#180578)
2026-01-06 [email protected] Add tooltip support to PlatformMenuItem and PlatformMenu. (flutter/flutter#180069)
2026-01-06 [email protected] Add DropdownMenuFormField.errorBuilder (flutter/flutter#179345)
2026-01-06 [email protected] Roll Skia from 1845397e11ba to b970aeffa66f (2 revisions) (flutter/flutter#180566)
2026-01-06 [email protected] Don't embed unreferenced assets (flutter/flutter#179251)
2026-01-06 [email protected] Improve documentation about ValueNotifier's behavior (flutter/flutter#179870)
2026-01-06 [email protected] Roll Skia from 904ba00331ca to 1845397e11ba (5 revisions) (flutter/flutter#180558)
2026-01-06 [email protected] Roll Dart SDK from 2fb9ad834c4d to 8150be8a0e48 (1 revision) (flutter/flutter#180557)
2026-01-06 [email protected] Roll Skia from 98c042dde68c to 904ba00331ca (3 revisions) (flutter/flutter#180550)
2026-01-06 [email protected] Roll Dart SDK from ba9f7f790966 to 2fb9ad834c4d (2 revisions) (flutter/flutter#180548)
2026-01-06 [email protected] Roll Fuchsia Linux SDK from ubBGcRaAKWKihQ4ac... to QfR2ZFZ5kGTD3raO3... (flutter/flutter#180547)
2026-01-06 [email protected] Make sure that a DraggableScrollableSheet doesn't crash in 0x0 enviro… (flutter/flutter#180433)
2026-01-06 [email protected] Make sure that a ColorFiltered doesn't crash 0x0 environment (flutter/flutter#180307)
2026-01-06 [email protected] Make sure that a FadeInImage doesn't crash in 0x0 environment (flutter/flutter#180495)
...
ivan-vanyusho pushed a commit to ivan-vanyusho/packages that referenced this pull request Jan 26, 2026
Roll Flutter from 13b2b91 to 01d37bc (74 revisions)

flutter/flutter@13b2b91...01d37bc

2026-01-07 [email protected] Replace Hybrid Composition wiki page with dev-facing content (flutter/flutter#180642)
2026-01-07 [email protected] Improve code quality in `FlutterActivityTest.java` (flutter/flutter#180585)
2026-01-07 [email protected] Roll Dart SDK to 3.11.0-296.1.beta (flutter/flutter#180633)
2026-01-07 [email protected] Bump target Windows version to 10 (flutter/flutter#180624)
2026-01-07 [email protected] Run hook_user_defines and link_hook integration tests on CI (flutter/flutter#180622)
2026-01-07 [email protected] Fix division by zero in RenderTable intrinsic size methods (flutter/flutter#178217)
2026-01-07 [email protected] Remove more  `requires 24` anotations (flutter/flutter#180116)
2026-01-07 [email protected] Roll Skia from 3971dbb85d45 to c5359a4d720e (1 revision) (flutter/flutter#180631)
2026-01-07 [email protected] Fix TabBar.image does not render at initialIndex for the first time (flutter/flutter#179616)
2026-01-07 [email protected] Roll Skia from 1e7ad625f6f7 to 3971dbb85d45 (3 revisions) (flutter/flutter#180627)
2026-01-07 [email protected] Unpin DDS (flutter/flutter#180571)
2026-01-07 [email protected] Fix DropdownMenuEntry.style not resolved when entry is highlighted (flutter/flutter#178873)
2026-01-07 [email protected] Remove unnecessary `@RequiresApi24` annotations from FlutterFragment methods (flutter/flutter#180117)
2026-01-07 [email protected] Roll Skia from 7fc63228056b to 1e7ad625f6f7 (1 revision) (flutter/flutter#180616)
2026-01-07 [email protected] Roll Fuchsia Linux SDK from QfR2ZFZ5kGTD3raO3... to dTvN_JVSCfGFRasvH... (flutter/flutter#180612)
2026-01-07 [email protected] [ Widget Preview ] Add support for `dart:ffi` imports (flutter/flutter#180586)
2026-01-07 [email protected] Roll Skia from eec90000a899 to 7fc63228056b (2 revisions) (flutter/flutter#180608)
2026-01-07 [email protected] Add --web-define flag for template variable injection in Flutter web builds (flutter/flutter#175805)
2026-01-07 [email protected] docs(engine): update rbe notes (flutter/flutter#180599)
2026-01-06 [email protected] Roll Skia from b6249496c230 to eec90000a899 (3 revisions) (flutter/flutter#180602)
2026-01-06 [email protected] Forward proxy 404 responses to client (flutter/flutter#179858)
2026-01-06 [email protected] Roll Dart SDK from 40a8c6188f7f to d2e3ce177270 (1 revision) (flutter/flutter#180598)
2026-01-06 [email protected] Restore CLI precedence for web headers and HTTPS over web_dev_config.yaml (flutter/flutter#179639)
2026-01-06 [email protected] Roll Skia from f5d9da13d56d to b6249496c230 (1 revision) (flutter/flutter#180596)
2026-01-06 [email protected] Enable misc leak tracking (flutter/flutter#176992)
2026-01-06 [email protected] [hooks] Don't require NDK for Android targets (flutter/flutter#180594)
2026-01-06 [email protected] [skia] Disable legacy non-const SkData APIs (flutter/flutter#179684)
2026-01-06 [email protected] Fix/ios share context menu (flutter/flutter#176199)
2026-01-06 [email protected] Roll Skia from b970aeffa66f to f5d9da13d56d (5 revisions) (flutter/flutter#180588)
2026-01-06 [email protected] Manually bump dependencies (flutter/flutter#180509)
2026-01-06 [email protected] Roll Dart SDK from 8150be8a0e48 to 40a8c6188f7f (2 revisions) (flutter/flutter#180582)
2026-01-06 [email protected] Roll Packages from 12eb764 to d3f860d (2 revisions) (flutter/flutter#180581)
2026-01-06 [email protected] Roll Fuchsia Test Scripts from MHF-UAfO6sVKqSEYk... to nR2ESa1Gd8yPcWo06... (flutter/flutter#180578)
2026-01-06 [email protected] Add tooltip support to PlatformMenuItem and PlatformMenu. (flutter/flutter#180069)
2026-01-06 [email protected] Add DropdownMenuFormField.errorBuilder (flutter/flutter#179345)
2026-01-06 [email protected] Roll Skia from 1845397e11ba to b970aeffa66f (2 revisions) (flutter/flutter#180566)
2026-01-06 [email protected] Don't embed unreferenced assets (flutter/flutter#179251)
2026-01-06 [email protected] Improve documentation about ValueNotifier's behavior (flutter/flutter#179870)
2026-01-06 [email protected] Roll Skia from 904ba00331ca to 1845397e11ba (5 revisions) (flutter/flutter#180558)
2026-01-06 [email protected] Roll Dart SDK from 2fb9ad834c4d to 8150be8a0e48 (1 revision) (flutter/flutter#180557)
2026-01-06 [email protected] Roll Skia from 98c042dde68c to 904ba00331ca (3 revisions) (flutter/flutter#180550)
2026-01-06 [email protected] Roll Dart SDK from ba9f7f790966 to 2fb9ad834c4d (2 revisions) (flutter/flutter#180548)
2026-01-06 [email protected] Roll Fuchsia Linux SDK from ubBGcRaAKWKihQ4ac... to QfR2ZFZ5kGTD3raO3... (flutter/flutter#180547)
2026-01-06 [email protected] Make sure that a DraggableScrollableSheet doesn't crash in 0x0 enviro… (flutter/flutter#180433)
2026-01-06 [email protected] Make sure that a ColorFiltered doesn't crash 0x0 environment (flutter/flutter#180307)
2026-01-06 [email protected] Make sure that a FadeInImage doesn't crash in 0x0 environment (flutter/flutter#180495)
...
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants