Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected format result with Uuid ToString method #151

Closed
ivila opened this issue Nov 28, 2024 · 3 comments
Closed

Unexpected format result with Uuid ToString method #151

ivila opened this issue Nov 28, 2024 · 3 comments

Comments

@ivila
Copy link
Contributor

ivila commented Nov 28, 2024

Problem

I found some unexpected results when using the ToString method of Uuid, In both optee_utee and optee_teec:

1. the output formation is different with input

the parse_str method require formation of neither 32 bytes or 36 bytes (just what the uuid crate defined), however, the realization of Display trait of optee_teec::Uuid and optee_utee::Uuid will output a str with 35 bytes.
for example:

fn test() {
    let uuid_str = "6bfaa402-50ed-42f2-9bde-b2aecfa216c2";
    let uuid = optee_teec::Uuid::parse_str(uuid_str).unwrap();
    let uuid_str2 = uuid.to_string();  // this is "6bfaa402-50ed-42f2-9bdeb2aecfa216c2", the last "-" is missing
}

2. missing the prefix zeros:

the formation teaclave use is "{:x}-{:x}-{:x}-{}", which will get an unexpected result when format number has zeros at front, for example:

fn main() {
    let uuids = vec![
        "00173366-2aca-49bc-beb7-10c975e6131e",
        "11173366-0aca-49bc-beb7-10c975e6131e",
        "11173366-2aca-09bc-beb7-10c975e6131e",
        "11173366-2aca-19bc-beb7-10c975e6131e",
    ];
    for (i, raw) in uuids.iter().enumerate() {
        let tmp = optee_teec::Uuid::parse_str(raw).unwrap();
        let formatted = tmp.to_string();
        let raw2 = to_same_format(raw);
        println!(
            "str {}\norigin: {}\nparsed: {}\nequal: {}\n",
            i,
            raw2,
            formatted,
            raw2.eq_ignore_ascii_case(&formatted),
        );
    }
}

fn to_same_format(origin: &str) -> String {
    assert_eq!(origin.len(), 36);
    let mut buffer = Vec::with_capacity(35);
    buffer.extend(origin.as_bytes()[0..23].iter());
    buffer.extend(origin.as_bytes()[24..36].iter());
    assert_eq!(buffer.len(), 35);
    String::from_utf8(buffer).unwrap()
}

the codes will produce:

str 0
origin: 00173366-2aca-49bc-beb710c975e6131e
parsed: 173366-2aca-49bc-beb710c975e6131e
equal: false

str 1
origin: 11173366-0aca-49bc-beb710c975e6131e
parsed: 11173366-aca-49bc-beb710c975e6131e
equal: false

str 2
origin: 11173366-2aca-09bc-beb710c975e6131e
parsed: 11173366-2aca-9bc-beb710c975e6131e
equal: false

str 3
origin: 11173366-2aca-19bc-beb710c975e6131e
parsed: 11173366-2aca-19bc-beb710c975e6131e
equal: true

Suggest Solution

I wish we could just produce output string as same as the input string(the 36 bytes format with no missing prefix zeros, as this is how tee-supplicant finds the TA), in short, just change the realization of Display traits, from:

impl fmt::Display for Uuid {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{:x}-{:x}-{:x}-{}",
            self.raw.timeLow,
            self.raw.timeMid,
            self.raw.timeHiAndVersion,
            hex::encode(self.raw.clockSeqAndNode)
        )
    }
}

to

impl fmt::Display for Uuid {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{:08x}-{:04x}-{:04x}-{}-{}",
            self.raw.timeLow,
            self.raw.timeMid,
            self.raw.timeHiAndVersion,
            hex::encode(&self.raw.clockSeqAndNode[0..2]),
            hex::encode(&self.raw.clockSeqAndNode[2..8]),
        )
    }
}
@DemesneGH
Copy link
Contributor

Please feel free to open the PR, thanks!

@DemesneGH
Copy link
Contributor

It's okay to have two commits in a single PR. Thanks!

@ivila
Copy link
Contributor Author

ivila commented Nov 29, 2024

@DemesneGH A PR was created, and the pipeline passed, can you take a look at it?

@ivila ivila closed this as completed Nov 29, 2024
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

No branches or pull requests

2 participants