Skip to content

Commit e4d81e1

Browse files
committed
Eliminate dead return statement.
Previously, the `ipv6` variable was initialised to `-1`, but that value was never read. Either it was set to 0 or 1, or the function would return before it was read. Thus, I've changed it to uninitialised `bool is_ipv4` (inverted semantics to avoid negative conditions `if (!is_ipv6)`). The `pack_ip_port` function is a bit unfortunate, but I'm not rewriting it until we have a binary writer (I'd be rewriting it twice).
1 parent 8ef1f35 commit e4d81e1

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

toxcore/DHT.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -316,27 +316,27 @@ static int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
316316
return -1;
317317
}
318318

319-
int ipv6 = -1;
319+
bool is_ipv4;
320320
uint8_t net_family;
321321

322322
if (ip_port->ip.family == AF_INET) {
323323
// TODO(irungentoo): use functions to convert endianness
324-
ipv6 = 0;
324+
is_ipv4 = true;
325325
net_family = TOX_AF_INET;
326326
} else if (ip_port->ip.family == TCP_INET) {
327-
ipv6 = 0;
327+
is_ipv4 = true;
328328
net_family = TOX_TCP_INET;
329329
} else if (ip_port->ip.family == AF_INET6) {
330-
ipv6 = 1;
330+
is_ipv4 = false;
331331
net_family = TOX_AF_INET6;
332332
} else if (ip_port->ip.family == TCP_INET6) {
333-
ipv6 = 1;
333+
is_ipv4 = false;
334334
net_family = TOX_TCP_INET6;
335335
} else {
336336
return -1;
337337
}
338338

339-
if (ipv6 == 0) {
339+
if (is_ipv4) {
340340
uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t);
341341

342342
if (size > length) {
@@ -347,9 +347,7 @@ static int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
347347
memcpy(data + 1, &ip_port->ip.ip4, SIZE_IP4);
348348
memcpy(data + 1 + SIZE_IP4, &ip_port->port, sizeof(uint16_t));
349349
return size;
350-
}
351-
352-
if (ipv6 == 1) {
350+
} else {
353351
uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t);
354352

355353
if (size > length) {
@@ -361,8 +359,6 @@ static int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
361359
memcpy(data + 1 + SIZE_IP6, &ip_port->port, sizeof(uint16_t));
362360
return size;
363361
}
364-
365-
return -1;
366362
}
367363

368364
static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
@@ -399,34 +395,34 @@ static int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length
399395
return -1;
400396
}
401397

402-
int ipv6 = -1;
398+
bool is_ipv4;
403399
uint8_t host_family;
404400

405401
if (data[0] == TOX_AF_INET) {
406-
ipv6 = 0;
402+
is_ipv4 = true;
407403
host_family = AF_INET;
408404
} else if (data[0] == TOX_TCP_INET) {
409405
if (!tcp_enabled) {
410406
return -1;
411407
}
412408

413-
ipv6 = 0;
409+
is_ipv4 = true;
414410
host_family = TCP_INET;
415411
} else if (data[0] == TOX_AF_INET6) {
416-
ipv6 = 1;
412+
is_ipv4 = false;
417413
host_family = AF_INET6;
418414
} else if (data[0] == TOX_TCP_INET6) {
419415
if (!tcp_enabled) {
420416
return -1;
421417
}
422418

423-
ipv6 = 1;
419+
is_ipv4 = false;
424420
host_family = TCP_INET6;
425421
} else {
426422
return -1;
427423
}
428424

429-
if (ipv6 == 0) {
425+
if (is_ipv4) {
430426
uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t);
431427

432428
if (size > length) {
@@ -437,9 +433,7 @@ static int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length
437433
memcpy(&ip_port->ip.ip4, data + 1, SIZE_IP4);
438434
memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t));
439435
return size;
440-
}
441-
442-
if (ipv6 == 1) {
436+
} else {
443437
uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t);
444438

445439
if (size > length) {
@@ -451,8 +445,6 @@ static int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length
451445
memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t));
452446
return size;
453447
}
454-
455-
return -1;
456448
}
457449

458450
/* Pack number of nodes into data of maxlength length.

0 commit comments

Comments
 (0)