From: "cron2 (Code Review)" Date: Mon, 28 Jul 2025 14:14:42 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <708480a93af92c62502989fc673b1ef544234113-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="7kGgNAD4OEs="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: options: Simplify function setenv_foreign_option List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: plaisthos , openvpn-devel --7kGgNAD4OEs= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/11= 12?usp=3Demail ) Change subject: options: Simplify function setenv_foreign_option ...................................................................... options: Simplify function setenv_foreign_option This was relatively complex for the actual usage. Looked at the code because of -Wconversion warnings related to the len argument. So this should also be gone. Change-Id: I7efc77f63734501dfa8a8f5bed17b1a1b4e9e201 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Message-Id: <20250728125647.26992-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg32= 396.html Signed-off-by: Gert Doering --- M src/openvpn/options.c 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 0662b49..d44c102 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -1067,41 +1067,32 @@ #ifndef _WIN32 static void -setenv_foreign_option(struct options *o, const char *argv[], int len, stru= ct env_set *es) +setenv_foreign_option(struct options *o, const char *option, const char *v= alue, struct env_set *es) { - if (len > 0) - { - struct gc_arena gc =3D gc_new(); - struct buffer name =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc); - struct buffer value =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc); - int i; - bool first =3D true; - bool good =3D true; + struct gc_arena gc =3D gc_new(); + struct buffer env_name =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc); + struct buffer env_value =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc); + bool good =3D true; - good &=3D buf_printf(&name, "foreign_option_%d", o->foreign_option= _index + 1); - ++o->foreign_option_index; - for (i =3D 0; i < len; ++i) - { - if (argv[i]) - { - if (!first) - { - good &=3D buf_printf(&value, " "); - } - good &=3D buf_printf(&value, "%s", argv[i]); - first =3D false; - } - } - if (good) - { - setenv_str(es, BSTR(&name), BSTR(&value)); - } - else - { - msg(M_WARN, "foreign_option: name/value overflow"); - } - gc_free(&gc); + good &=3D buf_printf(&env_name, "foreign_option_%d", o->foreign_option= _index + 1); + if (value) + { + good &=3D buf_printf(&env_value, "dhcp-option %s %s", option, valu= e); } + else + { + good &=3D buf_printf(&env_value, "dhcp-option %s", option); + } + if (good) + { + setenv_str(es, BSTR(&env_name), BSTR(&env_value)); + ++o->foreign_option_index; + } + else + { + msg(M_WARN, "foreign_option: name/value overflow"); + } + gc_free(&gc); } #endif /* ifndef _WIN32 */ @@ -3678,15 +3669,10 @@ else if (o->up_script && !dns_updown_user_set(dns) && !dns_updown_forc= ed(dns)) { /* Set foreign option env vars from --dns config */ - const char *p[] =3D { "dhcp-option", NULL, NULL }; - size_t p_len =3D sizeof(p) / sizeof(p[0]); - - p[1] =3D "DOMAIN"; const struct dns_domain *d =3D dns->search_domains; while (d) { - p[2] =3D d->name; - setenv_foreign_option(o, (const char **)p, p_len, es); + setenv_foreign_option(o, "DOMAIN", d->name, es); d =3D d->next; } @@ -3713,17 +3699,19 @@ { for (int i =3D 0; i < s->addr_count; ++i) { + const char *option; + const char *value; if (s->addr[i].family =3D=3D AF_INET) { - p[1] =3D "DNS"; - p[2] =3D print_in_addr_t(s->addr[i].in.a4.s_addr, = IA_NET_ORDER, &gc); + option =3D "DNS"; + value =3D print_in_addr_t(s->addr[i].in.a4.s_addr,= IA_NET_ORDER, &gc); } else { - p[1] =3D "DNS6"; - p[2] =3D print_in6_addr(s->addr[i].in.a6, 0, &gc); + option =3D "DNS6"; + value =3D print_in6_addr(s->addr[i].in.a6, 0, &gc); } - setenv_foreign_option(o, (const char **)p, p_len, es); + setenv_foreign_option(o, option, value, es); } break; } @@ -8388,7 +8376,7 @@ goto err; } #else /* if defined(_WIN32) || defined(TARGET_ANDROID) */ - setenv_foreign_option(options, (const char **)p, 3, es); + setenv_foreign_option(options, p[1], p[2], es); #endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */ } #ifdef _WIN32 @@ -8530,7 +8518,7 @@ else if (streq(p[0], "dhcp-option") && p[1] && !p[3]) { VERIFY_PERMISSION(OPT_P_DHCPDNS); - setenv_foreign_option(options, (const char **)p, 3, es); + setenv_foreign_option(options, p[1], p[2], es); } else if (streq(p[0], "route-method") && p[1] && !p[2]) { --=20 To view, visit http://gerrit.openvpn.net/c/openvpn/+/1112?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: I7efc77f63734501dfa8a8f5bed17b1a1b4e9e201 Gerrit-Change-Number: 1112 Gerrit-PatchSet: 4 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: merged --7kGgNAD4OEs= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

options: Simplify function setenv_foreign_opt=
ion

This was relatively complex for the actual usage.
Looked at t= he code because of -Wconversion warnings
related to the len argument. So= this should also be
gone.

Change-Id: I7efc77f63734501dfa8a8f5bed= 17b1a1b4e9e201
Signed-off-by: Frank Lichtenheld <frank@...2641...>=
Acked-by: Gert Doering <gert@...1296...>
Message-Id: <20250= 728125647.26992-1-gert@...1296...>
URL: https://www.mail-archive.com/= openvpn-devel@lists.sourceforge.net/msg32396.html
Signed-off-by: Gert Do= ering <gert@...1296...>
---
M src/openvpn/options.c
1 file c= hanged, 33 insertions(+), 45 deletions(-)

diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index 0662b49..d44c102 100644
--- a/src/openvpn/options.= c
+++ b/src/openvpn/options.c
@@ -1067,41 += 1067,32 @@

#ifndef _WIN32
static void
-setenv_= foreign_option(struct options *o, const char *argv[], int len, struct env_s= et *es)
+setenv_forei= gn_option(struct options *o, const char *option, const char *value, struct = env_set *es)
{
- if (len > 0)
- {
- = struct gc_arena gc =3D gc_new();
- struct buffer name =3D alloc_buf_gc(OPTION_PARM_SIZE, &a= mp;gc);
- struct= buffer value =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc);
- int i;
- bool first =3D true;
- bool good =3D true;+ struct gc_arena gc =3D g= c_new();
+ struct = buffer env_name =3D alloc_buf_gc(OPTION_PARM_SIZE, &gc);
+ struct buffer env_value =3D al= loc_buf_gc(OPTION_PARM_SIZE, &gc);
+ bool good =3D true;

- good &=3D buf_printf(&am= p;name, "foreign_option_%d", o->foreign_option_index + 1);

- ++o->foreign_o= ption_index;
- f= or (i =3D 0; i < len; ++i)
- {
- = if (argv[i])
- = {
- = if (!first)
- = {
- = good &=3D buf_printf(&value, " ");- }
- good &=3D buf_p= rintf(&value, "%s", argv[i]);
- first =3D false;
- }
- }
- if (good)
- {
- = setenv_str(es, BSTR(&name), BSTR(&value));
- }
- else
- {
- msg(M_WARN, "foreign_option: name/value overflow"= ;);
- }- gc_free(&gc);
+ good &=3D buf_pr= intf(&env_name, "foreign_option_%d", o->foreign_option_ind= ex + 1);
+ if (val= ue)
+ {
= + good &=3D buf_prin= tf(&env_value, "dhcp-option %s %s", option, value); }
+ el= se
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ good &=3D buf_print= f(&env_value, "dhcp-option %s", option);

+ }
+ if (good)
+ {
= + setenv_str(es, BSTR(&env_name), BSTR(&env_value));<= br>+ ++o->foreign_opt= ion_index;
+ }

+ else
+ {
+ msg(M_WARN, "foreign_option: name/v= alue overflow");
+ }
+ gc_free(= &gc);
}
#endif /* ifndef _WIN32 */

@@ -3678,15 +3669,10 @@
= else if (o->up_script && !dns_updown_user_set(dns) && = !dns_updown_forced(dns))
{
/*= Set foreign option env vars from --dns config */
- const char *p[] =3D { "dhcp-option= ", NULL, NULL };
-= size_t p_len =3D sizeof(p) / sizeof(p[0]);
-
- p[1] =3D "DOMAIN";
const= struct dns_domain *d =3D dns->search_domains;
= while (d)
{
- p[2] =3D d->name;
- setenv_foreign_option(o, (const char = **)p, p_len, es);
+ = setenv_foreign_option(o, "DOMAIN", d->name, es);

d =3D d->next;
}

@@ -3713,17 +3699,19 @@
= {
for (int i =3D 0; i < s->a= ddr_count; ++i)
{
+ const char *option;
+ cons= t char *value;
if (s->addr[i].famil= y =3D=3D AF_INET)
{
- p[1] =3D "D= NS";
- = p[2] =3D print_in_addr_t(s->addr[i].in.a4.s_addr, IA_NET_ORD= ER, &gc);
+ = option =3D "DNS";
+ value =3D print_in_addr_t= (s->addr[i].in.a4.s_addr, IA_NET_ORDER, &gc);
= }
else
= {
= - p[1] =3D "DNS6";
- p[2] =3D print_in6_= addr(s->addr[i].in.a6, 0, &gc);
+ option =3D "DNS6";
+ = value =3D print_in6_addr(s->addr[i].in.a6, 0, &gc);
}

- setenv_foreign_option(o, (const char **)p, p_len, es)= ;
+ = setenv_foreign_option(o, option, value, es);
= }
break;
= }
@@ -8388,7 +8376,7 @@
goto = err;
}
#else /* if defined(_WIN32= ) || defined(TARGET_ANDROID) */
- setenv_foreign_option(options, (const char **)p, 3, es);<= /span>
+ setenv_forei= gn_option(options, p[1], p[2], es);
#endif /* if defined(_= WIN32) || defined(TARGET_ANDROID) */
}
#ifdef _WIN32

@@ -8530,7 +8518,7 @@
= else if (streq(p[0], "dhcp-option") && p[1] && !p= [3])
{
VERIFY_PERMISSION(OPT_= P_DHCPDNS);
- se= tenv_foreign_option(options, (const char **)p, 3, es);
+ setenv_foreign_option(options, p= [1], p[2], es);
}
else if (streq(= p[0], "route-method") && p[1] && !p[2]) {

To view, visit change 1112. T= o unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I7efc77f63734501dfa8a8f5bed1= 7b1a1b4e9e201
Gerrit-Change-Number: 1112
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: merged
--7kGgNAD4OEs=--