fix(p5-socket): clang-format 18 reflow + cppcheck const-correctness

This commit is contained in:
2026-09-09 14:29:46 +02:00
parent 07d1dd84f7
commit f86ba7a556
7 changed files with 39 additions and 47 deletions
+4 -5
View File
@@ -382,12 +382,11 @@ static Client* connect_transfer_client(const Config* config) {
connect_opts.sockopt_count = config->sockopt_count;
bool connected;
if (config->use_tls) {
connected = client_connect_tls_ex(client, config->server_host, config->server_port,
config->tls_cert, config->tls_key, config->tls_ca,
&connect_opts);
connected =
client_connect_tls_ex(client, config->server_host, config->server_port, config->tls_cert,
config->tls_key, config->tls_ca, &connect_opts);
} else {
connected = client_connect_ex(client, config->server_host, config->server_port,
&connect_opts);
connected = client_connect_ex(client, config->server_host, config->server_port, &connect_opts);
}
if (!connected) {
client_disconnect(client);
+1 -1
View File
@@ -402,7 +402,7 @@ int config_sockopts_parse(const char* spec, SockOptEntry** out, int* out_count)
SockOptEntry* entries = NULL;
char* saveptr = NULL;
bool ok = true;
for (char* token = strtok_r(copy, ",", &saveptr); token != NULL;
for (const char* token = strtok_r(copy, ",", &saveptr); token != NULL;
token = strtok_r(NULL, ",", &saveptr)) {
if (*token == '\0') {
ok = false; /* empty entry: a stray/trailing comma */
+5 -6
View File
@@ -66,8 +66,8 @@ Server* server_create_ex(int port, const ServerBindOptions* bind_opts) {
int err = getaddrinfo(bind_address, port_str, &hints, &result);
if (err != 0 || result == NULL) {
char* escaped = bind_address ? output_escape(bind_address, false) : NULL;
fprintf(stderr, "Could not resolve bind address %s (%s)\n",
escaped ? escaped : "(wildcard)", gai_strerror(err));
fprintf(stderr, "Could not resolve bind address %s (%s)\n", escaped ? escaped : "(wildcard)",
gai_strerror(err));
free(escaped);
free(server);
return NULL;
@@ -172,8 +172,7 @@ static void plain_child_fn(int fd, void* ctx) {
}
bool server_listen(Server* server, void (*handler)(int file_descriptor)) {
log_message(LOG_LEVEL_INFO, "Start Listening on Port: %d",
server_address_port(&server->address));
log_message(LOG_LEVEL_INFO, "Start Listening on Port: %d", server_address_port(&server->address));
struct plain_ctx ctx = {handler};
accept_loop(server, plain_child_fn, &ctx, "Received Connection");
return true;
@@ -403,7 +402,7 @@ bool tcp_connect_socket_ex(Client* client, const char* host, int port,
return true;
}
bool tcp_connect_socket(Client* client, char* host, int port) {
bool tcp_connect_socket(Client* client, const char* host, int port) {
return tcp_connect_socket_ex(client, host, port, NULL);
}
@@ -414,7 +413,7 @@ bool client_connect_ex(Client* client, const char* host, int port, const TcpConn
return true;
}
bool client_connect(Client* client, char* host, int port) {
bool client_connect(Client* client, const char* host, int port) {
if (!tcp_connect_socket_ex(client, host, port, NULL))
return false;
tcp_apply_socket_timeout(client->file_descriptor);
+4 -4
View File
@@ -37,8 +37,8 @@ typedef struct {
* --sockopts). All fields are client/connection-level and never cross the
* wire config frame. */
typedef struct {
const char* bind_address; /* --address: local source address to bind, or NULL */
int family; /* AF_INET / AF_INET6 / AF_UNSPEC (from -4 / -6) */
const char* bind_address; /* --address: local source address to bind, or NULL */
int family; /* AF_INET / AF_INET6 / AF_UNSPEC (from -4 / -6) */
const SockOptEntry* sockopts; /* --sockopts allowlist entries */
int sockopt_count;
} TcpConnectOptions;
@@ -51,10 +51,10 @@ void server_accept_loop(Server* server, void (*child_fn)(int, void*), void* chil
void server_delete(Server** server);
Client* client_create();
bool client_connect_ex(Client* client, const char* host, int port, const TcpConnectOptions* opts);
bool client_connect(Client* client, char* host, int port);
bool client_connect(Client* client, const char* host, int port);
bool tcp_connect_socket_ex(Client* client, const char* host, int port,
const TcpConnectOptions* opts);
bool tcp_connect_socket(Client* client, char* host, int port);
bool tcp_connect_socket(Client* client, const char* host, int port);
void client_disconnect(Client* client);
void client_delete(Client* client);
void tcp_set_timeouts(int timeout_sec, int contimeout_sec);
+1 -1
View File
@@ -221,7 +221,7 @@ bool client_connect_tls_ex(Client* client, const char* host, int port, const cha
return true;
}
bool client_connect_tls(Client* client, char* host, int port, const char* cert_path,
bool client_connect_tls(Client* client, const char* host, int port, const char* cert_path,
const char* key_path, const char* ca_path) {
return client_connect_tls_ex(client, host, port, cert_path, key_path, ca_path, NULL);
}
+1 -1
View File
@@ -12,7 +12,7 @@ bool server_listen_tls(Server* server, void (*handler)(int file_descriptor));
bool client_connect_tls_ex(Client* client, const char* host, int port, const char* cert_path,
const char* key_path, const char* ca_path,
const TcpConnectOptions* opts);
bool client_connect_tls(Client* client, char* host, int port, const char* cert_path,
bool client_connect_tls(Client* client, const char* host, int port, const char* cert_path,
const char* key_path, const char* ca_path);
#endif
+23 -29
View File
@@ -33,9 +33,8 @@ static void test_sockopts_parse_valid() {
out = NULL;
count = 0;
EXPECT_EQ_INT(config_sockopts_parse("SO_RCVBUF=65536,SO_SNDBUF=131072,SO_REUSEADDR=1", &out,
&count),
0);
EXPECT_EQ_INT(
config_sockopts_parse("SO_RCVBUF=65536,SO_SNDBUF=131072,SO_REUSEADDR=1", &out, &count), 0);
EXPECT_EQ_INT(count, 3);
EXPECT_EQ_INT(out[0].id, SOCKOPT_SO_RCVBUF);
EXPECT_EQ_INT(out[0].value, 65536);
@@ -47,16 +46,15 @@ static void test_sockopts_parse_valid() {
}
static void test_sockopts_parse_rejects() {
static const char* const bad[] = {
"IP_TTL=1", /* unknown option name */
"SO_KEEPALIVE", /* missing '=' */
"=1", /* missing option name */
"TCP_NODELAY=", /* missing value */
"TCP_NODELAY=2", /* boolean must be 0/1 */
"TCP_NODELAY=on", /* non-numeric boolean */
"SO_RCVBUF=-1", /* negative buffer */
"SO_SNDBUF=abc", /* non-numeric buffer */
""}; /* empty spec */
static const char* const bad[] = {"IP_TTL=1", /* unknown option name */
"SO_KEEPALIVE", /* missing '=' */
"=1", /* missing option name */
"TCP_NODELAY=", /* missing value */
"TCP_NODELAY=2", /* boolean must be 0/1 */
"TCP_NODELAY=on", /* non-numeric boolean */
"SO_RCVBUF=-1", /* negative buffer */
"SO_SNDBUF=abc", /* non-numeric buffer */
""}; /* empty spec */
for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) {
SockOptEntry* out = NULL;
int count = 0;
@@ -74,19 +72,17 @@ static void test_sockopts_apply_sets_option() {
int fd = socket(AF_INET, SOCK_STREAM, 0);
EXPECT_TRUE(fd >= 0);
if (fd >= 0) {
for (int i = 0; i < count; i++) {
int value = entries[i].value;
int level = entries[i].id == SOCKOPT_TCP_NODELAY ? IPPROTO_TCP : SOL_SOCKET;
int name = entries[i].id == SOCKOPT_TCP_NODELAY ? TCP_NODELAY : SO_REUSEADDR;
EXPECT_EQ_INT(setsockopt(fd, level, name, &value, sizeof(value)), 0);
}
int got = 0;
socklen_t len = sizeof(got);
EXPECT_EQ_INT(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &got, &len), 0);
EXPECT_EQ_INT(got, 1);
close(fd);
for (int i = 0; i < count; i++) {
int value = entries[i].value;
int level = entries[i].id == SOCKOPT_TCP_NODELAY ? IPPROTO_TCP : SOL_SOCKET;
int name = entries[i].id == SOCKOPT_TCP_NODELAY ? TCP_NODELAY : SO_REUSEADDR;
EXPECT_EQ_INT(setsockopt(fd, level, name, &value, sizeof(value)), 0);
}
int got = 0;
socklen_t len = sizeof(got);
EXPECT_EQ_INT(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &got, &len), 0);
EXPECT_EQ_INT(got, 1);
close(fd);
free(entries);
}
@@ -98,10 +94,8 @@ static void test_server_create_bind_address() {
opts.family = AF_INET;
Server* s = server_create_ex(0, &opts);
EXPECT_NOT_NULL(s);
if (s) {
EXPECT_EQ_INT(s->address.ss_family, AF_INET);
server_delete(&s);
}
EXPECT_EQ_INT(s->address.ss_family, AF_INET);
server_delete(&s);
}
/* An IPv6 bind is honored when the host supports it; on a host with no IPv6 a