Skip to content

Fix NullReferenceException in DidSelectFunctionalUnit on macOS#832

Open
pike00 wants to merge 1 commit into
cyanfish:masterfrom
pike00:fix/macos-null-functional-unit-nre
Open

Fix NullReferenceException in DidSelectFunctionalUnit on macOS#832
pike00 wants to merge 1 commit into
cyanfish:masterfrom
pike00:fix/macos-null-functional-unit-nre

Conversation

@pike00

@pike00 pike00 commented Jun 15, 2026

Copy link
Copy Markdown

Fixes #831.

Problem

DeviceOperator.DidSelectFunctionalUnit dereferences functionalUnit unconditionally in its debug log line. ImageCaptureCore may invoke didSelectFunctionalUnit:error: with a null functionalUnit, so functionalUnit.GetType().Name throws NullReferenceException and scanning crashes (full trace in #831).

Change

-        _logger.LogDebug("DidSelectFunctionalUnit {Unit} {Error}", functionalUnit.GetType().Name, error);
-        SetResultOrError(_unitTcs, functionalUnit, error);
+        _logger.LogDebug("DidSelectFunctionalUnit {Unit} {Error}", functionalUnit?.GetType().Name, error);
+        SetResultOrError(_unitTcs, functionalUnit ?? _device.SelectedFunctionalUnit, error);
  • Null-conditional in the log line so a null unit logs as empty instead of crashing.
  • Fall back to _device.SelectedFunctionalUnit when the callback hands back a null unit, so the null does not propagate into SelectUnit/Scan.

Testing

  • NAPS2.Sdk builds cleanly for net8-macos (the TFM that compiles this file).
  • NAPS2.App.Mac builds and launches.
  • The guard is a strict superset of prior behaviour: the ?? only activates when the value is already null, so it cannot change the existing non-null path.

🤖 Generated with Claude Code

ImageCaptureCore may invoke didSelectFunctionalUnit:error: with a null functionalUnit; the debug log line dereferenced it unconditionally and crashed. Null-guard the log and fall back to SelectedFunctionalUnit. Fixes cyanfish#831.
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.

[macOS] NullReferenceException in DeviceOperator.DidSelectFunctionalUnit when scanner reports a null functional unit

1 participant