Skip to content
34 changes: 29 additions & 5 deletions soroban-sdk-macros/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,33 @@
_ => None,
})
.map(|s| s.trim().to_string())
.join("\n")
.as_bytes()
.to_vec();
docs.truncate(DOCS_MAX_LEN as usize);
docs.try_into().unwrap()
.join("\n");
// Truncate on a char boundary to avoid splitting multi-byte UTF-8 codepoints.
let max = DOCS_MAX_LEN as usize;
let safe_len = docs.floor_char_boundary(max);

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (increment_with_fuzz, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (atomic_swap, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (cross_contract/contract_a, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (increment, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (groth16_verifier, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (logging, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (cross_contract/contract_a, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (account, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (bls_signature, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (single_offer, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (mint-lock, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (timelock, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (mint-lock, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (auth, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (increment_with_pause, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (increment, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (upgradeable_contract/new_contract, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (other_custom_types, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (ttl, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (hello_world, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (increment_with_fuzz, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (pause, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (simple_account, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (deep_contract_auth, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (simple_account, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (token, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (timelock, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (custom_types, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (eth_abi, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (liquidity_pool, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (fuzzing, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (bls_signature, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (custom_types, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (ttl, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (upgradeable_contract/new_contract, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (multisig_1_of_n_account/contract, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (deployer/contract, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (events, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (increment_with_pause, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (multisig_1_of_n_account/contract, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (hello_world, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (fuzzing, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (deployer/contract, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (single_offer, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (auth, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (liquidity_pool, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (merkle_distribution, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (events, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (other_custom_types, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (merkle_distribution, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (groth16_verifier, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (token, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (alloc, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (alloc, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (atomic_swap, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (errors, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (eth_abi, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (pause, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (account, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (errors, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (import_ark_bn254, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (logging, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (import_ark_bn254, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (deep_contract_auth, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (atomic_multiswap, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (cross_contract/contract_b, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (upgradeable_contract/old_contract, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (deployer/deployer, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (deployer/deployer, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (atomic_multiswap, false)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (upgradeable_contract/old_contract, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (cross_contract/contract_b, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (privacy-pools, true)

use of unstable library feature `round_char_boundary`

Check failure on line 26 in soroban-sdk-macros/src/doc.rs

View workflow job for this annotation

GitHub Actions / test-example (privacy-pools, false)

use of unstable library feature `round_char_boundary`
docs.truncate(safe_len);
docs.into_bytes().try_into().unwrap()
}

#[cfg(test)]
mod test {
use super::*;
use syn::parse_quote;

#[test]
fn test_truncation_does_not_split_multibyte_utf8() {
// (DOCS_MAX_LEN - 1) ASCII bytes followed by 'é' (2 bytes: 0xC3
// 0xA9) = DOCS_MAX_LEN + 1 bytes. Truncation at DOCS_MAX_LEN keeps
// the 0xC3 but drops the 0xA9, producing invalid UTF-8.
let padding = "a".repeat(DOCS_MAX_LEN as usize - 1);
let doc_value = format!("{padding}é");
let attr: Attribute = parse_quote!(#[doc = #doc_value]);
let result = docs_from_attrs(&[attr]);
let bytes: Vec<u8> = result.into();
assert!(
std::str::from_utf8(&bytes).is_ok(),
"truncation produced invalid UTF-8: trailing bytes {:?}",
&bytes[bytes.len().saturating_sub(4)..]
);
}
}
Loading