Skip to content

Commit 82eac97

Browse files
committed
Clean up casts and union typedefs in network
Refactors several networking and libc helpers for safer, cleaner C++ style: replaces C-style casts with `static_cast`, uses `nullptr` checks, and converts `typedef union` patterns to named/aliased unions. It also updates `debug_printbits` formatting casts and include placement under `#ifndef NDEBUG`, plus applies consistent brace/formatting cleanup in the NTP client implementation.
1 parent fa123de commit 82eac97

7 files changed

Lines changed: 82 additions & 115 deletions

File tree

common/include/firmware/debug/debug_printbits.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@
2626
#ifndef COMMON_DEBUG_DEBUG_PRINTBITS_H_
2727
#define COMMON_DEBUG_DEBUG_PRINTBITS_H_
2828

29-
#include <cstdio>
3029
#include <cstdint>
3130

3231
namespace debug {
3332
#ifdef NDEBUG
3433
inline void PrintBits([[maybe_unused]] uint32_t u) {}
3534
#else
35+
#include <cstdio>
3636
inline void PrintBits(uint32_t u) {
37-
printf("%.8x ", u);
37+
printf("%.8x ", static_cast<unsigned>(u));
3838
uint32_t b = 1U << 31;
3939

4040
for (uint32_t i = 0; i < 32; i++) {
4141
if ((b & u) == b) {
4242
uint32_t bit_number = 31 - i;
43-
printf("%-2d ", bit_number);
43+
printf("%-2u ", static_cast<unsigned>(bit_number));
4444
}
4545
b = b >> 1;
4646
}
@@ -50,4 +50,4 @@ inline void PrintBits(uint32_t u) {
5050
#endif
5151
} // namespace debug
5252

53-
#endif /* COMMON_DEBUG_DEBUG_PRINTBITS_H_ */
53+
#endif // COMMON_DEBUG_DEBUG_PRINTBITS_H_

lib-clib/src/inet_aton.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file inet_aton.cpp
33
*
44
*/
5-
/* Copyright (C) 2016-2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2016-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -27,14 +27,18 @@
2727
#include <cctype>
2828
#include <netinet/in.h>
2929

30-
typedef union pcast32 {
30+
namespace {
31+
using _pcast32 = union pcast32 {
3132
uint32_t u32;
3233
uint8_t u8[4];
33-
} _pcast32;
34+
};
35+
} // namespace
3436

3537
extern "C" int inet_aton(const char* cp, struct in_addr* ip_address) {
3638
const char* b = cp;
37-
int i, j, k;
39+
int i;
40+
int j;
41+
int k;
3842
_pcast32 cast32;
3943

4044
for (i = 0; i < 3; i++) {
@@ -46,12 +50,12 @@ extern "C" int inet_aton(const char* cp, struct in_addr* ip_address) {
4650
return 0;
4751
}
4852

49-
if (0 == isdigit((int)*b)) {
53+
if (0 == isdigit(static_cast<int>(*b))) {
5054
return 0;
5155
}
5256

5357
j++;
54-
k = k * 10 + (int)*b - (int)'0';
58+
k = (k * 10) + static_cast<int>(*b) - '0';
5559
b++;
5660
}
5761

@@ -72,13 +76,13 @@ extern "C" int inet_aton(const char* cp, struct in_addr* ip_address) {
7276
}
7377

7478
j++;
75-
k = k * 10 + (int)*b - (int)'0';
79+
k = (k * 10) + static_cast<int>(*b) - '0';
7680
b++;
7781
}
7882

7983
cast32.u8[i] = (uint8_t)k;
8084

81-
if (ip_address != 0) {
85+
if (ip_address != nullptr) {
8286
ip_address->s_addr = cast32.u32;
8387
}
8488

lib-clib/src/inet_ntoa.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
static char buffer[18];
3030

3131
extern "C" char* inet_ntoa(struct in_addr in) {
32-
unsigned char* bytes = (unsigned char*)&in;
32+
auto* bytes = (unsigned char*)&in;
3333
snprintf(buffer, sizeof(buffer), "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
3434
return buffer;
3535
}

0 commit comments

Comments
 (0)