summaryrefslogtreecommitdiffstats
path: root/vendor/idna/src/punycode.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /vendor/idna/src/punycode.rs
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/idna/src/punycode.rs')
-rw-r--r--vendor/idna/src/punycode.rs24
1 files changed, 18 insertions, 6 deletions
diff --git a/vendor/idna/src/punycode.rs b/vendor/idna/src/punycode.rs
index faef379ca..b1afc96f7 100644
--- a/vendor/idna/src/punycode.rs
+++ b/vendor/idna/src/punycode.rs
@@ -215,6 +215,9 @@ impl<'a> ExactSizeIterator for Decode<'a> {
/// This is a convenience wrapper around `encode`.
#[inline]
pub fn encode_str(input: &str) -> Option<String> {
+ if input.len() > u32::MAX as usize {
+ return None;
+ }
let mut buf = String::with_capacity(input.len());
encode_into(input.chars(), &mut buf).ok().map(|()| buf)
}
@@ -224,6 +227,9 @@ pub fn encode_str(input: &str) -> Option<String> {
/// Return None on overflow, which can only happen on inputs that would take more than
/// 63 encoded bytes, the DNS limit on domain name labels.
pub fn encode(input: &[char]) -> Option<String> {
+ if input.len() > u32::MAX as usize {
+ return None;
+ }
let mut buf = String::with_capacity(input.len());
encode_into(input.iter().copied(), &mut buf)
.ok()
@@ -235,9 +241,9 @@ where
I: Iterator<Item = char> + Clone,
{
// Handle "basic" (ASCII) code points. They are encoded as-is.
- let (mut input_length, mut basic_length) = (0, 0);
+ let (mut input_length, mut basic_length) = (0u32, 0);
for c in input.clone() {
- input_length += 1;
+ input_length = input_length.checked_add(1).ok_or(())?;
if c.is_ascii() {
output.push(c);
basic_length += 1;
@@ -269,10 +275,7 @@ where
for c in input.clone() {
let c = c as u32;
if c < code_point {
- delta += 1;
- if delta == 0 {
- return Err(()); // Overflow
- }
+ delta = delta.checked_add(1).ok_or(())?;
}
if c == code_point {
// Represent delta as a generalized variable-length integer:
@@ -314,3 +317,12 @@ fn value_to_digit(value: u32) -> char {
_ => panic!(),
}
}
+
+#[test]
+#[ignore = "slow"]
+#[cfg(target_pointer_width = "64")]
+fn huge_encode() {
+ let mut buf = String::new();
+ assert!(encode_into(std::iter::repeat('ß').take(u32::MAX as usize + 1), &mut buf).is_err());
+ assert_eq!(buf.len(), 0);
+}