Format-specific UUID parsers can report incorrect error diagnostics #880

Closed
opened 2026-04-26 04:05:29 -05:00 by trrk · 0 comments
trrk commented 2026-04-26 04:05:29 -05:00 (Migrated from github.com)

When parsing UUIDs through format-specific types, some invalid inputs produce incorrect error diagnostics.

Reproduction

  • uuid: 1.23.0
use std::str::FromStr;
use uuid::fmt::{Hyphenated, Simple, Urn, Braced};

fn main() {
    // Misleading and internally inconsistent error: "expected length 32 ... found 32".
    // This is parsed as Hyphenated, but it is reported as a Simple-style length error.
    println!("{}", Hyphenated::from_str("550e8400e29b41d4a716446655440000").unwrap_err());

    // Self-contradictory and misclassified error: "expected 12, found 12".
    // Simple has no groups at all.
    println!("{}", Simple::from_str("550e8400-e29b-41d4-a716-446655440000").unwrap_err());

    // Self-contradictory and misclassified error: "expected 12, found 12".
    // The actual problem, the missing `urn:uuid:` prefix, is never mentioned.
    println!("{}", Urn::from_str("550e8400-e29b-41d4-a716-446655440000").unwrap_err());

    // Self-contradictory and misclassified error: "expected 12, found 12".
    // The actual problem, the missing braces, is never mentioned.
    println!("{}", Braced::from_str("550e8400-e29b-41d4-a716-446655440000").unwrap_err());
}

Output:

invalid length: expected length 32 for simple format, found 32
invalid group length in group 4: expected 12, found 12
invalid group length in group 4: expected 12, found 12
invalid group length in group 4: expected 12, found 12

Expected behavior

The parsers should still reject these inputs. However, a format-specific parser should not report an error as if a different UUID format had been requested. The diagnostic should also not report equal expected and actual values, such as expected 12, found 12.

Suspected cause

github.com/uuid-rs/uuid@ca0c85fe21/src/fmt.rs (L871-L909)

The format-specific parsers call their corresponding strict parser first, then convert the lightweight parse failure through InvalidUuid::into_err(). InvalidUuid::into_err() re-analyzes the input string generically in order to construct a detailed Error. As a result, the generated diagnostic may be based on a perspective other than the originally requested format, which can lead to inaccurate error reporting.

When parsing UUIDs through format-specific types, some invalid inputs produce incorrect error diagnostics. ### Reproduction - uuid: 1.23.0 ```rust use std::str::FromStr; use uuid::fmt::{Hyphenated, Simple, Urn, Braced}; fn main() { // Misleading and internally inconsistent error: "expected length 32 ... found 32". // This is parsed as Hyphenated, but it is reported as a Simple-style length error. println!("{}", Hyphenated::from_str("550e8400e29b41d4a716446655440000").unwrap_err()); // Self-contradictory and misclassified error: "expected 12, found 12". // Simple has no groups at all. println!("{}", Simple::from_str("550e8400-e29b-41d4-a716-446655440000").unwrap_err()); // Self-contradictory and misclassified error: "expected 12, found 12". // The actual problem, the missing `urn:uuid:` prefix, is never mentioned. println!("{}", Urn::from_str("550e8400-e29b-41d4-a716-446655440000").unwrap_err()); // Self-contradictory and misclassified error: "expected 12, found 12". // The actual problem, the missing braces, is never mentioned. println!("{}", Braced::from_str("550e8400-e29b-41d4-a716-446655440000").unwrap_err()); } ``` Output: ```text invalid length: expected length 32 for simple format, found 32 invalid group length in group 4: expected 12, found 12 invalid group length in group 4: expected 12, found 12 invalid group length in group 4: expected 12, found 12 ``` ### Expected behavior The parsers should still reject these inputs. However, a format-specific parser should not report an error as if a different UUID format had been requested. The diagnostic should also not report equal expected and actual values, such as `expected 12, found 12`. ### Suspected cause https://github.com/uuid-rs/uuid/blob/ca0c85fe2172e82e9d0c76e659f5c57ceb86d9a4/src/fmt.rs#L871-L909 The format-specific parsers call their corresponding strict parser first, then convert the lightweight parse failure through `InvalidUuid::into_err()`. `InvalidUuid::into_err()` re-analyzes the input string generically in order to construct a detailed Error. As a result, the generated diagnostic may be based on a perspective other than the originally requested format, which can lead to inaccurate error reporting.
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#880
No description provided.