[PATCH 03 of 10] Mail: handling of pipelined commands which cross buffer boundary

Maxim Dounin mdounin at mdounin.ru
Fri Mar 15 18:14:17 UTC 2024


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1710526243 -10800
#      Fri Mar 15 21:10:43 2024 +0300
# Node ID 970059108845286afec7ae37ba5f55b0cf30feef
# Parent  1d26e6050416a6c8a86d83119d894f7f9f4f5a94
Mail: handling of pipelined commands which cross buffer boundary.

Previously, pipelined commands which cross buffer boundary were rejected
as too long, even if the command itself was short enough to be handled
within the client buffer.

Fix is to move non-processed commands to the start of the buffer after
the previous command is fully processed.

diff --git a/src/mail/ngx_mail_imap_handler.c b/src/mail/ngx_mail_imap_handler.c
--- a/src/mail/ngx_mail_imap_handler.c
+++ b/src/mail/ngx_mail_imap_handler.c
@@ -102,6 +102,7 @@ void
 ngx_mail_imap_auth_state(ngx_event_t *rev)
 {
     u_char              *p;
+    size_t               n;
     ngx_int_t            rc;
     ngx_uint_t           tag;
     ngx_connection_t    *c;
@@ -285,6 +286,12 @@ ngx_mail_imap_auth_state(ngx_event_t *re
             if (s->buffer->pos == s->buffer->last) {
                 s->buffer->pos = s->buffer->start;
                 s->buffer->last = s->buffer->start;
+
+            } else {
+                n = s->buffer->last - s->buffer->pos;
+                ngx_memmove(s->buffer->start, s->buffer->pos, n);
+                s->buffer->pos = s->buffer->start;
+                s->buffer->last = s->buffer->start + n;
             }
 
             s->tag.len = 0;
diff --git a/src/mail/ngx_mail_pop3_handler.c b/src/mail/ngx_mail_pop3_handler.c
--- a/src/mail/ngx_mail_pop3_handler.c
+++ b/src/mail/ngx_mail_pop3_handler.c
@@ -119,6 +119,7 @@ ngx_mail_pop3_init_protocol(ngx_event_t 
 void
 ngx_mail_pop3_auth_state(ngx_event_t *rev)
 {
+    size_t               n;
     ngx_int_t            rc;
     ngx_connection_t    *c;
     ngx_mail_session_t  *s;
@@ -291,6 +292,12 @@ ngx_mail_pop3_auth_state(ngx_event_t *re
         if (s->buffer->pos == s->buffer->last) {
             s->buffer->pos = s->buffer->start;
             s->buffer->last = s->buffer->start;
+
+        } else {
+            n = s->buffer->last - s->buffer->pos;
+            ngx_memmove(s->buffer->start, s->buffer->pos, n);
+            s->buffer->pos = s->buffer->start;
+            s->buffer->last = s->buffer->start + n;
         }
 
         if (s->state) {
diff --git a/src/mail/ngx_mail_smtp_handler.c b/src/mail/ngx_mail_smtp_handler.c
--- a/src/mail/ngx_mail_smtp_handler.c
+++ b/src/mail/ngx_mail_smtp_handler.c
@@ -430,6 +430,7 @@ ngx_mail_smtp_create_buffer(ngx_mail_ses
 void
 ngx_mail_smtp_auth_state(ngx_event_t *rev)
 {
+    size_t               n;
     ngx_int_t            rc;
     ngx_connection_t    *c;
     ngx_mail_session_t  *s;
@@ -577,6 +578,12 @@ ngx_mail_smtp_auth_state(ngx_event_t *re
         if (s->buffer->pos == s->buffer->last) {
             s->buffer->pos = s->buffer->start;
             s->buffer->last = s->buffer->start;
+
+        } else {
+            n = s->buffer->last - s->buffer->pos;
+            ngx_memmove(s->buffer->start, s->buffer->pos, n);
+            s->buffer->pos = s->buffer->start;
+            s->buffer->last = s->buffer->start + n;
         }
 
         if (s->state) {




More information about the nginx-devel mailing list