Skip to content

Guard against registry values that disappear mid-read - #76

Open
dcalonego wants to merge 1 commit into
Rem0o:masterfrom
dcalonego:fix/null-safety
Open

Guard against registry values that disappear mid-read#76
dcalonego wants to merge 1 commit into
Rem0o:masterfrom
dcalonego:fix/null-safety

Conversation

@dcalonego

Copy link
Copy Markdown
Contributor

Three null dereferences that can each take down the plugin, plus one compiler warning.

The races

HWiNFO rewrites the VSB key on every polling cycle. A value name returned by GetValueNames() can already be gone by the time GetValue() runs a moment later.

GetSensorType cast the result to string and immediately called Trim():

var value = (string)key.GetValue(VALUE_REGISTRY_NAME + index);
var unit = value.Trim()...   // NullReferenceException

The cast yields null rather than throwing, so the exception surfaces on the next line and propagates out of Load().

GetId has the same race on the same value, via .ToString().

UpdateValues dereferenced _key with no check. Dispose() sets _key = null, so an update cycle overlapping a Close() throws inside FanControl's update loop.

On sensor ids

GetId now falls back to an empty unit. That is the same string the existing ?? string.Empty already produces for a value with no unit, so ids stay byte-identical and references stored in user configurations keep resolving. This mattered enough to double-check, since a changed id silently orphans a fan curve.

Also

Drops the unused exception variable in IsActive(). master currently builds with one warning:

HWInfoRegistry.cs(35,30): warning CS0168: The variable 'exception' is declared but never used

After this change the project builds clean.

Testing

Built against master, exercised through a harness against a live HWiNFO instance: 47 sensors, 15 update cycles, 0 exceptions. The races themselves are timing-dependent and I did not manage to reproduce them on demand — these are guards against a failure mode visible in the code, not a fix for an observed crash.

Comment thread HWInfoRegistry.cs Outdated

internal HWInfoRegistryUpdateResult UpdateValues(HWInfoPluginSensor[] sensors)
{
// The key can be closed between two update cycles, for instance when Close()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Update() after Close() is technically impossible, as it is done under lock upstream.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed, thanks. I inferred that race from reading the plugin in isolation, with no visibility into the locking upstream.

The other two guards are a different race: GetValueNames() and GetValue() are separate calls, so GetValue can return null for a name listed a moment earlier and GetSensorType calls Trim() on it. Happy to drop those too if you'd rather not carry the check.

HWiNFO rewrites the VSB key on every polling cycle, so a value name returned by
GetValueNames() can already be gone when GetValue() runs a moment later.

- GetSensorType cast the result to string and called Trim() on it. When the
  value had disappeared the cast produced null and the call threw a
  NullReferenceException, which propagated out of Load().
- GetId has the same race on the same value, through .ToString().

GetId falls back to an empty unit, which is the same string the existing
"?? string.Empty" produces for a value with no unit, so sensor ids stay
byte-identical and references in saved configurations keep resolving.

Also drops the unused exception variable in IsActive() (warning CS0168).
@dcalonego dcalonego changed the title Guard against null registry values and a disposed key Guard against registry values that disappear mid-read Aug 29, 2026
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