flakeid icon indicating copy to clipboard operation
flakeid copied to clipboard

IsSnowflake validation incorrectly rejects valid IDs

Open aevitas opened this issue 6 months ago • 0 comments

Description

The IsSnowflake() extension method, used by Id.Parse() and Id.TryParse(), contains faulty validation logic that rejects many validly generated IDs.

The method checks if the decoded thread and process components are strictly greater than zero (> 0). However, a masked thread ID or process ID can validly be 0. For example, if Thread.CurrentThread.ManagedThreadId is 32, masking it with ThreadIdMask (31) will result in 0.

This bug renders the Parse() and TryParse() methods unreliable. They will throw an exception or return false for valid IDs generated by any thread or process whose masked ID is 0. This can lead to unexpected errors, data rejection, and failed deserialization in applications using this library.

Affected line:

    return timestamp > 0 && thread > 0 && process > 0 && increment >= 0;

Suggested Fix

The checks for thread > 0 and process > 0 should be removed. Perfect validation is impossible, but the current logic is detrimental (and worse than no check)

aevitas avatar Jul 29 '25 05:07 aevitas