Skip to content

Commit

Permalink
feat: Truncate the string if it exceeds the length
Browse files Browse the repository at this point in the history
  • Loading branch information
mycrl committed Feb 7, 2025
1 parent dc616ca commit 024ac5d
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/service_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl ServiceInfo {
/// - `Option<HashMap<String, String>>`
/// - slice of tuple: `&[(K, V)]` where `K` and `V` are [`std::string::ToString`].
///
/// Note: The maximum length of a single `properties` string is `u8::MAX`.
/// > `(key + value) <= u8::MAX`
/// Note: The maximum length of a single property string is `255`, Property that exceed the length are truncated.
/// > `len(key + value) <= u8::MAX`
///
/// `ip` can be one or more IP addresses, in a type that implements
/// [`AsIpAddrs`] trait. It supports:
Expand Down Expand Up @@ -694,10 +694,10 @@ fn encode_txt<'a>(properties: impl Iterator<Item = &'a TxtProperty>) -> Vec<u8>
s.extend(v);
}

assert!(
s.len() <= u8::MAX as usize,
"the key and value of properties exceed the maximum length limit of u8::MAX."
);
// Property that exceed the length are truncated
if s.len() > u8::MAX as usize {
s.resize(u8::MAX as usize, 0);
}

// TXT uses (Length,Value) format for each property,
// i.e. the first byte is the length.
Expand Down

0 comments on commit 024ac5d

Please sign in to comment.