ivila opened a new issue, #151:
URL: https://github.com/apache/incubator-teaclave-trustzone-sdk/issues/151

   ## 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](https://crates.io/crates/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:
   ```rust
   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:
   ```rust
   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: 
   ```shell
   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:
   ```rust
   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
   ```rust
   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]),
           )
       }
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org
For additional commands, e-mail: dev-h...@teaclave.apache.org

Reply via email to