Skip to content

Commit

Permalink
refactoring: property length check
Browse files Browse the repository at this point in the history
  • Loading branch information
keepsimple1 committed Feb 10, 2025
1 parent 818f37c commit adf964f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/service_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,13 +721,15 @@ fn encode_txt<'a>(properties: impl Iterator<Item = &'a TxtProperty>) -> Vec<u8>
}

// Property that exceed the length are truncated
if s.len() > u8::MAX as usize {
let sz: u8 = s.len().try_into().unwrap_or_else(|_| {
debug!("Property {} is too long, truncating to 255 bytes", prop.key);
s.resize(u8::MAX as usize, 0);
}
u8::MAX
});

// TXT uses (Length,Value) format for each property,
// i.e. the first byte is the length.
bytes.push(s.len().try_into().unwrap());
bytes.push(sz);
bytes.extend(s);
}
if bytes.is_empty() {
Expand Down

0 comments on commit adf964f

Please sign in to comment.