Skip to content

Commit b298a74

Browse files
flichtenheldcron2
authored andcommitted
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 <[email protected]> Acked-by: Gert Doering <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1268 Message-Id: <[email protected]> URL: https://sourceforge.net/p/openvpn/mailman/message/59246219/ Signed-off-by: Gert Doering <[email protected]>
1 parent c4f7b41 commit b298a74

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

src/openvpn/dhcp.c

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,13 @@ dhcp_extract_router_msg(struct buffer *ipbuf)
188188

189189
#if defined(_WIN32) || defined(DHCP_UNIT_TEST)
190190

191-
#if defined(__GNUC__) || defined(__clang__)
192-
#pragma GCC diagnostic push
193-
#pragma GCC diagnostic ignored "-Wconversion"
194-
#endif
195-
196191
/*
197192
* Convert DHCP options from the command line / config file
198193
* into a raw DHCP-format options string.
199194
*/
200195

201196
static void
202-
write_dhcp_u8(struct buffer *buf, const int type, const int data, bool *error)
197+
write_dhcp_u8(struct buffer *buf, const uint8_t type, const uint8_t data, bool *error)
203198
{
204199
if (!buf_safe(buf, 3))
205200
{
@@ -213,13 +208,12 @@ write_dhcp_u8(struct buffer *buf, const int type, const int data, bool *error)
213208
}
214209

215210
static void
216-
write_dhcp_u32_array(struct buffer *buf, const int type, const uint32_t *data,
211+
write_dhcp_u32_array(struct buffer *buf, const uint8_t type, const uint32_t *data,
217212
const unsigned int len, bool *error)
218213
{
219214
if (len > 0)
220215
{
221-
int i;
222-
const int size = len * sizeof(uint32_t);
216+
const size_t size = len * sizeof(uint32_t);
223217

224218
if (!buf_safe(buf, 2 + size))
225219
{
@@ -230,22 +224,22 @@ write_dhcp_u32_array(struct buffer *buf, const int type, const uint32_t *data,
230224
if (size < 1 || size > 255)
231225
{
232226
*error = true;
233-
msg(M_WARN, "write_dhcp_u32_array: size (%d) must be > 0 and <= 255", size);
227+
msg(M_WARN, "write_dhcp_u32_array: size (%zu) must be > 0 and <= 255", size);
234228
return;
235229
}
236230
buf_write_u8(buf, type);
237-
buf_write_u8(buf, size);
238-
for (i = 0; i < len; ++i)
231+
buf_write_u8(buf, (uint8_t)size);
232+
for (unsigned int i = 0; i < len; ++i)
239233
{
240234
buf_write_u32(buf, data[i]);
241235
}
242236
}
243237
}
244238

245239
static void
246-
write_dhcp_str(struct buffer *buf, const int type, const char *str, bool *error)
240+
write_dhcp_str(struct buffer *buf, const uint8_t type, const char *str, bool *error)
247241
{
248-
const int len = strlen(str);
242+
const size_t len = strlen(str);
249243
if (!buf_safe(buf, 2 + len))
250244
{
251245
*error = true;
@@ -259,7 +253,7 @@ write_dhcp_str(struct buffer *buf, const int type, const char *str, bool *error)
259253
return;
260254
}
261255
buf_write_u8(buf, type);
262-
buf_write_u8(buf, len);
256+
buf_write_u8(buf, (uint8_t)len);
263257
buf_write(buf, str, len);
264258
}
265259

@@ -272,15 +266,14 @@ write_dhcp_str(struct buffer *buf, const int type, const char *str, bool *error)
272266
* 0x1D 0x7 openvpn 0x3 net 0x00 0x0A duckduckgo 0x3 com 0x00
273267
*/
274268
static void
275-
write_dhcp_search_str(struct buffer *buf, const int type, const char *const *str_array,
269+
write_dhcp_search_str(struct buffer *buf, const uint8_t type, const char *const *str_array,
276270
int array_len, bool *error)
277271
{
278272
char tmp_buf[256];
279-
int i;
280-
int len = 0;
281-
int label_length_pos;
273+
size_t len = 0;
274+
size_t label_length_pos;
282275

283-
for (i = 0; i < array_len; i++)
276+
for (int i = 0; i < array_len; i++)
284277
{
285278
const char *ptr = str_array[i];
286279

@@ -301,7 +294,8 @@ write_dhcp_search_str(struct buffer *buf, const int type, const char *const *str
301294
{
302295
if (*ptr == '.' || *ptr == '\0')
303296
{
304-
tmp_buf[label_length_pos] = (len - label_length_pos) - 1;
297+
/* cast is protected by sizeof(tmp_buf) */
298+
tmp_buf[label_length_pos] = (char)(len - label_length_pos - 1);
305299
label_length_pos = len;
306300
if (*ptr == '\0')
307301
{
@@ -328,14 +322,10 @@ write_dhcp_search_str(struct buffer *buf, const int type, const char *const *str
328322
}
329323

330324
buf_write_u8(buf, type);
331-
buf_write_u8(buf, len);
325+
buf_write_u8(buf, (uint8_t)len);
332326
buf_write(buf, tmp_buf, len);
333327
}
334328

335-
#if defined(__GNUC__) || defined(__clang__)
336-
#pragma GCC diagnostic pop
337-
#endif
338-
339329
bool
340330
build_dhcp_options_string(struct buffer *buf, const struct tuntap_options *o)
341331
{

src/openvpn/options.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ show_tuntap_options(const struct tuntap_options *o)
13221322
SHOW_BOOL(dhcp_pre_release);
13231323
SHOW_STR(domain);
13241324
SHOW_STR(netbios_scope);
1325-
SHOW_INT(netbios_node_type);
1325+
SHOW_UNSIGNED(netbios_node_type);
13261326
SHOW_BOOL(disable_nbt);
13271327

13281328
show_dhcp_option_addrs("DNS", o->dns, o->dns_len);
@@ -8001,7 +8001,7 @@ add_option(struct options *options, char *p[], bool is_inline, const char *file,
80018001
msg(msglevel, "--dhcp-option NBT: parameter (%d) must be 1, 2, 4, or 8", t);
80028002
goto err;
80038003
}
8004-
o->netbios_node_type = t;
8004+
o->netbios_node_type = (uint8_t)t;
80058005
o->dhcp_options |= DHCP_OPTIONS_DHCP_REQUIRED;
80068006
}
80078007
else if (streq(p[1], "WINS") && p[2] && !p[3])

src/openvpn/tun.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,10 @@ struct tuntap_options
104104

105105
const char *netbios_scope; /* NBS (47) */
106106

107-
int netbios_node_type; /* NBT 1,2,4,8 (46) */
107+
uint8_t netbios_node_type; /* NBT 1,2,4,8 (46) */
108108

109-
#define N_DHCP_ADDR \
110-
4 /* Max # of addresses allowed for \
111-
* DNS, WINS, etc. */
109+
/* Max # of addresses allowed for DNS, WINS, etc. */
110+
#define N_DHCP_ADDR 4
112111

113112
/* DNS (6) */
114113
in_addr_t dns[N_DHCP_ADDR];

0 commit comments

Comments
 (0)