Uuid::get_version() returns Version::Max for non-max UUIDs with a 0xF version nibble #867

Closed
opened 2026-03-15 10:24:59 -05:00 by meng-xu-cs · 1 comment
meng-xu-cs commented 2026-03-15 10:24:59 -05:00 (Migrated from github.com)

Summary

Version::Max could be viewed as the special all-ones UUID, but Uuid::get_version() currently returns Some(Version::Max) for any UUID whose version nibble is 0xF, even when the value is not actually the all-ones max UUID.

Disclaimer: This might be a gap in the [documentation[(https://www.ietf.org/rfc/rfc9562.html#name-max-uuid) rather than an implementation error. The document only formally defines MAX UUID to be more specific, not Version::MAX, and Version::MAX can be considered as the maximum possible version that can be set for UUID, which is indeed 0xF. If this is the case, feel free to close the issue.

Reproduction

use uuid::{Uuid, Variant, Version};

fn main() {
    let non_max = Uuid::from_bytes([
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00,
        0xF0, 0x00,
        0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    ]);

    assert!(!non_max.is_max());
    assert_eq!(non_max.get_variant(), Variant::RFC4122);
    assert_eq!(non_max.get_version_num(), 0xF);

    // Actual behavior:
    assert_eq!(non_max.get_version(), Some(Version::Max));
}

Suggested fix

Gate the Version::Max branch on the full UUID value, mirroring the existing Nil handling:

match self.get_version_num() {
    0 if self.is_nil() => Some(Version::Nil),
    1 => Some(Version::Mac),
    2 => Some(Version::Dce),
    3 => Some(Version::Md5),
    4 => Some(Version::Random),
    5 => Some(Version::Sha1),
    6 => Some(Version::SortMac),
    7 => Some(Version::SortRand),
    8 => Some(Version::Custom),
    0xf if self.is_max() => Some(Version::Max),
    _ => None,
}

A regression test for a non-max UUID with version nibble 0xF would also help:

#[test]
fn non_max_uuid_with_f_version_nibble_is_not_max() {
    let non_max = Uuid::from_bytes([
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00,
        0xF0, 0x00,
        0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    ]);

    assert!(!non_max.is_max());
    assert_eq!(non_max.get_version_num(), 0xF);
    assert_eq!(non_max.get_version(), None);
}

Issue #322 (“Version number more than 5 returned for UUID”) looks related in the sense that it discusses version nibble 15, but this issue is specifically about get_version() misclassifying such values as Version::Max rather than returning None unless the UUID is actually the all-ones max value.

## Summary `Version::Max` could be viewed as the special all-ones UUID, but `Uuid::get_version()` currently returns `Some(Version::Max)` for any UUID whose version nibble is `0xF`, even when the value is not actually the all-ones max UUID. **Disclaimer**: This might be a gap in the [documentation[(https://www.ietf.org/rfc/rfc9562.html#name-max-uuid) rather than an implementation error. The document only formally defines MAX UUID to be more specific, not `Version::MAX`, and `Version::MAX` can be considered as the maximum possible version that can be set for UUID, which is indeed `0xF`. If this is the case, feel free to close the issue. ## Reproduction ```rust use uuid::{Uuid, Variant, Version}; fn main() { let non_max = Uuid::from_bytes([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]); assert!(!non_max.is_max()); assert_eq!(non_max.get_variant(), Variant::RFC4122); assert_eq!(non_max.get_version_num(), 0xF); // Actual behavior: assert_eq!(non_max.get_version(), Some(Version::Max)); } ``` ## Suggested fix Gate the `Version::Max` branch on the full UUID value, mirroring the existing `Nil` handling: ```rust match self.get_version_num() { 0 if self.is_nil() => Some(Version::Nil), 1 => Some(Version::Mac), 2 => Some(Version::Dce), 3 => Some(Version::Md5), 4 => Some(Version::Random), 5 => Some(Version::Sha1), 6 => Some(Version::SortMac), 7 => Some(Version::SortRand), 8 => Some(Version::Custom), 0xf if self.is_max() => Some(Version::Max), _ => None, } ``` A regression test for a non-max UUID with version nibble `0xF` would also help: ```rust #[test] fn non_max_uuid_with_f_version_nibble_is_not_max() { let non_max = Uuid::from_bytes([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]); assert!(!non_max.is_max()); assert_eq!(non_max.get_version_num(), 0xF); assert_eq!(non_max.get_version(), None); } ``` ## Possibly related Issue #322 (“Version number more than 5 returned for UUID”) looks related in the sense that it discusses version nibble `15`, but this issue is specifically about `get_version()` misclassifying such values as `Version::Max` rather than returning `None` unless the UUID is actually the all-ones max value.
KodrAus commented 2026-03-16 22:28:02 -05:00 (Migrated from github.com)

I think it would be more correct to use the suggested fix here; only return Version::MAX for the actual max UUID.

I think it would be more correct to use the suggested fix here; only return Version::MAX for the actual max UUID.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-uuid-rs/uuid#867
No description provided.