]> dgit.raspbian.org Git - dovecot.git/commitdiff
[PATCH] lib-mail: ostream-dot - Optimize stream writing
authorAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 26 Jan 2026 12:55:08 +0000 (14:55 +0200)
committerNoah Meyerhans <noahm@debian.org>
Wed, 16 Sep 2026 19:06:35 +0000 (15:06 -0400)
Use i_memcspn() to figure out how much we can skip.

Gbp-Pq: Name 0001-lib-mail-ostream-dot-Optimize-stream-writing.patch

src/lib-mail/ostream-dot.c

index f5bfa72e20be173e959ad16e060507203d8eca77..4fa868b567d67661bc5e63f36cd6e543a4390608 100644 (file)
@@ -108,9 +108,17 @@ o_stream_dot_sendv(struct ostream_private *stream,
                for (; p < pend && (size_t)(p-data)+2 < max_bytes; p++) {
                        char add = 0;
 
+                       size = pend - p;
                        switch (dstream->state) {
                        /* none */
-                       case STREAM_STATE_NONE:
+                       case STREAM_STATE_NONE: {
+                               size_t maxlen = I_MIN(size, max_bytes - ((size_t)(p - data) + 2));
+                               p = CONST_PTR_OFFSET(p, i_memcspn(p, maxlen, "\r\n", 2));
+                               i_assert(p <= pend);
+                               if (p == pend) {
+                                       p--;
+                                       continue;
+                               }
                                switch (*p) {
                                case '\n':
                                        dstream->state = STREAM_STATE_CRLF;
@@ -122,8 +130,10 @@ o_stream_dot_sendv(struct ostream_private *stream,
                                        break;
                                }
                                break;
+                       }
                        /* got CR */
                        case STREAM_STATE_CR:
+                               i_assert(p < pend);
                                switch (*p) {
                                case '\r':
                                        break;
@@ -138,6 +148,7 @@ o_stream_dot_sendv(struct ostream_private *stream,
                        /* got CRLF, or the first line */
                        case STREAM_STATE_INIT:
                        case STREAM_STATE_CRLF:
+                               i_assert(p < pend);
                                switch (*p) {
                                case '\r':
                                        dstream->state = STREAM_STATE_CR;
@@ -186,6 +197,7 @@ o_stream_dot_sendv(struct ostream_private *stream,
 
                if (max_bytes == 0)
                        break;
+               i_assert(p <= pend);
                chunk = ((size_t)(p-data) >= max_bytes ?
                                max_bytes : (size_t)(p - data));
                if (chunk > 0) {