Commit 9e3c8ec9d2 for openssl.org

commit 9e3c8ec9d2430bb602531cb7e9e2170be8f4fdf2
Author: Liu Dongmiao <473522+liudongmiao@users.noreply.github.com>
Date:   Wed Nov 18 12:35:48 2026 +0800

    quic: handle send stream data sent in more than one packet

    To let a PTO probe resend data while its original packet stays in
    flight, the send stream must handle one copy being ACKed while another
    is declared lost, and the same bytes being ACKed twice with different
    boundaries.

    Lost ranges now skip ACKed bytes, and ACKed ranges leave the new set
    before they are recorded, so no byte is ever in both sets, even on
    allocation failure. A UINT_SET insert that overlaps existing ranges now
    also merges with a range it touches, as other inserts already do.

    Assisted-by: Claude:claude-opus-5-5
    Reviewed-by: Saša NedvÄ›dický <sashan@openssl.org>
    Reviewed-by: Neil Horman <nhorman@openssl.org>
    Merge-date: Fri Dec  4 13:55:46 2026
    Merged-from: https://github.com/openssl/openssl/pull/33782

diff --git a/ssl/quic/quic_sstream.c b/ssl/quic/quic_sstream.c
index 314104addc..ce20800b6f 100644
--- a/ssl/quic/quic_sstream.c
+++ b/ssl/quic/quic_sstream.c
@@ -211,13 +211,37 @@ int ossl_quic_sstream_mark_lost(QUIC_SSTREAM *qss,
     uint64_t end)
 {
     UINT_RANGE r;
+    UINT_SET_ITEM *acked;
+
     r.start = start;
     r.end = end;

     /*
      * We lost a range of stream data bytes, so reinsert them into the new set,
      * so that they are returned once more by ossl_quic_sstream_get_stream_frame.
+     * Bytes already ACKed in another packet, such as a PTO probe, are skipped.
      */
+    for (acked = ossl_list_uint_set_tail(&qss->acked_set);
+        acked != NULL && acked->range.end >= r.start;
+        acked = ossl_list_uint_set_prev(acked)) {
+        if (acked->range.start > r.end)
+            continue;
+
+        if (acked->range.end < r.end) {
+            UINT_RANGE gap;
+
+            gap.start = acked->range.end + 1;
+            gap.end = r.end;
+            if (!ossl_uint_set_insert(&qss->new_set, &gap))
+                return 0;
+        }
+
+        if (acked->range.start <= r.start)
+            return 1;
+
+        r.end = acked->range.start - 1;
+    }
+
     if (!ossl_uint_set_insert(&qss->new_set, &r))
         return 0;

@@ -243,6 +267,13 @@ int ossl_quic_sstream_mark_acked(QUIC_SSTREAM *qss,
     r.start = start;
     r.end = end;

+    /*
+     * A copy of the range may still be queued for retransmission. Remove it
+     * first, so that a failure never leaves a byte both NEW and ACKED.
+     */
+    if (!ossl_uint_set_remove(&qss->new_set, &r))
+        return 0;
+
     if (!ossl_uint_set_insert(&qss->acked_set, &r))
         return 0;

diff --git a/ssl/quic/uint_set.c b/ssl/quic/uint_set.c
index 4b723869e8..bcb5af3ee0 100644
--- a/ssl/quic/uint_set.c
+++ b/ssl/quic/uint_set.c
@@ -218,6 +218,11 @@ int ossl_uint_set_insert(UINT_SET *s, const UINT_RANGE *range)
                 ossl_list_uint_set_remove(s, x);
                 OPENSSL_free(x);
             }
+
+            /* The merged range may now touch its neighbours. */
+            uint_set_merge_adjacent(s, ovend);
+            if ((x = ossl_list_uint_set_next(ovend)) != NULL)
+                uint_set_merge_adjacent(s, x);
             break;
         } else if (end < z->range.start
             && (zprev == NULL || start > zprev->range.end)) {