From: Willy Tarreau Date: Mon, 18 Nov 2024 18:04:57 +0000 (+0100) Subject: MINOR: cfgparse: parse tune.bufsize as a size X-Git-Tag: v3.1-dev14~126 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a344d37fadecaa20da046585654571920fa393ea;p=thirdparty%2Fhaproxy.git MINOR: cfgparse: parse tune.bufsize as a size Till now this value was parsed as raw integer using atol() and would silently ignore any trailing suffix, preventing from starting when set e.g. to "64k". Let's make use of parse_size_err() on it so that units are supported. This requires to turn it to uint as well, and to explicitly limit its range to INT_MAX - 2*sizeof(void*), which was previously partially handled as part of the sign check. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index 29fe89af9e..793716a5a6 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -3320,7 +3320,7 @@ tune.buffers.reserve a user would want to change this value, unless a core developer suggests to change it for a very specific reason. -tune.bufsize +tune.bufsize Sets the buffer size to this size (in bytes). Lower values allow more streams to coexist in the same amount of RAM, and higher values allow some applications with very large cookies to work. The default value is 16384 and diff --git a/include/haproxy/global-t.h b/include/haproxy/global-t.h index a013b96e60..0a176a65f5 100644 --- a/include/haproxy/global-t.h +++ b/include/haproxy/global-t.h @@ -165,7 +165,7 @@ struct global { int options; /* various tuning options */ int runqueue_depth;/* max number of tasks to run at once */ uint recv_enough; /* how many input bytes at once are "enough" */ - int bufsize; /* buffer size in bytes, defaults to BUFSIZE */ + uint bufsize; /* buffer size in bytes, defaults to BUFSIZE */ int bufsize_small; /* small buffer size in bytes */ int maxrewrite; /* buffer max rewrite size in bytes, defaults to MAXREWRITE */ int reserved_bufs; /* how many buffers can only be allocated for response */ diff --git a/src/cfgparse-global.c b/src/cfgparse-global.c index 61c5a20201..b8369c821e 100644 --- a/src/cfgparse-global.c +++ b/src/cfgparse-global.c @@ -1195,7 +1195,16 @@ static int cfg_parse_global_tune_opts(char **args, int section_type, memprintf(err, "'%s' expects an integer argument", args[0]); return -1; } - global.tune.bufsize = atol(args[1]); + res = parse_size_err(args[1], &global.tune.bufsize); + if (res != NULL) + goto size_err; + + if (global.tune.bufsize > INT_MAX - (int)(2 * sizeof(void *))) { + memprintf(err, "'%s' expects a size in bytes from 0 to %d.", + args[0], INT_MAX - (int)(2 * sizeof(void *))); + return -1; + } + /* round it up to support a two-pointer alignment at the end */ global.tune.bufsize = (global.tune.bufsize + 2 * sizeof(void *) - 1) & -(2 * sizeof(void *)); if (global.tune.bufsize <= 0) {