From: "cron2 (Code Review)" Date: Sun, 14 Sep 2025 13:26:23 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., max@...2606... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="VDMKZSXyZZA="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: options: Factor out usages of strtoll and atoll List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld , plaisthos , MaxF Cc: openvpn-devel --VDMKZSXyZZA= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#5) to the change originally created by= flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1154?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by MaxF Change subject: options: Factor out usages of strtoll and atoll ...................................................................... options: Factor out usages of strtoll and atoll This covers the cases where we actually want to allow numbers > 2^31 Change-Id: I454126b3f8fa9d14501f6c4b1ed9ce7b2904be61 Signed-off-by: Frank Lichtenheld Acked-by: MaxF URL: https://gerrit.openvpn.net/c/openvpn/+/1154 Message-Id: <20250911201505.25582-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg32= 858.html Signed-off-by: Gert Doering --- M src/openvpn/options.c M src/openvpn/options_util.c M src/openvpn/options_util.h M tests/unit_tests/openvpn/test_misc.c 4 files changed, 41 insertions(+), 12 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/1154/5 diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 18cc1d9..ae285d9 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -7193,8 +7193,7 @@ options->inactivity_timeout =3D positive_atoi(p[1], msglevel); if (p[2]) { - int64_t val =3D atoll(p[2]); - options->inactivity_minimum_bytes =3D (val < 0) ? 0 : val; + positive_atoll(p[2], &options->inactivity_minimum_bytes, p[0],= msglevel); if (options->inactivity_minimum_bytes > INT_MAX) { msg(M_WARN, @@ -9545,26 +9544,18 @@ else if (streq(p[0], "reneg-bytes") && p[1] && !p[2]) { VERIFY_PERMISSION(OPT_P_TLS_PARMS); - char *end; - long long reneg_bytes =3D strtoll(p[1], &end, 10); - if (*end !=3D '\0' || reneg_bytes < 0) + if (!positive_atoll(p[1], &options->renegotiate_bytes, p[0], msgle= vel)) { - msg(msglevel, "--reneg-bytes parameter must be an integer and = >=3D 0"); goto err; } - options->renegotiate_bytes =3D reneg_bytes; } else if (streq(p[0], "reneg-pkts") && p[1] && !p[2]) { VERIFY_PERMISSION(OPT_P_TLS_PARMS); - char *end; - long long pkt_max =3D strtoll(p[1], &end, 10); - if (*end !=3D '\0' || pkt_max < 0) + if (!positive_atoll(p[1], &options->renegotiate_packets, p[0], msg= level)) { - msg(msglevel, "--reneg-pkts parameter must be an integer and >= =3D 0"); goto err; } - options->renegotiate_packets =3D pkt_max; } else if (streq(p[0], "reneg-sec") && p[1] && !p[3]) { diff --git a/src/openvpn/options_util.c b/src/openvpn/options_util.c index 28f1f9e..5740ee5 100644 --- a/src/openvpn/options_util.c +++ b/src/openvpn/options_util.c @@ -131,6 +131,22 @@ return (int)i; } +bool +positive_atoll(const char *str, int64_t *value, const char *name, int msgl= evel) +{ + char *endptr; + long long ll =3D strtoll(str, &endptr, 10); + + if (ll < 0 || *endptr !=3D '\0') + { + msg(msglevel, "%s: Cannot parse '%s' as non-negative integer", nam= e, str); + return false; + } + + *value =3D (int64_t)ll; + return true; +} + int atoi_warn(const char *str, int msglevel) { diff --git a/src/openvpn/options_util.h b/src/openvpn/options_util.h index 9eca7b6..45417d9 100644 --- a/src/openvpn/options_util.h +++ b/src/openvpn/options_util.h @@ -41,6 +41,17 @@ /** * Converts a str to an integer if the string can be represented as an + * integer number and is >=3D 0. + * The integer is stored in \p value. + * On error, print a warning with \p msglevel using \p name. \p value is + * not changed on error. + * + * @return \c true if the integer has been parsed and stored in value, \c = false otherwise + */ +bool positive_atoll(const char *str, int64_t *value, const char *name, int= msglevel); + +/** + * Converts a str to an integer if the string can be represented as an * integer number. Otherwise print a warning with \p msglevel and return 0 */ int atoi_warn(const char *str, int msglevel); diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvp= n/test_misc.c index c3f80dc..93e2460 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -359,6 +359,17 @@ assert_true(atoi_constrained("-1194", ¶meter, "test", INT_MIN, INT= _MAX, msglevel)); assert_int_equal(parameter, -1194); + int64_t parameter64 =3D 0; + assert_true(positive_atoll("1234", ¶meter64, "test", msglevel)); + assert_int_equal(parameter64, 1234); + assert_true(positive_atoll("0", ¶meter64, "test", msglevel)); + assert_int_equal(parameter64, 0); + assert_true(positive_atoll("2147483653", ¶meter64, "test", msgleve= l)); + assert_int_equal(parameter64, 2147483653); + /* overflow gets capped to LLONG_MAX */ + assert_true(positive_atoll("9223372036854775810", ¶meter64, "test"= , msglevel)); + assert_int_equal(parameter64, 9223372036854775807); + CLEAR(mock_msg_buf); assert_int_equal(positive_atoi("-1234", msglevel), 0); assert_string_equal(mock_msg_buf, "Cannot parse argument '-1234' as no= n-negative integer"); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1154?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I454126b3f8fa9d14501f6c4b1ed9ce7b2904be61 Gerrit-Change-Number: 1154 Gerrit-PatchSet: 5 Gerrit-Owner: flichtenheld Gerrit-Reviewer: MaxF Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --VDMKZSXyZZA= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #5 to the change originally cr= eated by flichtenheld.

View Change

The following approvals got outd= ated and were removed: Code-Review+2 by MaxF

options: Factor out usages of strtoll and atoll
This covers the cases where we actually want to
allow numbers > 2^3= 1

Change-Id: I454126b3f8fa9d14501f6c4b1ed9ce7b2904be61
Signed-off= -by: Frank Lichtenheld <frank@...2641...>
Acked-by: MaxF <max@...220...= ..2606...>
URL: https://gerrit.openvpn.net/c/openvpn/+/1154
Messag= e-Id: <20250911201505.25582-1-gert@...1296...>
URL: https://www.ma= il-archive.com/openvpn-devel@lists.sourceforge.net/msg32858.html
Signed-= off-by: Gert Doering <gert@...1296...>
---
M src/openvpn/option= s.c
M src/openvpn/options_util.c
M src/openvpn/options_util.h
M te= sts/unit_tests/openvpn/test_misc.c
4 files changed, 41 insertions(+), 12= deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn ref=
s/changes/54/1154/5
diff --git a/src/openvpn/options.c b/src/openvp=
n/options.c
index 18cc1d9..ae285d9 100644
-= -- a/src/openvpn/options.c
+++ b/src/openvpn/options.c
@@ -7193,8 +7193,7 @@
options->inac= tivity_timeout =3D positive_atoi(p[1], msglevel);
= if (p[2])
{
- int64_t val =3D atoll(p[2]);
- options->inactivity_minimu= m_bytes =3D (val < 0) ? 0 : val;
+ positive_atoll(p[2], &options->inactivit= y_minimum_bytes, p[0], msglevel);
if (options-= >inactivity_minimum_bytes > INT_MAX)
{
msg(M_WARN,
@@ -9545,26 +95= 44,18 @@
else if (streq(p[0], "reneg-bytes")= && p[1] && !p[2])
{
= VERIFY_PERMISSION(OPT_P_TLS_PARMS);
- char *end;
- long long reneg_bytes =3D strtoll(p[1], &end= , 10);
- if (*en= d !=3D '\0' || reneg_bytes < 0)
+ if (!positive_atoll(p[1], &options->r= enegotiate_bytes, p[0], msglevel))
{
- msg(msglevel, "--r= eneg-bytes parameter must be an integer and >=3D 0");

goto err;

}
- options->renegotiate_bytes =3D r= eneg_bytes;
}
else if (streq(p[0]= , "reneg-pkts") && p[1] && !p[2])
{
VERIFY_PERMISSION(OPT_P_TLS_PARMS);=
- char *end;- long long pkt_max =3D s= trtoll(p[1], &end, 10);
- if (*end !=3D '\0' || pkt_max < 0)
+ if (!positive_atoll(p[1], &= amp;options->renegotiate_packets, p[0], msglevel))
= {
- msg(= msglevel, "--reneg-pkts parameter must be an integer and >=3D 0&quo= t;);
goto err;
}
- options->renego= tiate_packets =3D pkt_max;
}
else= if (streq(p[0], "reneg-sec") && p[1] && !p[3])
{
diff --git a/src/openvpn/options_util= .c b/src/openvpn/options_util.c
index 28f1f9e..5740ee5 1006= 44
--- a/src/openvpn/options_util.c
+++ b/s= rc/openvpn/options_util.c
@@ -131,6 +131,22 @@
return (int)i;

}

+bool

+positive_atoll(const char *str, int64_t *value, c= onst char *name, int msglevel)
+{
+ cha= r *endptr;
+ long = long ll =3D strtoll(str, &endptr, 10);
+
+ if (ll < 0 || *endptr !=3D '\0')
+ {
+ msg(msglevel, "%s: Cannot parse '%s= 9; as non-negative integer", name, str);
+ return false;
+ }
+
+ *= value =3D (int64_t)ll;
+= }
+
= int
atoi_warn(const char *str, int msglevel)
{
diff --git a/src/openvpn/options_util.h b/src/openvpn= /options_util.h
index 9eca7b6..45417d9 100644
--- a/src/openvpn/options_util.h
+++ b/src/openvpn/optio= ns_util.h
@@ -41,6 +41,17 @@

/**
* Converts a str to an integer if the string can= be represented as an
+ * integer number and is >=3D 0.
+ * The integer is stored in \p value.
+ * On error, print a warning with \p ms= glevel using \p name. \p value is
+ * not changed on error.
+ *
+ * @return \c true if the integer has been parsed and stored in value, \c= false otherwise
+ */=
+bool positive_atoll= (const char *str, int64_t *value, const char *name, int msglevel);+
+/**
+ * Converts a str to an integer if the string can be represente= d as an
* integer number. Otherwise print a warning with = \p msglevel and return 0
*/
int atoi_war= n(const char *str, int msglevel);
diff --git a/tests/unit_t= ests/openvpn/test_misc.c b/tests/unit_tests/openvpn/test_misc.c
<= span>index c3f80dc..93e2460 100644
--- a/tests/unit_tests/o= penvpn/test_misc.c
+++ b/tests/unit_tests/openvpn/test_misc= .c
@@ -359,6 +359,17 @@
assert_true(at= oi_constrained("-1194", &parameter, "test", INT_MIN= , INT_MAX, msglevel));
assert_int_equal(parameter, -11= 94);

+ assert_true(positive_atoll("1234", &paramet= er64, "test", msglevel));
+ assert_int_equal(parameter64, 1234);
+ assert_true(positive_atoll("0= ", &parameter64, "test", msglevel));
+ assert_int_equal(parameter64, 0);
+ assert_true(posit= ive_atoll("2147483653", &parameter64, "test", msgle= vel));
+ assert_in= t_equal(parameter64, 2147483653);
+ /* overflow gets capped to LLONG_MAX */
+ assert_true(positive_atoll("= 9223372036854775810", &parameter64, "test", msglevel));<= /span>
+ assert_int_equal= (parameter64, 9223372036854775807);
+
CLEAR(mock_msg_buf);
= assert_int_equal(positive_atoi("-1234", msglevel), 0);

assert_string_equal(mock_msg_buf, "Cannot parse argume= nt '-1234' as non-negative integer");
<= br>

To view, visit change 1154. To unsubscribe, or for help writing mai= l filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I454126b3f8fa9d14501f6c4b1ed= 9ce7b2904be61
Gerrit-Change-Number: 1154
Gerrit-PatchSet: 5
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: MaxF <max@...2606...> <= /div>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--VDMKZSXyZZA=--