[nginx] HTTP/2: modified ngx_http_v2_send_chain() loop to be safer.

Maxim Dounin mdounin at mdounin.ru
Tue Nov 24 15:44:35 UTC 2026


details:   http://freenginx.org/hg/nginx/rev/cffef4d7dc36
branches:  stable-1.30
changeset: 9625:cffef4d7dc36
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Tue Nov 24 06:19:22 2026 +0300
description:
HTTP/2: modified ngx_http_v2_send_chain() loop to be safer.

Previously, the loop used a "limit == 0" condition, and only at the end
of the loop.  In case of other issues, such as "limit" being negative
due to h2c->send_window being large enough to overflow off_t, this
resulted in incorrect buffer construction and subsequent segmentation
faults or memory corruption.

With the change, "limit > 0" is checked as the loop condition, and this
ensures that the loop is safely terminated if "limit" ever becomes
negative due to a bug,

diffstat:

 src/http/v2/ngx_http_v2_filter_module.c |  6 +-----
 1 files changed, 1 insertions(+), 5 deletions(-)

diffs (23 lines):

diff --git a/src/http/v2/ngx_http_v2_filter_module.c b/src/http/v2/ngx_http_v2_filter_module.c
--- a/src/http/v2/ngx_http_v2_filter_module.c
+++ b/src/http/v2/ngx_http_v2_filter_module.c
@@ -958,7 +958,7 @@ ngx_http_v2_send_chain(ngx_connection_t 
     cl = NULL;
 #endif
 
-    for ( ;; ) {
+    while (limit > 0) {
         if ((off_t) frame_size > limit) {
             frame_size = (size_t) limit;
         }
@@ -1053,10 +1053,6 @@ ngx_http_v2_send_chain(ngx_connection_t 
         }
 
         limit -= frame_size;
-
-        if (limit == 0) {
-            break;
-        }
     }
 
     if (offset) {


More information about the nginx-devel mailing list