Skip to content

IsSnowflake validation incorrectly rejects valid IDs #19

Description

@aevitas

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions