From bea5cd1d213d25fc812793d09a5fcab7dcf56353 Mon Sep 17 00:00:00 2001
From: Frank Steffahn <frank.steffahn@stu.uni-kiel.de>
Date: Wed, 8 Sep 2021 22:40:21 +0200
Subject: [PATCH 1/2] Improve `size_hint` lower bound for `DecodeUtf16`
 iterator

---
 library/core/src/char/decode.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/library/core/src/char/decode.rs b/library/core/src/char/decode.rs
index 4784418f98c50..89e741a0c3e9c 100644
--- a/library/core/src/char/decode.rs
+++ b/library/core/src/char/decode.rs
@@ -122,7 +122,7 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
         let (low, high) = self.iter.size_hint();
         // we could be entirely valid surrogates (2 elements per
         // char), or entirely non-surrogates (1 element per char)
-        (low / 2, high)
+        (low.div_ceil(2), high)
     }
 }
 

From fa5e2839df12324baae58065be6cb23661dbf50e Mon Sep 17 00:00:00 2001
From: Frank Steffahn <frank.steffahn@stu.uni-kiel.de>
Date: Wed, 8 Sep 2021 23:11:45 +0200
Subject: [PATCH 2/2] Add comments, and fix issue 88762

---
 library/core/src/char/decode.rs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/library/core/src/char/decode.rs b/library/core/src/char/decode.rs
index 89e741a0c3e9c..a0d6890ec908f 100644
--- a/library/core/src/char/decode.rs
+++ b/library/core/src/char/decode.rs
@@ -122,7 +122,12 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
         let (low, high) = self.iter.size_hint();
         // we could be entirely valid surrogates (2 elements per
         // char), or entirely non-surrogates (1 element per char)
-        (low.div_ceil(2), high)
+        //
+        // In addition to `self.iter`, there's potentially one
+        // additional number in `self.buf`.
+        // On odd lower bound, at least one element must stay unpaired
+        // (with other elements from `self.iter`).
+        (low.div_ceil(2), try { high?.checked_add(1)? })
     }
 }