Skip to content

fix: guard against invalid color primaries to prevent crash on VMs / ICC-less displays - #1901

Open
loliri wants to merge 1 commit into
Scighost:mainfrom
loliri:fix/screenshot-color-primaries-guard
Open

fix: guard against invalid color primaries to prevent crash on VMs / ICC-less displays#1901
loliri wants to merge 1 commit into
Scighost:mainfrom
loliri:fix/screenshot-color-primaries-guard

Conversation

@loliri

@loliri loliri commented Jul 25, 2026

Copy link
Copy Markdown

问题

VM 或无 ICC profile 的设备上,DisplayAdvancedColorInfo 可能报告 NaN 或越界的色彩 primaries。保存截图时这些畸形值喂给 ICCHelper/lcms2,破坏 GC 堆,导致进程崩溃(0xC0000005)。

修复

GetColorPrimariesFromDisplayInformationAsync 内校验 primaries —— 任一 primary 非有限、超出 (0,1) 范围、或违反 x+y≤1 CIE 物理约束,则 fallback BT709,避免崩溃(不写畸形 ICC chunk)。

范围

单文件改动,纯逻辑兜底。不改默认值、UI、字符串 —— 正常显示器行为不变。

…ICC-less displays

On VMs or devices without an ICC profile, DisplayAdvancedColorInfo can report
NaN or out-of-range primaries. Feeding these to ICCHelper/lcms2 corrupts the
GC heap and crashes (0xC0000005) when saving the screenshot.

Fall back to BT709 when primaries are non-finite, outside (0,1), or violate
the x+y<=1 CIE constraint. No behavior change for normal displays.
@loliri
loliri force-pushed the fix/screenshot-color-primaries-guard branch from bc15ca2 to 628bba6 Compare July 25, 2026 12:31
@Scighost
Scighost requested a review from Copilot July 25, 2026 15:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens screenshot color-management by validating display-reported color primaries (or ICC-derived primaries) before they are used downstream, preventing crashes on VMs / ICC-less or otherwise misreporting displays.

Changes:

  • Validate DisplayAdvancedColorInfo-reported primaries when no ICC profile is available; fallback to BT.709 if invalid.
  • Validate ICC-derived primaries from ICCHelper; fallback to BT.709 if invalid.
  • Add ArePrimariesValid helper enforcing finiteness, (0,1) bounds, and CIE x+y≤1 constraint.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +196 to +200
private static bool ArePrimariesValid(ColorPrimaries p)
{
return Valid(p.Red) && Valid(p.Green) && Valid(p.Blue) && Valid(p.White);
static bool Valid(Vector2 v) => float.IsFinite(v.X) && float.IsFinite(v.Y) && v.X > 0f && v.X < 1f && v.Y > 0f && v.Y < 1f && v.X + v.Y <= 1f;
}
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.

2 participants