]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tree-wide: Use the buffer size instead of global setting when possible
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 28 Jan 2026 09:54:41 +0000 (10:54 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 18 Feb 2026 12:26:20 +0000 (13:26 +0100)
At many places, we rely on global.tune.bufsize value instead of using the buffer
size. For now, it is not a problem. But if we want to be able to deal with
buffers of different sizes, it is good to reduce as far as possible dependencies
on the global value. most of time, we can use b_size() or c_size()
functions. The main change is performed on the error snapshot where the buffer
size was added into the error_snapshot structure.

include/haproxy/channel.h
include/haproxy/proxy-t.h
include/haproxy/sc_strm.h
src/hlua.c
src/http_fetch.c
src/mux_h1.c
src/mux_h2.c
src/payload.c
src/proxy.c
src/ssl_sock.c

index 0c872dc314189d3a11274c5cb7de28ee35927097..af2989416d6e44e6a2a49737f64df92457591237 100644 (file)
@@ -787,8 +787,12 @@ static inline int channel_recv_max(const struct channel *chn)
  */
 static inline size_t channel_data_limit(const struct channel *chn)
 {
-       size_t max = (global.tune.bufsize - global.tune.maxrewrite);
 
+       size_t max;
+
+       if (!c_size(chn))
+               return 0;
+       max = (c_size(chn) - global.tune.maxrewrite);
        if (IS_HTX_STRM(chn_strm(chn)))
                max -= HTX_BUF_OVERHEAD;
        return max;
index 20c359126e894986599571897db4365dea20c10c..5f58bdb4c5b388902668defd57ea9488ee4ad2ff 100644 (file)
@@ -293,7 +293,8 @@ struct error_snapshot {
        struct server *srv;             /* server associated with the error (or NULL) */
        /* @64 */
        unsigned int ev_id;             /* event number (counter incremented for each capture) */
-       /* @68: 4 bytes hole here */
+       unsigned int buf_size;          /* buffer size */
+
        struct sockaddr_storage src;    /* client's address */
 
        /**** protocol-specific part ****/
index 5d473f3b82f791881232a6cde54751cccf0b5162..bac877dc1a22cd8d2644c236bb26acddf93b0726 100644 (file)
@@ -133,7 +133,7 @@ static inline void sc_have_room(struct stconn *sc)
 static inline void sc_need_room(struct stconn *sc, ssize_t room_needed)
 {
        sc->flags |= SC_FL_NEED_ROOM;
-       BUG_ON_HOT(room_needed > (ssize_t)global.tune.bufsize);
+       BUG_ON_HOT(room_needed > (ssize_t)c_size(sc_ic(sc)));
        sc->room_needed = room_needed;
 }
 
index b826e6ab867d9725e24c049a2f4c4492bf059d1e..34b93b0364d3b8596b14903b212f59a47c9270ff 100644 (file)
@@ -3952,7 +3952,7 @@ __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KCon
                if (!len)
                        goto dup;
                if (len == -1)
-                       len = global.tune.bufsize;
+                       len = c_size(chn);
                if (len < 0) {
                        lua_pushfstring(L, "length out of range.");
                        WILL_LJMP(lua_error(L));
@@ -4035,7 +4035,7 @@ __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KCon
                if (!len)
                        goto dup;
                if (len == -1)
-                       len = global.tune.bufsize;
+                       len = c_size(chn);
                if (len < 0) {
                        lua_pushfstring(L, "length out of range.");
                        WILL_LJMP(lua_error(L));
@@ -7470,7 +7470,7 @@ __LJMP static int hlua_http_msg_get_body(lua_State *L)
                if (!len)
                        goto dup;
                if (len == -1)
-                       len = global.tune.bufsize;
+                       len = c_size(msg->chn);
                if (len < 0) {
                        lua_pushfstring(L, "length out of range.");
                        WILL_LJMP(lua_error(L));
index b8b6f616c051b4868b33bb578fe217df1853cd54..1f8ff8c992b02d58d8a33a71a7a59aa682778c72 100644 (file)
@@ -138,7 +138,7 @@ static int get_http_auth(struct sample *smp, struct htx *htx)
 
                len = base64dec(txn->auth.method_data.area,
                                txn->auth.method_data.data,
-                               http_auth->area, global.tune.bufsize - 1);
+                               http_auth->area, http_auth->size -1);
 
                if (len < 0)
                        return 0;
index 81bd071bace06bd01bdc42ac561d3b8debfc3cb6..045fd603a31d3771249caf01ade59afffe17aee7 100644 (file)
@@ -4008,8 +4008,8 @@ static int h1_recv(struct h1c *h1c)
                 * a pointer swap and the next read will be doable at once in
                 * an empty buffer.
                 */
-               if (max > global.tune.bufsize - global.tune.maxrewrite)
-                       max = global.tune.bufsize - global.tune.maxrewrite;
+               if (max > b_size(&h1c->ibuf) - global.tune.maxrewrite)
+                       max = b_size(&h1c->ibuf) - global.tune.maxrewrite;
        }
 
        if (max) {
index a870426dc3b3b82e4e2f86d07177a1a7829ba3ca..41fbbc26dd8b4b282e65fa009073446ff894d058 100644 (file)
@@ -4256,7 +4256,7 @@ static void h2_process_demux(struct h2c *h2c)
                                goto done;
                        }
 
-                       if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
+                       if ((int)hdr.len < 0 || (int)hdr.len > b_size(&h2c->dbuf)) {
                                /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
                                h2c_report_glitch(h2c, 1, "invalid settings frame length");
                                TRACE_ERROR("invalid settings frame length", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_SETTINGS|H2_EV_PROTO_ERR, h2c->conn);
@@ -4303,7 +4303,7 @@ static void h2_process_demux(struct h2c *h2c)
                                break;
                        }
 
-                       if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
+                       if ((int)hdr.len < 0 || (int)hdr.len > b_size(&h2c->dbuf)) {
                                h2c_report_glitch(h2c, 1, "invalid H2 frame length");
                                TRACE_ERROR("invalid H2 frame length", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn);
                                h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
@@ -4384,7 +4384,7 @@ static void h2_process_demux(struct h2c *h2c)
                        h2c->st0 = H2_CS_FRAME_P;
 
                        /* check for minimum basic frame format validity */
-                       ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
+                       ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, b_size(&h2c->dbuf));
                        if (ret != H2_ERR_NO_ERROR) {
                                h2c_report_glitch(h2c, 1, "received invalid H2 frame header");
                                TRACE_ERROR("received invalid H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn);
@@ -6066,7 +6066,7 @@ next_frame:
                        goto fail;
                }
 
-               if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
+               if ((unsigned)hdr.len > (unsigned)b_size(&h2c->dbuf)) {
                        /* RFC7540#4.2: invalid frame length */
                        h2c_report_glitch(h2c, 1, "too large CONTINUATION frame");
                        TRACE_STATE("too large CONTINUATION frame", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_HDR|H2_EV_RX_CONT|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
index 62071aca66badda493db2120f9230f3878b459e9..e1a1583559b171db780437b1e4f35b6ad35a7eb8 100644 (file)
@@ -1205,7 +1205,6 @@ smp_fetch_payload_lv(const struct arg *arg_p, struct sample *smp, const char *kw
        unsigned int len_size = arg_p[1].data.sint;
        unsigned int buf_offset;
        unsigned int buf_size = 0;
-       struct channel *chn = NULL;
        char *head = NULL;
        size_t max, data;
        int i;
@@ -1215,12 +1214,15 @@ smp_fetch_payload_lv(const struct arg *arg_p, struct sample *smp, const char *kw
        /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */
 
        if (smp->strm) {
+               struct channel *chn = NULL;
+
                /* meaningless for HTX buffers */
                if (IS_HTX_STRM(smp->strm))
                        return 0;
                chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
                head = ci_head(chn);
                data = ci_data(chn);
+               max = c_size(chn);
        }
        else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK) {
                struct check *check = __objt_check(smp->sess->origin);
@@ -1230,8 +1232,9 @@ smp_fetch_payload_lv(const struct arg *arg_p, struct sample *smp, const char *kw
                        return 0;
                head = b_head(&check->bi);
                data = b_data(&check->bi);
+               max = b_size(&check->bi);
        }
-       max = global.tune.bufsize;
+
        if (!head)
                goto too_short;
 
@@ -1290,6 +1293,7 @@ smp_fetch_payload(const struct arg *arg_p, struct sample *smp, const char *kw, v
                chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req;
                head = ci_head(chn);
                data = ci_data(chn);
+               max = c_size(chn);
        }
        else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK) {
                struct check *check = __objt_check(smp->sess->origin);
@@ -1299,8 +1303,8 @@ smp_fetch_payload(const struct arg *arg_p, struct sample *smp, const char *kw, v
                        return 0;
                head = b_head(&check->bi);
                data = b_data(&check->bi);
+               max =  b_size(&check->bi);
        }
-       max = global.tune.bufsize;
        if (!head)
                goto too_short;
 
index 06c3fb877d226dceef645f4e4da8c345a205782c..778c9d9e4556d89268c66a11bec2551b9793ac0c 100644 (file)
@@ -4173,6 +4173,7 @@ void proxy_capture_error(struct proxy *proxy, int is_back,
        if (!es)
                return;
 
+       es->buf_size = buf->size;
        es->buf_len = buf_len;
        es->ev_id   = ev_id;
 
@@ -5224,7 +5225,7 @@ static int cli_io_handler_show_errors(struct appctx *appctx)
                                      es->srv ? es->srv->puid : -1,
                                      es->ev_id, pn, port,
                                      es->buf_ofs, es->buf_out,
-                                     global.tune.bufsize - es->buf_out - es->buf_len,
+                                     es->buf_size - es->buf_out - es->buf_len,
                                      es->buf_len, es->buf_wrap, es->buf_err);
 
                        if (es->show)
@@ -5250,12 +5251,12 @@ static int cli_io_handler_show_errors(struct appctx *appctx)
                }
 
                /* OK, ptr >= 0, so we have to dump the current line */
-               while (ctx->ptr < es->buf_len && ctx->ptr < global.tune.bufsize) {
+               while (ctx->ptr < es->buf_len && ctx->ptr < es->buf_size) {
                        int newptr;
                        int newline;
 
                        newline = ctx->bol;
-                       newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, ctx->ptr);
+                       newptr = dump_text_line(&trash, es->buf, es->buf_size, es->buf_len, &newline, ctx->ptr);
                        if (newptr == ctx->ptr) {
                                applet_fl_set(appctx, APPCTX_FL_OUTBLK_FULL);
                                goto cant_send_unlock;
index a7323426c5d1fe721dc7d1391939c72010377ae3..291aa9467e0fa06e72c0cc1c7ecb5b75eeec38be 100644 (file)
@@ -5918,7 +5918,7 @@ static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
                             * a buffer.
                             */
                            (!b_is_null(&ctx->early_buf)) ?
-                           global.tune.bufsize - global.tune.maxrewrite : 0);
+                           ctx->early_buf.size - global.tune.maxrewrite : 0);
                }
 #endif