From: "flichtenheld (Code Review)" Date: Fri, 10 Oct 2025 22:49:21 +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="3Ag45k8iCbw="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: dhcp: Clean up type handling of write_dhcp_* List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --3Ag45k8iCbw= 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/+/1268?usp=3Demail to review the following change. Change subject: dhcp: Clean up type handling of write_dhcp_* ...................................................................... dhcp: Clean up type handling of write_dhcp_* Use more appropriate types. Add casts where necessary but ensure that they are safe. Change-Id: I30a50826350ac3176443cf3bf16d3972609723a2 Signed-off-by: Frank Lichtenheld --- M src/openvpn/dhcp.c M src/openvpn/options.c M src/openvpn/tun.h 3 files changed, 21 insertions(+), 32 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/68/1268/1 diff --git a/src/openvpn/dhcp.c b/src/openvpn/dhcp.c index 0893ec7..a34bfca 100644 --- a/src/openvpn/dhcp.c +++ b/src/openvpn/dhcp.c @@ -188,18 +188,13 @@ #if defined(_WIN32) || defined(DHCP_UNIT_TEST) -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wconversion" -#endif - /* * Convert DHCP options from the command line / config file * into a raw DHCP-format options string. */ static void -write_dhcp_u8(struct buffer *buf, const int type, const int data, bool *er= ror) +write_dhcp_u8(struct buffer *buf, const uint8_t type, const uint8_t data, = bool *error) { if (!buf_safe(buf, 3)) { @@ -213,13 +208,12 @@ } static void -write_dhcp_u32_array(struct buffer *buf, const int type, const uint32_t *d= ata, +write_dhcp_u32_array(struct buffer *buf, const uint8_t type, const uint32_= t *data, const unsigned int len, bool *error) { if (len > 0) { - int i; - const int size =3D len * sizeof(uint32_t); + const size_t size =3D len * sizeof(uint32_t); if (!buf_safe(buf, 2 + size)) { @@ -230,12 +224,12 @@ if (size < 1 || size > 255) { *error =3D true; - msg(M_WARN, "write_dhcp_u32_array: size (%d) must be > 0 and <= =3D 255", size); + msg(M_WARN, "write_dhcp_u32_array: size (%zu) must be > 0 and = <=3D 255", size); return; } buf_write_u8(buf, type); - buf_write_u8(buf, size); - for (i =3D 0; i < len; ++i) + buf_write_u8(buf, (uint8_t)size); + for (unsigned int i =3D 0; i < len; ++i) { buf_write_u32(buf, data[i]); } @@ -243,9 +237,9 @@ } static void -write_dhcp_str(struct buffer *buf, const int type, const char *str, bool *= error) +write_dhcp_str(struct buffer *buf, const uint8_t type, const char *str, bo= ol *error) { - const int len =3D strlen(str); + const size_t len =3D strlen(str); if (!buf_safe(buf, 2 + len)) { *error =3D true; @@ -259,7 +253,7 @@ return; } buf_write_u8(buf, type); - buf_write_u8(buf, len); + buf_write_u8(buf, (uint8_t)len); buf_write(buf, str, len); } @@ -272,15 +266,14 @@ * 0x1D 0x7 openvpn 0x3 net 0x00 0x0A duckduckgo 0x3 com 0x00 */ static void -write_dhcp_search_str(struct buffer *buf, const int type, const char *cons= t *str_array, +write_dhcp_search_str(struct buffer *buf, const uint8_t type, const char *= const *str_array, int array_len, bool *error) { char tmp_buf[256]; - int i; - int len =3D 0; - int label_length_pos; + size_t len =3D 0; + size_t label_length_pos; - for (i =3D 0; i < array_len; i++) + for (int i =3D 0; i < array_len; i++) { const char *ptr =3D str_array[i]; @@ -301,7 +294,8 @@ { if (*ptr =3D=3D '.' || *ptr =3D=3D '\0') { - tmp_buf[label_length_pos] =3D (len - label_length_pos) - 1; + /* cast is protected by sizeof(tmp_buf) */ + tmp_buf[label_length_pos] =3D (char)(len - label_length_po= s - 1); label_length_pos =3D len; if (*ptr =3D=3D '\0') { @@ -328,14 +322,10 @@ } buf_write_u8(buf, type); - buf_write_u8(buf, len); + buf_write_u8(buf, (uint8_t)len); buf_write(buf, tmp_buf, len); } -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif - bool build_dhcp_options_string(struct buffer *buf, const struct tuntap_options = *o) { diff --git a/src/openvpn/options.c b/src/openvpn/options.c index f2e6dec..a0f5743 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -1322,7 +1322,7 @@ SHOW_BOOL(dhcp_pre_release); SHOW_STR(domain); SHOW_STR(netbios_scope); - SHOW_INT(netbios_node_type); + SHOW_UNSIGNED(netbios_node_type); SHOW_BOOL(disable_nbt); show_dhcp_option_addrs("DNS", o->dns, o->dns_len); @@ -8002,7 +8002,7 @@ msg(msglevel, "--dhcp-option NBT: parameter (%d) must be 1= , 2, 4, or 8", t); goto err; } - o->netbios_node_type =3D t; + o->netbios_node_type =3D (uint8_t)t; o->dhcp_options |=3D DHCP_OPTIONS_DHCP_REQUIRED; } else if (streq(p[1], "WINS") && p[2] && !p[3]) diff --git a/src/openvpn/tun.h b/src/openvpn/tun.h index 741798d..e13f99f 100644 --- a/src/openvpn/tun.h +++ b/src/openvpn/tun.h @@ -104,11 +104,10 @@ const char *netbios_scope; /* NBS (47) */ - int netbios_node_type; /* NBT 1,2,4,8 (46) */ + uint8_t netbios_node_type; /* NBT 1,2,4,8 (46) */ -#define N_DHCP_ADDR \ - 4 /* Max # of addresses allowed for \ - * DNS, WINS, etc. */ +/* Max # of addresses allowed for DNS, WINS, etc. */ +#define N_DHCP_ADDR 4 /* DNS (6) */ in_addr_t dns[N_DHCP_ADDR]; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1268?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newchange Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I30a50826350ac3176443cf3bf16d3972609723a2 Gerrit-Change-Number: 1268 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --3Ag45k8iCbw= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

V= iew Change

dhcp: Clean up type handling of write_dhcp_=
*

Use more appropriate types. Add casts where
necessary but ensur= e that they are safe.

Change-Id: I30a50826350ac3176443cf3bf16d397260= 9723a2
Signed-off-by: Frank Lichtenheld <frank@...2641...>
---<= br>M src/openvpn/dhcp.c
M src/openvpn/options.c
M src/openvpn/tun.h3 files changed, 21 insertions(+), 32 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/68/1268/1=
diff --git a/src/openvpn/dhcp.c b/src/openvpn/dhcp.c
index 0893ec7..a34bfca 100644
--- a/src/openvpn/dhcp.c
+++ b/src/openvpn/dhcp.c
@@ -188,18 +188,13 = @@

#if defined(_WIN32) || defined(DHCP_U= NIT_TEST)

-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wco= nversion"
-#endif<= /span>
-
/*
* Convert DHCP options from the command line / config file=
* into a raw DHCP-format options string.
*/


static void
-write_dhcp_u8(struct buffer *buf, const int= type, const int data, bool *error)
+write_dhcp_u8(struct buffer *buf, const uint8_t type, const= uint8_t data, bool *error)
{
if (!bu= f_safe(buf, 3))
{
@@ -213,13 +208,12 @= @
}

static void<= br>-write_dhcp_u32_array(struct b= uffer *buf, const int type, const uint32_t *data,
+write_dhcp_u32_array(struct buffer *buf, cons= t uint8_t type, const uint32_t *data,
= const unsigned int len, bool *error)
{
= if (len > 0)
{
- int i;
- const int size =3D len * sizeof(uint32_t);
<= span style=3D"color: hsl(120, 100%, 40%);">+ const size_t size =3D l= en * sizeof(uint32_t);


if (!buf_= safe(buf, 2 + size))
{
@@ -230,12 = +224,12 @@
if (size < 1 || size > 255)
{
*error =3D true;<= br>- msg(M_WARN, "= ;write_dhcp_u32_array: size (%d) must be > 0 and <=3D 255", size= );
+ msg(M= _WARN, "write_dhcp_u32_array: size (%zu) must be > 0 and <=3D 25= 5", size);
return;
= }
buf_write_u8(buf, type);
- buf_write_u8(buf, size);- for (i =3D 0; i < le= n; ++i)
+ buf_= write_u8(buf, (uint8_t)size);
+ for (unsigned int i =3D 0; i < len; ++i)
{

buf_write_u32(buf, data[i]);

}
@@ -243,9 +237,9 @@
}

static void
-write_dhcp_str(struct buffer *buf, const int t= ype, const char *str, bool *error)
+write_dhcp_str(struct buffer *buf, const uint8_t type, const= char *str, bool *error)
{
- const int len =3D strlen(str);
+ const size_t len =3D strlen(str);<= /span>
if (!buf_safe(buf, 2 + len))
{
*error =3D true;
@@ -259,7 +253,7 @@=
return;
}
= buf_write_u8(buf, type);
- buf_write_u8(buf, len);
+ buf_write_u8(buf, (uint8_t)len);
buf= _write(buf, str, len);
}

@@ -272,15 +266,14 @@

* 0x1D 0x7 openvpn 0x3 net 0x00 = 0x0A duckduckgo 0x3 com 0x00
*/
static v= oid
-write_dhcp_search_= str(struct buffer *buf, const int type, const char *const *str_array,

+write_dhcp_search_str(str= uct buffer *buf, const uint8_t type, const char *const *str_array, int array_len, bool *error)
= {
char tmp_buf[256];
- int i;
- int len =3D 0;
- int label_length_pos;
+ size_t len =3D 0;
+ size_t label_length_pos;

- for (i =3D 0; i < array_len;= i++)
+ for (int i= =3D 0; i < array_len; i++)
{
= const char *ptr =3D str_array[i];

@@ = -301,7 +294,8 @@
{
if= (*ptr =3D=3D '.' || *ptr =3D=3D '\0')
= {
- = tmp_buf[label_length_pos] =3D (len - label_length_pos) - 1;
= + /* cast is pro= tected by sizeof(tmp_buf) */
+ tmp_buf[label_length_pos] =3D (char)(len - label_l= ength_pos - 1);
label_length_pos =3D len;<= /span>
if (*ptr =3D=3D '\0')
{

@@ -328,14 +322,10 @@
}

buf_write_u8(buf, type);
- buf_write_u8(buf, le= n);
+ buf_write_u8= (buf, (uint8_t)len);
buf_write(buf, tmp_buf, len);

}

-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
bool
build_dhcp_= options_string(struct buffer *buf, const struct tuntap_options *o) {
diff --git a/src/openvpn/options.c b/src/openvpn= /options.c
index f2e6dec..a0f5743 100644
--= - a/src/openvpn/options.c
+++ b/src/openvpn/options.c
@@ -1322,7 +1322,7 @@
SHOW_BOOL(dhcp_pre_re= lease);
SHOW_STR(domain);
SHOW_ST= R(netbios_scope);
- = SHOW_INT(netbios_node_type);
+ SHOW_UNSIGNED(netbios_node_type);
SHOW_BO= OL(disable_nbt);

show_dhcp_option_ad= drs("DNS", o->dns, o->dns_len);
@@ -8002,7 = +8002,7 @@
msg(msglevel, "--dhcp-opti= on NBT: parameter (%d) must be 1, 2, 4, or 8", t);
= goto err;
}
- o->netbios_node_type =3D t;=
+ o->n= etbios_node_type =3D (uint8_t)t;
o->dhcp_op= tions |=3D DHCP_OPTIONS_DHCP_REQUIRED;
} else if (streq(p[1], "WINS") && p[2] &= ;& !p[3])
diff --git a/src/openvpn/tun.h b/src/openvpn/= tun.h
index 741798d..e13f99f 100644
--- a/s= rc/openvpn/tun.h
+++ b/src/openvpn/tun.h
@@= -104,11 +104,10 @@

const char *netb= ios_scope; /* NBS (47) */

- int netbios_node_type; /* NBT 1,2,4,8 (46) */=
+ uint8_t netbios= _node_type; /* NBT 1,2,4,8 (46) */

-#define N_DHCP_ADDR \
- 4 /* Max # of addre= sses allowed for \
- = * DNS, WINS, etc. */
+/* Max # of addresses allowed for DNS, WINS, etc. */
+#define N_DHCP_ADDR 4

/* DNS (6) */
in_addr_t dns[N= _DHCP_ADDR];

To view, visit change 1268. To= unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I30a50826350ac3176443cf3bf16= d3972609723a2
Gerrit-Change-Number: 1268
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...>
= --3Ag45k8iCbw=--