From: "flichtenheld (Code Review)" Date: Fri, 22 Aug 2025 13:45:25 +0000 References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="E51vmMfb/LM="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: options: Introduce boolean_flag() and review usages of atoi_warn List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --E51vmMfb/LM= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1151?usp=3Demail to review the following change. Change subject: options: Introduce boolean_flag() and review usages of atoi= _warn ...................................................................... options: Introduce boolean_flag() and review usages of atoi_warn This uses atoi_constrained. Note that this makes the tests stricter since previously any non-zero integer would be interpreted as "true". Change-Id: Ic30612971367a4aa54ca89f93452efc172d4f4e2 Signed-off-by: Frank Lichtenheld --- 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, 38 insertions(+), 11 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/51/1151/1 diff --git a/src/openvpn/options.c b/src/openvpn/options.c index b62e22c..46f4753 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -8358,7 +8358,7 @@ { VERIFY_PERMISSION(OPT_P_GENERAL); options->sc_info.challenge_text =3D p[1]; - if (atoi_warn(p[2], msglevel)) + if (boolean_flag(p[2], "static-challenge echo", msglevel)) { options->sc_info.flags |=3D SC_ECHO; } @@ -8721,7 +8721,7 @@ options->exit_event_name =3D p[1]; if (p[2]) { - options->exit_event_initial_state =3D (atoi_warn(p[2], msgleve= l) !=3D 0); + options->exit_event_initial_state =3D boolean_flag(p[2], "serv= ice state", msglevel); } } else if (streq(p[0], "allow-nonadmin") && !p[2]) @@ -9682,7 +9682,8 @@ else if (streq(p[0], "show-pkcs11-ids") && !p[3]) { char *provider =3D p[1]; - bool cert_private =3D (p[2] =3D=3D NULL ? false : (atoi_warn(p[2],= msglevel) !=3D 0)); + bool cert_private =3D (p[2] =3D=3D NULL ? false + : boolean_flag(p[2], "show-pkcs1= 1-ids private", msglevel)); #ifdef DEFAULT_PKCS11_MODULE if (!provider) @@ -9728,14 +9729,12 @@ } else if (streq(p[0], "pkcs11-protected-authentication")) { - int j; - VERIFY_PERMISSION(OPT_P_GENERAL); - for (j =3D 1; j < MAX_PARMS && p[j] !=3D NULL; ++j) + for (int j =3D 1; j < MAX_PARMS && p[j] !=3D NULL; ++j) { options->pkcs11_protected_authentication[j - 1] =3D - atoi_warn(p[j], msglevel) !=3D 0 ? 1 : 0; + boolean_flag(p[j], p[0], msglevel); } } else if (streq(p[0], "pkcs11-private-mode") && p[1]) @@ -9751,13 +9750,11 @@ } else if (streq(p[0], "pkcs11-cert-private")) { - int j; - VERIFY_PERMISSION(OPT_P_GENERAL); - for (j =3D 1; j < MAX_PARMS && p[j] !=3D NULL; ++j) + for (int j =3D 1; j < MAX_PARMS && p[j] !=3D NULL; ++j) { - options->pkcs11_cert_private[j - 1] =3D (bool)(atoi_warn(p[j],= msglevel)); + options->pkcs11_cert_private[j - 1] =3D boolean_flag(p[j], p[0= ], msglevel); } } else if (streq(p[0], "pkcs11-pin-cache") && p[1] && !p[2]) diff --git a/src/openvpn/options_util.c b/src/openvpn/options_util.c index c1f7c74..32a9edb 100644 --- a/src/openvpn/options_util.c +++ b/src/openvpn/options_util.c @@ -181,6 +181,14 @@ return true; } +bool +boolean_flag(const char *str, const char *name, int msglevel) +{ + int number =3D 0; + atoi_constrained(str, &number, name, 0, 1, msglevel); + return (bool)number; +} + static const char *updatable_options[] =3D { "block-ipv6", "block-outside-= dns", "dhcp-option", "dns", "ifconfig", "ifconfig-ipv6", diff --git a/src/openvpn/options_util.h b/src/openvpn/options_util.h index d5ae8b9..b9e1569 100644 --- a/src/openvpn/options_util.h +++ b/src/openvpn/options_util.h @@ -59,6 +59,12 @@ int msglevel); /** + * Converts a str to an boolean if the string can be parsed as either 0 or= 1. + * Otherwise print a warning with \p msglevel and return \c false. + */ +bool boolean_flag(const char *str, const char *name, int msglevel); + +/** * Filter an option line by all pull filters. * * If a match is found, the line is modified depending on diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvp= n/test_misc.c index 80a5dee..2d2cc9e 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -431,6 +431,22 @@ assert_string_equal(mock_msg_buf, "test: Must be an integer >=3D 1, no= t 0"); assert_int_equal(parameter, -42); + /* special tests for boolean_flag */ + assert_false(boolean_flag("0", "test", msglevel)); + assert_true(boolean_flag("1", "test", msglevel)); + + CLEAR(mock_msg_buf); + assert_false(boolean_flag("foo77", "test", msglevel)); + assert_string_equal(mock_msg_buf, "test: Cannot parse 'foo77' as integ= er"); + + CLEAR(mock_msg_buf); + assert_false(boolean_flag("-77", "test", msglevel)); + assert_string_equal(mock_msg_buf, "test: Must be an integer between 0 = and 1, not -77"); + + CLEAR(mock_msg_buf); + assert_false(boolean_flag("77", "test", msglevel)); + assert_string_equal(mock_msg_buf, "test: Must be an integer between 0 = and 1, not 77"); + mock_set_debug_level(saved_log_level); } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1151?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: Ic30612971367a4aa54ca89f93452efc172d4f4e2 Gerrit-Change-Number: 1151 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-MessageType: newchange --E51vmMfb/LM= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

V= iew Change

options: Introduce boolean_flag() and review usages of atoi_=
warn

This uses atoi_constrained. Note that this makes the tests
s= tricter since previously any non-zero integer would be
interpreted as &q= uot;true".

Change-Id: Ic30612971367a4aa54ca89f93452efc172d4f4e2=
Signed-off-by: Frank Lichtenheld <frank@...2641...>
---
M s= rc/openvpn/options.c
M src/openvpn/options_util.c
M src/openvpn/optio= ns_util.h
M tests/unit_tests/openvpn/test_misc.c
4 files changed, 38 = insertions(+), 11 deletions(-)

git pull ssh://gerrit.openvpn.net:=
29418/openvpn refs/changes/51/1151/1
diff --git a/src/openvpn/optio=
ns.c b/src/openvpn/options.c
index b62e22c..46f4753 100644<= /span>
--- a/src/openvpn/options.c
+++ b/src/openv= pn/options.c
@@ -8358,7 +8358,7 @@
{
VERIFY_PERMISSION(OPT_P_GENERAL);
= options->sc_info.challenge_text =3D p[1];
- if (atoi_warn(p[2], msglevel))
+ if (boolean_flag= (p[2], "static-challenge echo", msglevel))
= {
options->sc_info.flags |=3D SC_ECHO;
}
@@ -8721,7 +8721,7 @@
<= span> options->exit_event_name =3D p[1];

= if (p[2])
{
- options->exit_event_initial_state =3D (atoi= _warn(p[2], msglevel) !=3D 0);
+ options->exit_event_initial_state =3D boolean_fla= g(p[2], "service state", msglevel);
}
}
else if (streq(p[0], "allow-= nonadmin") && !p[2])
@@ -9682,7 +9682,8 @@
else if (streq(p[0], "show-pkcs11-ids") &&a= mp; !p[3])
{
char *provider = =3D p[1];
- bool= cert_private =3D (p[2] =3D=3D NULL ? false : (atoi_warn(p[2], msglevel) != =3D 0));
+ boo= l cert_private =3D (p[2] =3D=3D NULL ? false
+ : boolea= n_flag(p[2], "show-pkcs11-ids private", msglevel));

#ifdef DEFAULT_PKCS11_MODULE
= if (!provider)
@@ -9728,14 +9729,12 @@
= }
else if (streq(p[0], "pkcs11-protected-authen= tication"))
{
- int j;
-
VERIFY_PERMISSION(OPT_P_GENERAL);

- = for (j =3D 1; j < MAX_PARMS && p[j] !=3D NULL; ++j)
+ for (int j =3D 1; j <= MAX_PARMS && p[j] !=3D NULL; ++j)

{
options->pkcs11_protected_authentication[j - 1] = =3D
- at= oi_warn(p[j], msglevel) !=3D 0 ? 1 : 0;
+ boolean_flag(p[j], p[0], msglevel);
}
}
else if= (streq(p[0], "pkcs11-private-mode") && p[1])
<= span>@@ -9751,13 +9750,11 @@

}
el= se if (streq(p[0], "pkcs11-cert-private"))
{=
- int j;=
-
VE= RIFY_PERMISSION(OPT_P_GENERAL);

- for (j =3D 1; j < MAX_PARMS &&am= p; p[j] !=3D NULL; ++j)
+ for (int j =3D 1; j < MAX_PARMS && p[j] !=3D NULL; += +j)
{
- options->pkcs11_cert_private[j - 1] =3D (bool)(atoi= _warn(p[j], msglevel));
+ options->pkcs11_cert_private[j - 1] =3D boolean_flag(p[j= ], p[0], msglevel);
}
}
else if (streq(p[0], "pkcs11-pin-cache") &&am= p; p[1] && !p[2])
diff --git a/src/openvpn/options_= util.c b/src/openvpn/options_util.c
index c1f7c74..32a9edb = 100644
--- a/src/openvpn/options_util.c
+++= b/src/openvpn/options_util.c
@@ -181,6 +181,14 @@ return true;
}

+bool

+boolean_flag(const char *str, const char *name,= int msglevel)
+{
+ int number =3D 0;
+ atoi_constrained(= str, &number, name, 0, 1, msglevel);
+ return (bool)number;
+}
+
static const char *updatable_options[] =3D { &quo= t;block-ipv6", "block-outside-dns",
= "dhcp-option", "dns"= ;,
"ifconf= ig", "ifconfig-ipv6",
diff --git a/src/openv= pn/options_util.h b/src/openvpn/options_util.h
index d5ae8b= 9..b9e1569 100644
--- a/src/openvpn/options_util.h+++ b/src/openvpn/options_util.h
@@ -59,6 +59,12 @@=
int msglevel);

/**
+ = * Converts a str to an boolean if the string can be parsed as either 0 or 1= .
+ * Otherwise print= a warning with \p msglevel and return \c false.
+ */
+bool boolean_flag(const char *str, const char *name, int msgle= vel);
+
+/**

* Filter an= option line by all pull filters.
*
* I= f a match is found, the line is modified depending on
diff = --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvpn/tes= t_misc.c
index 80a5dee..2d2cc9e 100644
--- = a/tests/unit_tests/openvpn/test_misc.c
+++ b/tests/unit_tes= ts/openvpn/test_misc.c
@@ -431,6 +431,22 @@
assert_string_equal(mock_msg_buf, "test: Must be an integer >= =3D 1, not 0");

assert_int_equal(parameter, -42);=

+ = /* special tests for boolean_flag */
+ assert_false(boolean_flag("0", "test= ", msglevel));
+= assert_true(boolean_flag("1", "test", msglevel));
+
+ CLEAR(mock_msg_buf);
+ assert_false(boolean_flag("= ;foo77", "test", msglevel));

+ assert_string_equal(mock_msg_buf, "test: C= annot parse 'foo77' as integer");
+
+ CLEAR(mock_msg_buf);
+ assert_false(boolean_flag("-77", "test"= ;, msglevel));
+ a= ssert_string_equal(mock_msg_buf, "test: Must be an integer between 0 a= nd 1, not -77");
+
+ CLEAR(mock_ms= g_buf);
+ assert_f= alse(boolean_flag("77", "test", msglevel));
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_string_equal(mock_ms= g_buf, "test: Must be an integer between 0 and 1, not 77");
+
moc= k_set_debug_level(saved_log_level);
}


To view, visit change 1151. To unsubscribe, o= r for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ic30612971367a4aa54ca89f9345= 2efc172d4f4e2
Gerrit-Change-Number: 1151
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-MessageType: newchange
--E51vmMfb/LM=--