Skip to content

UniformGridLayout divide by zero when resized too narrow #10539

@oldnewthing

Description

@oldnewthing

Describe the bug

UniformGridLayout.GetMajorSize() divide by zero at

const int fullLinesCount = itemsCount / itemsPerLine;

itemsPerLine is zero if the available width is smaller than the width of a single item.

The value of itemsPerLine comes from two places.

const unsigned int itemsPerLine =
std::min( // note use of unsigned ints
std::max(1u, std::isfinite(availableSizeMinor)
? static_cast<unsigned int>((availableSizeMinor + MinItemSpacing()) / GetMinorItemSizeWithSpacing(context))
: itemsCount),
std::max(1u, m_maximumRowsOrColumns));

return std::min(
static_cast<unsigned int>((availableSizeMinor + MinItemSpacing()) / GetMinorItemSizeWithSpacing(context)),
maximumRowsOrColumns);

This is basically a copy-pasta. In both cases, if GetMinorItemSizeWithSpacing is greater than availableSizeMinor + MinItemSpacing(), then itemsPerLine is zero, which leads to the divide by zero.

itemsPerLine should probably be clamped so it cannot go below 1.

Steps to reproduce the bug

  1. Download, build, and run the WinUINotes sample.
  2. Create two notes.
  3. Go back to the AllNotesPage and resize the window to be as narrow as possible.
  4. When you get it narrower than the width of a single note, the UniformGridLayout crashes with a divide by zero.

This issue was copied from MicrosoftDocs/windows-topic-specific-samples#3

Expected behavior

No response

Screenshots

No response

NuGet package version

None

Windows version

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions